过去几周我一直在学习openGL ES 2.0,到目前为止我所做的一切都涉及到使用一个程序。我现在正在寻找使用多个程序,所以我可以让不同的着色器做不同的事情。
我遇到的问题是新程序和着色器正在使用,但是他们正在使用前一个程序的顶点坐标。我已将坐标绑定到单独的索引槽,但是它们没有被使用。
我知道正在使用正确的着色器,因为三角形呈蓝色闪烁而不是红色。因此我认为它是属性加载的问题。
我的代码:
着色
char vertexShaderSource[] =
"attribute vec4 Position; \n"
"uniform mat4 uMvp; \n"
"void main() \n"
"{ \n"
"gl_Position = uMvp * Position; \n"
"} \n";
char fragmentShaderSource[] =
"precision highp float;\n"
"uniform float time;\n"
"void main()\n"
"{\n"
"float c1 = sin(time);\n"
"if (c1 >= 0.0)"
"gl_FragColor = vec4(1.0,0.0,0.0,1.0);\n"
"else\n"
"gl_FragColor = vec4(0.0,0.0,0.0,1.0);\n"
"}\n";
char vertexShaderSource1[] =
"attribute vec4 Position1; \n"
"uniform mat4 uMvp; \n"
"void main() \n"
"{ \n"
"gl_Position = uMvp * Position1; \n"
"} \n";
char fragmentShaderSource1[] =
"precision highp float;\n"
"uniform float time;\n"
"void main()\n"
"{\n"
"float c1 = sin(time);\n"
"if (c1 >= 0.0)"
"gl_FragColor = vec4(0.0,0.0,1.0,1.0);\n"
"else\n"
"gl_FragColor = vec4(0.0,0.0,0.0,1.0);\n"
"}\n";
将属性绑定到单独的索引
glBindAttribLocation(mShader, 0, "Position");
glBindAttribLocation(mShader1, 1, "Position1");
Draw()函数
glUseProgram(mShader);
checkGLError("glUseProgram");
// ========================================================================
// define vertex coordinates
GLfloat Vertices[] = { -0.6f, 0.6f, 0.0f,
-0.5f, 0.48f, 0.0f,
-0.7f, 0.48f, 0.0f };
// ========================================================================
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// ========================================================================
// Set uniform function
glUniformMatrix4fv(mMvpLoc, 1, false, MyMatrix);
checkGLError("glUniform4fv");
glUniform1f(mTimeLoc, (maGetMilliSecondCount() - mStartTime) * 0.001f);
checkGLError("glUniform1f");
// ========================================================================
// Load the attribute data to the shaders
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, Vertices);
checkGLError("glVertexAttribPointer");
glEnableVertexAttribArray(0);
checkGLError("glEnableVertexAttribArray");
// ========================================================================
// Draw verticies
glDrawArrays(GL_TRIANGLES, 0, 3);
checkGLError("glDrawArrays");
glUseProgram(mShader1);
checkGLError("glUseProgram");
// ========================================================================
// define vertex coordinates
GLfloat Vertices1[] = { -0.5f, 0.12f, 0.0f,
-0.6f, 0.0f, 0.0f,
-0.7f, 0.12f, 0.0f };
// ========================================================================
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// ========================================================================
// Set uniform function
glUniformMatrix4fv(mMvpLoc, 1, false, MyMatrix);
checkGLError("glUniform4fv");
glUniform1f(mTimeLoc, (maGetMilliSecondCount() - mStartTime) * 0.001f);
checkGLError("glUniform1f");
// ========================================================================
// Load the attribute data to the shaders
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, Vertices1);
checkGLError("glVertexAttribPointer");
glEnableVertexAttribArray(1);
checkGLError("glEnableVertexAttribArray");
// ========================================================================
// Draw verticies
glDrawArrays(GL_TRIANGLES, 0, 3);
checkGLError("glDrawArrays");
答案 0 :(得分:0)
我明白了。我将program1的属性位置的索引更改为索引槽0.我还必须在第二个着色器绘制函数上取出清除缓冲区,这样它就不会删除绘制的第一个三角形。