这就是我设置的方式。加载.tga文件很好,我已经将这个加载用于2D纹理映射,效果很好。
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &engine-> GLData.gTextureId); checkGlError("glGenTextures gTextureId");
glEnable(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, engine-> GLData.gTextureId); checkGlError("glBindTexture gTextureId");
//glActiveTexture(GL_TEXTURE0); checkGlError("glActiveTexture");
for(int i = 0 ; i < 6 ; i++)
{
//glGenerateMipmap() see if u really need mipmapping
pbImageData = TGALoader(szCubeTextureFiles[i], &iWidth, &iHeight, &iComponents, &eFormat, engine-> app-> activity-> assetManager);
if(pbImageData != NULL)
{
LOGI("texture %s shoudl be loaded", szCubeTextureFiles[i]);
glTexImage2D(cubeTexTarget[i], 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pbImageData);
checkGlError("glTexImage2D");
free(pbImageData);
}
else
LOGI("Texture %s failed to laod from disk", szCubeTextureFiles[i]);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
这是立方体数据和纹理数据;这可能不是很错,我使用相同的数据在桌面上使用OpenGL创建Skybox
GLfloat Cube_Vertices[] = { -1.0, -1.0, 1.0, // Front
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
/* And so on for other faces, CCW */
};
纹理坐标:
GLfloat Cube_Textures[] = {
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f,
/* And similarly, remaining tex-coords */
};
没有什么真正看好着色器,顶点着色器只是填充位置并传递纹理坐标和片段着色器尝试使用
来映射纹理 " gl_FragColor = textureCube(s_texture, vTexCoord); \n"
这是我尝试渲染的方式
glVertexAttribPointer( engine->GLData.gPositionAttribute, 3, GL_FLOAT, GL_FALSE, 0, Cube_Vertices);
glVertexAttribPointer( engine->GLData.gTextureAttribute, 2, GL_FLOAT, GL_FALSE, 0, Cube_Textures);
glEnableVertexAttribArray( engine->GLData.gPositionAttribute);
glEnableVertexAttribArray( engine->GLData.gTextureAttribute);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, engine-> GLData.gTextureId);
// THE USUAL STUFF HERE.. MATRIX MULTIPLICATION & LOADING UNIFORMS,
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, Cube_Indices);
glDisableVertexAttribArray( engine->GLData.gPositionAttribute);
glDisableVertexAttribArray( engine->GLData.gTextureAttribute);
eglSwapBuffers(engine->EGL.display, engine->EGL.surface);
最后但非常重要的相机设置,我希望是“内部”立方体,并从内部映射纹理。
MPerspective(engine->Matrices.pMatrix, 1.25, 0.1f, 150.0f);
glViewport(0, 0, engine->Scr.width, engine->Scr.height); checkGlError("glViewport");
GLfloat pose[] = { 0.0, 0.5, 6.0};
GLfloat view[] = { 0.0, 0.5, 0.0};
GLfloat upVx[] = { 0.0, 1.0, 0.0};
LookAtM( engine->Matrices.cMatrix, pose, view, upVx);
感谢您的时间和耐心,非常感谢您的帮助。 (已经花了1.5天,试了很多东西,希望至少StackOverFlow有帮助..)