Skybox使用OpenGL ES 2.0,Android NDK

这是我设置的东西。 加载.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帮助..)

链接地址: http://www.djcxy.com/p/33613.html

上一篇: Skybox using OpenGL ES 2.0, Android NDK

下一篇: Images and mask in OpenGL ES 2.0