我正在學習OpenGL SuperBible一書中的OpenGL。我想渲染使用函數gltMakeSphere()創建的球體。我綁定一個紋理並運行useProgram()。着色器使用GLBatch處理三角形,但球體只有綠色。試圖在OpenGL中使用着色器紋理球體
下面是代碼:
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(0,0,-5.0f);
GLint locMVP = glGetUniformLocation(testShader, "mvpMatrix");
glBindTexture(GL_TEXTURE_2D, texture1ID);
glUniformMatrix4fv(locMVP, 1, GL_FALSE,
transformPipeline.GetModelViewProjectionMatrix());
glUseProgram(testShader);
testBatch.Draw();
sphereObject.Draw();
glutSwapBuffers();
glutPostRedisplay();
modelViewMatrix.PopMatrix();
}
的Vertex Shader
#version 130
in vec4 vVertex;
in vec2 vTexCoords;
smooth out vec2 vVaryingTexCoords;
void main(void)
{
vVaryingTexCoords = vTexCoords;
gl_Position = vVertex;
}
片段着色器
#version 130
uniform sampler2D colorMap;
out vec4 vFragColor;
smooth in vec2 vVaryingTexCoords;
void main(void)
{
vFragColor = texture(colorMap, vVaryingTexCoords.st);
}
Draw()函數內置於GLTriangleBatch類中,不是由我創建的。 – Ioncannon 2011-06-06 22:11:31