天空盒不显示纹理

huangapple go评论73阅读模式
英文:

Skybox does not show textures

问题

我的天空盒被显示出来了,但是加载的纹理并没有显示出来,而是显示为黑色。

我正在使用具有多重采样帧缓冲支持的渲染器(链接:https://github.com/LWJGL/lwjgl3-demos/blob/master/src/org/lwjgl/demo/opengl/fbo/MultisampledFbo2Demo.java)。

我的纹理加载代码如下:

private int loadSkyboxTextures(){
        int texID = glGenTextures();
        glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
        for(int i = 0; i < TEXTURE_FILES.length; i++){
            InputStream file = getClass().getResourceAsStream(TEXTURE_FILES[i]);
            byte[] pixelData = new byte[0];
            try {
                pixelData = new byte[file.available()];
                file.read(pixelData);
            } catch (IOException e) {
                e.printStackTrace();
            }
            ByteBuffer byteBuffer = ByteBuffer.wrap(pixelData);
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, 512, 512, 0,
                    GL_RGB, GL_UNSIGNED_BYTE, byteBuffer);
        }

        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_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);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

        return texID;
    }

立方体渲染代码:

private void drawSkybox(){
        glEnable(GL_TEXTURE_CUBE_MAP);
        glDepthMask(false);
        glGenBuffers(vbo);
        glBindBuffer (GL_ARRAY_BUFFER, vbo[0]);
        glBufferData(GL_ARRAY_BUFFER, POINTS, GL_STATIC_DRAW);
        glGenVertexArrays(vao);
        glBindVertexArray(vao[0]);
        glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * Float.BYTES, NULL);
        glEnableVertexAttribArray(0);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_CUBE_MAP, texId);
        glDrawArrays(GL_TRIANGLES, 0, 36);
        glDisableVertexAttribArray(0);
        glBindVertexArray(0);
        glDepthMask(true);
        glBindBuffer (GL_ARRAY_BUFFER,0);
        glDisable(GL_TEXTURE_CUBE_MAP);
    }

在主渲染函数中的立方体渲染调用:

                glMatrixMode(GL_PROJECTION);
                glOrtho(-max, max, -1, 1, 10, -10);
                glRotated(cameraX, 1f, 0f, 0);
                glRotated(cameraY, 0f, 1f, 0);
                glGetFloatv(GL_PROJECTION_MATRIX, pm);

                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();
                drawSkybox();
                glLoadIdentity();
                ...
                // 渲染其他物体
英文:

My skybox is displayed, but does not display the textures that I load from the image. Instead, it shows the black color.

I am using a render with MultisampledFbo support.

My texture loading code looks like this:

private int loadSkyboxTextures(){
        int texID = glGenTextures();
        glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
        for(int i = 0; i &lt; TEXTURE_FILES.length; i++){
            InputStream file = getClass().getResourceAsStream(TEXTURE_FILES[i]);
            byte[] pixelData = new byte[0];
            try {
                pixelData = new byte[file.available()];
                file.read(pixelData);
            } catch (IOException e) {
                e.printStackTrace();
            }
            ByteBuffer byteBuffer = ByteBuffer.wrap(pixelData);
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, 512, 512, 0,
                    GL_RGB, GL_UNSIGNED_BYTE, byteBuffer);
        }

        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_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);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

        return texID;
    }

Cube Render Code:

    private void drawSkybox(){
        glEnable(GL_TEXTURE_CUBE_MAP);
        glDepthMask(false);
        glGenBuffers(vbo);
        glBindBuffer (GL_ARRAY_BUFFER, vbo[0]);
        glBufferData(GL_ARRAY_BUFFER, POINTS, GL_STATIC_DRAW);
        glGenVertexArrays(vao);
        glBindVertexArray(vao[0]);
        glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * Float.BYTES, NULL);
        glEnableVertexAttribArray(0);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_CUBE_MAP, texId);
        glDrawArrays(GL_TRIANGLES, 0, 36);
        glDisableVertexAttribArray(0);
        glBindVertexArray(0);
        glDepthMask(true);
        glBindBuffer (GL_ARRAY_BUFFER,0);
        glDisable(GL_TEXTURE_CUBE_MAP);
    }

The cube rendering call in the main render function:

                glMatrixMode(GL_PROJECTION);
                glOrtho(-max, max, -1, 1, 10, -10);
                glRotated(cameraX, 1f, 0f, 0);
                glRotated(cameraY, 0f, 1f, 0);
                glGetFloatv(GL_PROJECTION_MATRIX, pm);

                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();
                drawSkybox();
                glLoadIdentity();
                ...
                //render other objects

答案1

得分: 1

Legacy OpenGL的纹理映射必须被启用。为了执行立方体映射纹理,必须启用GL_TEXTURE_CUBE_MAP(参见glEnable):

glEnable(GL_TEXTURE_CUBE_MAP);

注意,对于立方体贴图纹理,纹理坐标是三维的,并被视为从立方体贴图中心开始的向量。

英文:

Legacy OpenGL's texturing has to be enabled. To perform cube-mapped texturing, GL_TEXTURE_CUBE_MAP has to be enabled (see glEnable):

glEnable(GL_TEXTURE_CUBE_MAP);

Note, for cube map textures, the texture coordinates are 3-dimensional and treated as a vector form the center of the cube map.

huangapple
  • 本文由 发表于 2020年4月9日 04:50:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61109787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定