Skybox using OpenGL ES 2.0, Android NDK
This is how I setup the things. Loading .tga files is fine, I have used that loading for 2D texture mapping, which works fine.
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);
This is Cube Data and Texture Data; This may not be very wrong, I have used same data to create Skybox on desktop, using OpenGL
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 */
};
Texture Coords:
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 */
};
Nothing really fancy about shaders, vertex shader just fills in the position and pass-through the texture coords and fragment shader tries to map texture using
" gl_FragColor = textureCube(s_texture, vTexCoord); n"
The this is how i try to render
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);
Last but very important, camera settings, I expect to be "inside" cube, and textures be mapped from inside.
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);
Thank you for your time, and patience, your help is highly appreciated. (Have already spent 1.5 days, tried out many things, hope atleast StackOverFlow helps..)
链接地址: http://www.djcxy.com/p/33614.html上一篇: 斯卡拉与Groovy与Clojure