0

I am trying to add some shaders to my glut scene objects.

At this time I am trying to implement "hello world" shaders but when I use the default vertex shader, my objects dissapear.

shaders:

#define GLSL(version, shader) "#version " #version " core\n" #shader
const char* vert = GLSL
(
330,

layout (std140) uniform Matrices {
        mat4 pvm;
} ;

in vec4 position;

out vec4 color;

void main()
{
    color = position;
    gl_Position = pvm * position ;
} 
);

const char* frag = GLSL
(
330,    

in vec4 color;

out vec4 outputF;

void main()
{
    outputF = vec4(1.0, 0.5, 0.25, 1.0);
} 

);

Compilation shows no error:

Compiling shader : vertex shader
VERTEX STATUS:1
Compiling shader : fragment shader
FRAGMENT STATUS:1
Linking program
PROGRAM STATUS:1
PROGRAM ID : 3

Before calling glUseProgram:

enter image description here

After calling glUseProgram:

enter image description here

After calling glUseProgram without attach vertex shader: enter image description here

CODE for rendering:

int opengl_draw_path_gl(rendered_path_t *p) {
    unsigned int num_vertices,j;
    unsigned int face_size;
    unsigned long i,num_elems;
    vect_t *a,*b;
    num_elems=p->num_prisms;
    num_vertices=p->prism_faces;
    face_size=num_vertices*2;
    a=p->data+2; // saltem punt centre primera cara
    b=a+face_size;

    glColor4fv(p->color);

    // dibuixem tapa inici
    _opengl_draw_path_terminator(num_vertices,p->data,a);

    // Dibuixem tots els prismes
    glBegin(GL_TRIANGLE_STRIP);
    for(i=0;i<num_elems;i++) {
        for(j=0;j<num_vertices;j++) {
            glNormal3fv((GLfloat *)(a+j*2));
            glVertex3fv((GLfloat *)(a+j*2+1));
            glNormal3fv((GLfloat *)(b+j*2));
            glVertex3fv((GLfloat *)(b+j*2+1));
        }
        glNormal3fv((GLfloat *)(a));
        glVertex3fv((GLfloat *)(a+1));
        glNormal3fv((GLfloat *)(b));
        glVertex3fv((GLfloat *)(b+1));
        a+=face_size;
        b+=face_size;
    }
    glEnd();

    // dibuixem tapa final
    _opengl_draw_path_terminator(num_vertices,b,a);
   return 0;
}
Ripi2
  • 7,031
  • 1
  • 17
  • 33
Charles
  • 98
  • 8
  • I am not calling to glVertexAttrib anywhere, it is mandatory? – Charles Sep 20 '17 at 07:45
  • glBegin, glVertex, glNormal, etc are the **very old** OpenGL fixed-pipeline. All of them are incompatible if you use a OpenGL **Core** Profile (as you set in the first line of your shaders). – Ripi2 Sep 20 '17 at 17:44

1 Answers1

2

First of all I recommend you, to read a tutorial about vertex array objects.

But, since you are drawing with glBegin and glEnd, which is deprecated, you have to use compatibility mode shaders. You have to use the deprecated built in uniforms gl_Vertex and gl_Normal, according to the OpenGL commands glVertex3fv and glNormal3fv.

Adapt your code somhow like this:

#define GLSL(version, shader) "#version " #version "\n" #shader

Vertex shader:

const char* vert = GLSL
(
    110,

    varying vec4 position;
    varying vec3 normal;

    void main()
    {
        position    = gl_ModelViewMatrix * gl_Vertex;
        normal      = normalize( gl_NormalMatrix * gl_Normal.xyz );
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    } 
);

Fragment shader:

const char* frag  = GLSL
(
    110,

    varying vec4 position;
    varying vec3 normal;

    void main()
    {
        gl_FragColor = vec4(1.0, 0.5, 0.25, 1.0);
    } 
);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174