2011-06-06 71 views
0

我正在學習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); 
} 

回答

0

唯一想我能想到的是,你沒有設置紋理座標corectlly在這裏:
sphereObject.Draw();

儘量使球精度較低(更低多邊形),看看你的紋理,以驗證您的紋理座標是正確的(或至少跨越每個像素變化的恆定的顏色,而不是

+0

Draw()函數內置於GLTriangleBatch類中,不是由我創建的。 – Ioncannon 2011-06-06 22:11:31

0

的一種方式整個球體)是改變你的片段着色器,輸出紋理座標作爲一種顏色。

#version 130 
uniform sampler2D colorMap; 
out vec4 vFragColor 
smooth in vec2 vVaryingTexCoords; 
void main(void) 
{ 
    vFragColor = vec4(vVaryingTexCoords, 0.0f, 1.0f); 
} 

如果你的球畫有一個恆定的顏色與此着色器那麼什麼是錯的與你的紋理座標。

另外,請確保在調用glUseProgram後調用任何glUniform *函數,因爲它們影響當前程序。