在同一表面上映射多个纹理

时间:2012-02-09 15:09:29

标签: java opengl jogl texture-mapping render-to-texture

我正在渲染一个圆柱体并将两个不同的纹理映射到顶部和底部。

两种纹理都按以下方式加载:

    check3dFloorFile = new File(check3dFloorPath);
    try {
        check3dFloorTexture = TextureIO.newTexture(check3dFloorFile, true);
    } catch (IOException | GLException ex) {
        Logger.getLogger(Viewer.class.getName()).log(Level.SEVERE, null, ex);
    }
       
    check3dFloorTexture.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER, 
                                                   GL2.GL_LINEAR_MIPMAP_LINEAR);
    check3dFloorTexture.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER, 
                                                                 GL2.GL_LINEAR);
               
    check3dFloorBackFile = new File(check3dFloorBackPath);
           
    try {
        check3dFloorBackTexture = TextureIO.newTexture(check3dFloorBackFile, true);
    } catch (IOException | GLException ex) {
        Logger.getLogger(Viewer.class.getName()).log(Level.SEVERE, null, ex);
    }
     
    check3dFloorBackTexture.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER, 
                                                 GL2.GL_LINEAR_MIPMAP_LINEAR);
    check3dFloorBackTexture.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER, 
                                                               GL2.GL_LINEAR);

当我尝试渲染到一个纹理(一个空白的纹理,与前两个纹理无关)时会出现问题:

gl.glGenTextures(1, textureID, 0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
    
    gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
    gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
    gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
    gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
    
    // null means reserve texture memory, but texels are undefined
    gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGB, floorWidth, floorHeight,
                                                0, GL2.GL_RGB, GL2.GL_FLOAT, null);
    
    gl.glGenFramebuffers(1, frameBufferID, 0);
    gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, frameBufferID[0]);
    
    //Attach 2D texture to this FBO
    gl.glFramebufferTexture2D(GL2.GL_FRAMEBUFFER, GL2.GL_COLOR_ATTACHMENT0, 
                                    GL2.GL_TEXTURE_2D, textureID[0], 0);
    
    // depth buffer
    //int[] depthRenderBufferID = new int[1];
    gl.glGenRenderbuffers(1, depthRenderBufferID, 0);
    gl.glBindRenderbuffer(GL2.GL_RENDERBUFFER, depthRenderBufferID[0]);
    gl.glRenderbufferStorage(GL2.GL_RENDERBUFFER, GL2.GL_DEPTH_COMPONENT, 
                                            floorWidth, floorHeight);
    gl.glFramebufferRenderbuffer(GL2.GL_FRAMEBUFFER, GL2.GL_DEPTH_ATTACHMENT,
                                        GL2.GL_RENDERBUFFER, depthRenderBufferID[0]);
     
   if(gl.glCheckFramebufferStatus(GL2.GL_FRAMEBUFFER) == GL2.GL_FRAMEBUFFER_COMPLETE)
        System.out.println("[Viewer] GL_FRAMEBUFFER_COMPLETE!!");
    else
        System.out.println("..cazzo ^^");

执行此代码后,映射在圆柱顶部的纹理消失,所有顶部变为黑色......

为什么?

Ps:我的任务是将第三个渲染纹理映射到圆柱顶部,而不是现有的纹理。

1 个答案:

答案 0 :(得分:1)

gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
/*... */
gl.glGenFramebuffers(1, frameBufferID, 0);
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, frameBufferID[0]);

//Attach 2D texture to this FBO
gl.glFramebufferTexture2D(GL2.GL_FRAMEBUFFER, GL2.GL_COLOR_ATTACHMENT0, 
                                GL2.GL_TEXTURE_2D, textureID[0], 0);

您需要首先从纹理目标解除绑定纹理,然后才能将其附加到的帧缓冲区选为渲染目标。由于你没有这样做,你将不会对该纹理进行任何渲染,因此它会变成空白。