1

这就是我设置这些东西的方式。加载 .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);

这是立方体数据和纹理数据;这可能不是很错误,我使用相同的数据在桌面上创建 Skybox,使用 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 */                         
                        };

纹理坐标:

    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有所帮助..)

4

0 回答 0