0
votes

I have a running program that uses glVertexPointer, glNormalPointer etc to draw a large number of elements. This works around 30 FPS. Now I've reach a point where integrating shaders would be a viable option (each vertex need to have a color calculated based on a specific class). 1. Now my first question is how much would using shaders affect my FPS ? My current implementation (which I'm 99.99% is flawed in at least some way, will post code below) drops the FPS drastically to 3 FPS. If this kind of drop in FPS is normal there is no point to struggle with this.

Now for some code. My shaders look like:

 vertexsource = """
                  attribute vec3 position; 
                  attribute vec3 normal;
                  varying vec3 norm; 
                  varying vec3 color_out;
                  uniform vec3 color_in;

                  void main() 
                  { 
                      gl_Position = gl_ModelViewProjectionMatrix * vec4( position,1);
                      color_out = color_in; 
                      norm = normal;
                  }""" 
fragmentsource = """ 
                  varying vec3 norm; 
                  varying vec3 color_out;
                  void main() 
                  { 
                      gl_FragColor = vec4( color_out, 1.0);
                  }""" 
vshader = compileShader( vertexsource, GL_VERTEX_SHADER )
fshader = compileShader( fragmentsource, GL_FRAGMENT_SHADER )
program = compileProgram( vshader, fshader ) 
color = glGetUniformLocation( program, "color_in")
normal = glGetAttribLocation( program, "normal" )
position = glGetAttribLocation( program, "position" )
glUseProgram( program )
glUniform3fv( color, 3, (0,0,1) )
return position, normal

So I return position and normal because I will use them later to pass the actual vertices and normals. Right from here I have a question. Without shaders I just pass the normals and vertices arrays using VertexPointer and NormalPointer and OpenGL handles the rest. 2. I've read that shaders have gl_Vertex and gl_Normal attributes build in. How do I pass the values from my VBO's to these ? As you can see currently I'm passing my vertexes position to the attribute position and my normals to normal, but I'm not doing anything with the normals as I don't know what.

The drawing is done like this:

    glEnableVertexAttribArray( self.position )
    glEnableVertexAttribArray( self.normal )      
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, self.bufferVertices)
    glVertexAttribPointer( self.position, 3, GL_FLOAT, GL_FALSE, 0, None )          
    glEnableClientState(GL_NORMAL_ARRAY);
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, self.bufferNormals)
    glVertexAttribPointer( self.normal, 3, GL_FLOAT, GL_FALSE, 0, None )
    glDrawElements(GL_TRIANGLES, len(self.triangles) , GL_UNSIGNED_SHORT, ADT.voidDataPointer(self.triangles))

Here self.bufferVertices, self.bufferNormals are VBO's containg my vertices and normals. self.triangles indices array. It draws the correct indices so far but my FPS is very low as mentioned.

  1. Is this drawing sequence correct? Also are there other things that when enabled could conflict with the shader? (GL_LIGHTNING, GL_DEPTH_TEST and so on)
1
The shader looks reasonable and as it's a simple pass through shader, it should actually be faster than fixed function. By the way, the bultin gl_Vertex, gl_Normal,... attributes are just set with the usual attributes (glVertex/glVertexPointer, glNormal/glNormalPointer,...), that's their advantage.Christian Rau
As Christian said, the shader code looks sane, as does the rest of what you posted... The drop in perfomance is definitely not normal though. What hardware is this running on ? Such a drastic drop in perfomance might point at a software fallback.Nicolas Lefebvre
It's running on a MAC OS X 10.5.8. The display is an NVIDIA GeForce 9400.Bogdan
Sounds like that should support such a minimal shader. It's hard to tell what could be going wrong without directly reproducing the problem. As far as I know, most things like GL_LIGHTING will just be disabled when you have a program active, they shouldn't interfere with performance.Nicolas Lefebvre

1 Answers

3
votes

Are you compiling your shader each time you call that function (the first code)?