我正在通过OpenGL超级圣经第五版,并尝试使用多个纹理和一个glbatch对象。但是我无法获得任何多于一个纹理来渲染,而我指定用纹理2和3渲染的区域只是默认灰色。我目前只是在每个面上渲染一个具有不同纹理的立方体,没有混合,也没有花哨的插值。
我在论坛上看到线程声明glbatch可以轻松支持多个纹理,这是真的吗?如果是的话,我错过了什么?或者这是需要我编写一个小着色器的东西,因为我能找到的唯一多重纹理示例使用了一个小的自定义着色器(然而这是一个反射示例,因此它开始时非常复杂),如果是这样,有人会指出我如何开始编写所述着色器的方向。
此外,我使用GLShaderManager和股票着色器'GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF'我不确定这是否是我的问题这个着色器是否支持多个纹理?
代码:
GLBatch cubeBatch;
GLuint TEXID_Tex0;
GLuint TEXID_Tex1;
GLuint TEXID_Tex2;
// The initialization
void SetupRC()
{
// Turn on Culling
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Light Gray Background
glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
shaderManager.InitializeStockShaders();
// Load textures
glGenTextures(1, &TEXID_Tex0);
glBindTexture(GL_TEXTURE_2D, TEXID_Tex0);
LoadTGATexture("Texture1.tga", GL_LINEAR, GL_LINEAR, GL_REPEAT);
glGenTextures(1, &TEXID_Tex1);
glBindTexture(GL_TEXTURE_2D, TEXID_Tex1);
LoadTGATexture("Texture2.tga", GL_LINEAR, GL_LINEAR, GL_REPEAT);
glGenTextures(1, &TEXID_Tex2);
glBindTexture(GL_TEXTURE_2D, TEXID_Tex2);
LoadTGATexture("Texture3.tga", GL_LINEAR, GL_LINEAR, GL_REPEAT);
// Set the textures to their texture units
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, TEXID_Tex2);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, TEXID_Tex1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, TEXID_Tex0);
// Fill the cube batch with data
CreateCube(cubeBatch);
// Move the camera up a bit
cameraFrame.MoveForward(-7.0f);
}
void CreateCube(GLBatch& cubeBatch)
{
// Start the batch
cubeBatch.Begin(GL_TRIANGLES, 18, 3);
cubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
cubeBatch.MultiTexCoord2f(1, 1.0f, 1.0f);
cubeBatch.Vertex3f(1.0f, 1.0f, 1.0f);
cubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
cubeBatch.MultiTexCoord2f(1, 1.0f, 0.0f);
cubeBatch.Vertex3f(1.0f, 1.0f, -1.0f);
cubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
cubeBatch.MultiTexCoord2f(1, 0.0f, 0.0f);
cubeBatch.Vertex3f(-1.0f, 1.0f, -1.0f);
cubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
cubeBatch.MultiTexCoord2f(1, 1.0f, 1.0f);
cubeBatch.Vertex3f(1.0f, 1.0f, 1.0f);
cubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
cubeBatch.MultiTexCoord2f(1, 0.0f, 0.0f);
cubeBatch.Vertex3f(-1.0f, 1.0f, -1.0f);
cubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
cubeBatch.MultiTexCoord2f(1, 0.0f, 1.0f);
cubeBatch.Vertex3f(-1.0f, 1.0f, 1.0f);
cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
cubeBatch.Vertex3f(-1.0f, 1.0f, 1.0f);
cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
cubeBatch.Vertex3f(-1.0f, 1.0f, -1.0f);
cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
cubeBatch.Vertex3f(-1.0f, -1.0f, -1.0f);
cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
cubeBatch.Vertex3f(-1.0f, 1.0f, 1.0f);
cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
cubeBatch.Vertex3f(-1.0f, -1.0f, -1.0f);
cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
cubeBatch.Vertex3f(-1.0f, -1.0f, 1.0f);
cubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(2, 0.0f, 0.0f);
cubeBatch.Vertex3f(1.0f, -1.0f, -1.0f);
cubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(2, 1.0f, 0.0f);
cubeBatch.Vertex3f(1.0f, 1.0f, -1.0f);
cubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(2, 1.0f, 1.0f);
cubeBatch.Vertex3f(1.0f, 1.0f, 1.0f);
cubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(2, 1.0f, 1.0f);
cubeBatch.Vertex3f(1.0f, 1.0f, 1.0f);
cubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(2, 0.0f, 1.0f);
cubeBatch.Vertex3f(1.0f, -1.0f, 1.0f);
cubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
cubeBatch.MultiTexCoord2f(2, 0.0f, 0.0f);
cubeBatch.Vertex3f(1.0f, -1.0f, -1.0f);
cubeBatch.End();
}
void RenderScene(void)
{
static GLfloat vLightPos [] = { 1.0f, 1.0f, 0.0f };
static GLfloat vWhite [] = { 1.0f, 1.0f, 1.0f, 1.0f };
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
modelViewMatrix.PushMatrix();
M3DMatrix44f mCamera;
cameraFrame.GetCameraMatrix(mCamera);
modelViewMatrix.MultMatrix(mCamera);
M3DMatrix44f mObjectFrame;
objectFrame.GetMatrix(mObjectFrame);
modelViewMatrix.MultMatrix(mObjectFrame);
shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF,
transformPipeline.GetModelViewMatrix(),
transformPipeline.GetProjectionMatrix(),
vLightPos, vWhite, 0);
glEnable(GL_CULL_FACE);
cubeBatch.Draw();
glDisable(GL_CULL_FACE);
modelViewMatrix.PopMatrix();
// Flush drawing commands
glutSwapBuffers();
}
bool LoadTGATexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode)
{
GLbyte *pBits;
int nWidth, nHeight, nComponents;
GLenum eFormat;
// Read the texture bits
pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat);
if(pBits == NULL)
return false;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0,
eFormat, GL_UNSIGNED_BYTE, pBits);
free(pBits);
if(minFilter == GL_LINEAR_MIPMAP_LINEAR ||
minFilter == GL_LINEAR_MIPMAP_NEAREST ||
minFilter == GL_NEAREST_MIPMAP_LINEAR ||
minFilter == GL_NEAREST_MIPMAP_NEAREST)
glGenerateMipmap(GL_TEXTURE_2D);
return true;
}
唯一没有包含的内容是矩阵堆栈和框架的声明,以及用于改变大小和处理鼠标移动的misc函数。