Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:24:36+00:00 2026-06-18T04:24:36+00:00

I’m trying to run a program using shaders. In Another question I made ,

  • 0

I’m trying to run a program using shaders. In Another question I made, I discovered that I was mixing the fixed pipeline functions with the newer stuff (from OpenGL 2.0), so I tried to remove all the “old stuff” like glMatrixMode, but I’m not sure if I’m doing it correctly. In the doubt I also removed calls like glLightfv which I’m not really sure if they’re allowed if I used a programmable pipeline.

This is the main code:

#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>
#include <vector>
#include "utility.hpp"
#include "program.hpp"

GLfloat width=600, height=800;
Program* program_ref;

void init()
{   
    glewInit();
    Shader vertex_shader= Shader("vertex_shader",GL_VERTEX_SHADER);
    // This just reads the file "vertex_shader", creates the shader and compiles it
    Shader geometry_shader= Shader(); // This means that the shader is empty
    // So the program class will be enough smart to recognize it and don't attach it
    Shader fragment_shader= Shader("fragment_shader",GL_FRAGMENT_SHADER);
    program_ref= new Program(vertex_shader,geometry_shader,fragment_shader);
    // This creates a program, attaches the shaders to it, links and uses it
}

void display()
{
    vector<GLfloat> quad{-0.5,-0.5,0.5,-0.5,0.5,0.5,-0.5,0.5};
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2,GL_FLOAT,0,quad.data());
    glDrawArrays(GL_QUADS,0,4);
    glDisableClientState(GL_VERTEX_ARRAY);
    glutSwapBuffers();
    cout << glGetError() << endl;
}

void reshape(int w, int h)
{
    width=w;
    height=h;
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(500,500);
    glutCreateWindow("test");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    init();
    glewInit();
    glutMainLoop();
    return 0;
}

(Optional classes to see, in the comments I wrote how are Program and Shader are behaving)

shader.hpp:

#ifndef shader_hpp
#define shader_hpp

#include "utility.hpp"
#include "shader.hpp"
#include <fstream>

#define MAX_SIZE (size_t)1.0e5


class Shader
{
private:
    GLuint id;
    GLenum type;
    bool null;
public:
    Shader()
    {
        id=-1;
        type= -1;
        null= true;
    }
    Shader(const string& filename,GLenum type)
    {
        GLchar* data= new GLchar[MAX_SIZE];
        streamsize count;
        this->type= type;
        ifstream is;
        is.open(filename);
        is.read(data,MAX_SIZE);
        count= is.gcount();
        is.close();
        id=glCreateShader(type);
        glShaderSource(id,1,(const GLchar**)&data,&count);
        glCompileShader(id);
        delete[] data;
        null= false;
    }
    GLuint getId() const
    {
        return id;
    }
    GLenum getType() const
    {
        return type;
    }
    bool isNull() const
    {
        return null;
    }
};

#endif

program.hpp:

#ifndef program_hpp
#define program_hpp

#include "utility.hpp"
#include "shader.hpp"

class Program
{
private:
    GLuint id;
public:
    Program(const Shader& vertex_shader, const Shader& geometry_shader, const Shader& fragment_shader)
    {
    id= glCreateProgram();
    if(!vertex_shader.isNull())
    {
        glAttachShader(id,vertex_shader.getId());
    }
    if(!geometry_shader.isNull())
    {
        glAttachShader(id,geometry_shader.getId());
    }
    if(!fragment_shader.isNull())
    {
        glAttachShader(id,fragment_shader.getId());
    }
    glLinkProgram(id);
    glUseProgram(id);
    }
    GLuint getId()
    {
    return id;
    }
};

#endif

There two classes are just made to simplify everything when using glew to compile the shaders and attach them to the program.

The problem is the following: If I just attach the vertex shader and not the fragment shader, all is fine and I see the quad drawn with the right color (red, because in the vertex shader below I’ll set the color to red). If instead I also attach the fragment shader I see that the quad is white. I also tried to see what wasn’t right and I printed the result of glGetError(). If I include the fragment shader I get error 1282, if I don’t include it I don’t get any error (0).

