问题描述
我有一个正在运行的程序,该程序使用glVertexPointer,glNormalPointer等绘制大量元素.这大约可以达到30 FPS.现在,我已经达到了集成着色器将是一个可行选择的地步(每个顶点都需要具有基于特定类计算的颜色).现在,我的第一个问题是使用着色器会对我的FPS有多大影响?我目前的实施方式(我的99.99%至少存在某种缺陷,将在下面发布代码)将FPS大幅降低至3 FPS.如果FPS的这种下降是正常的,那就没有必要为此奋斗了.
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
所以我返回位置和法线,因为稍后将使用它们传递实际的顶点和法线.从这里我有一个问题.没有着色器,我只使用VertexPointer和NormalPointer传递法线和顶点数组,而OpenGL处理其余部分.2.我读过着色器内置了gl_Vertex和gl_Normal属性.如何将VBO中的值传递给这些着色器?如您所见,当前我正在将顶点位置传递到属性位置,将法线传递给法线,但是我不对法线做任何事情,因为我不知道什么.
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.
绘图是这样完成的:
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))
在这里self.bufferVertices,self.bufferNormals是VBO包含我的顶点和法线. self.triangles索引数组.到目前为止,它可以绘制正确的索引,但是我的FPS却很低.
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.
- 此绘图顺序是否正确?另外,启用后还可能与着色器发生冲突吗? (
GL_LIGHTNING
,GL_DEPTH_TEST
等)
推荐答案
每次调用该函数(第一个代码)时,您是否都在编译着色器?
Are you compiling your shader each time you call that function (the first code)?
这篇关于OpenGL着色器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!