Vertex shader:

void main()
{
    gl_Position    = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_FrontColor  = vec4(1,0,0,1);
    // The only relevant thing I do is to set the color to red
    // I see the quad red if I attach only the vertex shader and not the fragment shader
    gl_TexCoord[0] = gl_MultiTexCoord0;
}

Fragment shader:

void main()
{
    gl_fragColor= gl_Color;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:24:37+00:00Added an answer on June 18, 2026 at 4:24 am

    Give this a shot:

    #include <GL/glew.h>
    #include <GL/glut.h>
    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    void CheckStatus( GLuint obj )
    {
        GLint status = GL_FALSE, len = 10;
        if( glIsShader(obj) )   glGetShaderiv( obj, GL_COMPILE_STATUS, &status );
        if( glIsProgram(obj) )  glGetProgramiv( obj, GL_LINK_STATUS, &status );
        if( status == GL_TRUE ) return;
        if( glIsShader(obj) )   glGetShaderiv( obj, GL_INFO_LOG_LENGTH, &len );
        if( glIsProgram(obj) )  glGetProgramiv( obj, GL_INFO_LOG_LENGTH, &len );
        vector< char > log( len, 'X' );
        if( glIsShader(obj) )   glGetShaderInfoLog( obj, len, NULL, &log[0] );
        if( glIsProgram(obj) )  glGetProgramInfoLog( obj, len, NULL, &log[0] );
        cerr << &log[0] << endl;
        exit( -1 );
    }
    
    GLuint LoadShader( GLenum type, const char* src )
    {
        GLuint shader = glCreateShader( type );
        glShaderSource( shader, 1, &src, NULL );
        glCompileShader( shader );
        CheckStatus( shader );
        return shader;
    }
    
    GLuint LoadProgram( const char* vert, const char* geom, const char* frag )
    {
        GLuint program = glCreateProgram();
        if( vert ) glAttachShader( program, LoadShader( GL_VERTEX_SHADER, vert ) );
        if( geom ) glAttachShader( program, LoadShader( GL_GEOMETRY_SHADER, geom ) );
        if( frag ) glAttachShader( program, LoadShader( GL_FRAGMENT_SHADER, frag ) );
        glLinkProgram( program );
        CheckStatus( program );
        return program;
    }
    
    #define GLSL(version, shader)  "#version " #version "\n" #shader
    
    const GLchar* vert = GLSL
    (
        120,
        void main()
        {
            gl_Position    = gl_ModelViewProjectionMatrix * gl_Vertex;
            gl_FrontColor  = vec4(1.0,0.0,0.0,1.0);
            // The only relevant thing I do is to set the color to red
            // I see the quad red if I attach only the vertex shader and not the fragment shader
            gl_TexCoord[0] = gl_MultiTexCoord0;
        }
    );
    
    const GLchar* frag = GLSL
    (
        120,
        void main()
        {
            gl_FragColor= gl_Color;
        }
    );
    
    void display()
    {
        glClearColor(0,0,0,0);
        glClear(GL_COLOR_BUFFER_BIT);
    
        static GLuint prog = LoadProgram( vert, NULL, frag );
        glUseProgram( prog );
        glEnableClientState(GL_VERTEX_ARRAY);
        GLfloat quad[] = {-0.5,-0.5,0.5,-0.5,0.5,0.5,-0.5,0.5};
        glVertexPointer(2,GL_FLOAT,0,quad);
        glDrawArrays(GL_QUADS,0,4);
        glDisableClientState(GL_VERTEX_ARRAY);
        glutSwapBuffers();
    }
    
    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
        glutInitWindowPosition(100,100);
        glutInitWindowSize(500,500);
        glutCreateWindow("test");
        glutDisplayFunc(display);
        glewInit();
        glutMainLoop();
        return 0;
    }
    

    As Antonie Blom pointed out gl_fragColor should be gl_FragColor. The lower-case f chokes the GLSL compiler on my system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.