2012-07-02 164 views
0

为什么混合纹理的颜色?我该如何解决这个问题?Opengl多种纹理混合颜色

我这是怎么得出并初始化:

private void initGL_3D() { 
    int width = Display.getWidth(); 
    int height = Display.getHeight(); 
    glLoadIdentity(); // Reset The Projection Matrix 
    GLU.gluPerspective(45.0f, ((float) width/(float) height), 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window 
    glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix 

    glLoadIdentity(); // Reset The Modelview Matrix 

    glShadeModel(GL_SMOOTH); // Enables Smooth Shading 


    glClearDepth(1.0f); // Depth Buffer Setup 
    glEnable(GL_DEPTH_TEST); // Enables Depth Testing 
    glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do 
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations 
} 

private void initGL_2D() { 
    int width = Display.getWidth(); 
    int height = Display.getHeight(); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(0.0f, width, height, 0.0f, 1, -1); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glDisable(GL_DEPTH_TEST); 

    // Enable transparency 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
} 

private void renderGL() { 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    int width = Display.getWidth(); 
    int height = Display.getHeight(); 

    glEnable(GL_TEXTURE_2D); 

    glViewport(0, 0, width, height); // Reset The Current Viewport\ 
    glMatrixMode(GL_PROJECTION); // Select The Projection Matrix 
    if (game){ 
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background 
     initGL_3D(); 
     renderGL_3D(); 
    } 
    initGL_2D(); 
    renderGL_2D(); 
} 

private void renderGL_3D() { 
    glLoadIdentity(); 

    glTranslatef(0, 0f, -3f); 

    glRotatef(rotationX, 1.0f,0.0f,0.0f); 

    glRotatef(rotationY/4, 0.0f,1.0f,0.0f); 

    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, texture1.getTextureID()); 

    glActiveTexture(GL_TEXTURE1); 
    glBindTexture(GL_TEXTURE_2D, texture2.getTextureID()); 

    glBegin(GL_TRIANGLES); 
    glMultiTexCoord2f(GL_TEXTURE0, 0, 0); 
    glVertex3f(0, 0, 0); 

    glMultiTexCoord2f(GL_TEXTURE0, 1, 0); 
    glVertex3f(1, 0, 0); 

    glMultiTexCoord2f(GL_TEXTURE0, 0, 1); 
    glVertex3f(0, 1, 0); 

    glMultiTexCoord2f(GL_TEXTURE1, 1, 1); 
    glVertex3f(1, 1, 0); 

    glMultiTexCoord2f(GL_TEXTURE1, 1, 0); 
    glVertex3f(1, 0, 0); 

    glMultiTexCoord2f(GL_TEXTURE1, 0, 1); 
    glVertex3f(0, 1, 0); 
    glEnd(); 
} 

private void renderGL_2D() { 
    if (mainMenu) { 
    gui.setBackground(0); 
    glLoadIdentity(); 
    for (Button button : gui.buttons) { 
     float posX; 
     float posY; 

     if (button.posX == "center") { 
      posX = Display.getWidth()/2-button.width/2; 
     } else { 
      posX = Integer.parseInt(button.posX); 
     } 

     if (button.posY == "center") { 
      posY = Display.getHeight()/2-button.height/2; 
     } else { 
      posY = Integer.parseInt(button.posY); 
     } 

     if(guiMouseX > posX && guiMouseY > posY && guiMouseX < posX + button.width && guiMouseY < posY + button.height){ 
      button.textureOver.bind(); 
      button.mouseOver.run(); 
      if (Mouse.isButtonDown(0)) { 
       button.mouseDown.run(); 
      } 
     } else { 
      button.texture.bind(); 
     } 

     float imageWidth = button.texture.getImageWidth(); 
     float textureWidth = button.width/imageWidth; 
     float imageHeight = button.texture.getImageHeight(); 
     float textureHeight = button.height/imageHeight; 

     glBegin(GL_QUADS); 
     glTexCoord2f(0, 0); 
     glVertex2f(posX, posY); 
     glTexCoord2f(textureWidth, 0); 
     glVertex2f(posX + button.width, posY); 
     glTexCoord2f(textureWidth, textureHeight); 
     glVertex2f(posX + button.width, posY + button.height); 
     glTexCoord2f(0, textureHeight); 
     glVertex2f(posX, posY + button.height); 
     glEnd(); 
    } 
    } 
} 

借鉴一切正常,但唯一的问题是,纹理颜色混合!

这是质地如何看待当我运行的应用程序 enter image description here

,这些都是与theyr正常颜色

enter image description here

enter image description here

+0

请给自己解释一下。你能发布图片吗? – Tim

+0

@Tim我编辑的问题 – Qualphey

回答

1

由于纹理你正在调用“renderGL_ 3D“从”renderGL“中取出。这个函数绘制两个三角形,一个用texture0,另一个用texture1。

左下三角形的第0阶段texcoords全部为零。这使得它对绿色纹理的左上角进行采样,所以它也是绿色的。由于您已混合,第二个白色纹理在顶部混合,使其全部呈绿色。

要测试这一点,请尝试制作白色纹理洋红色的左上角像素或其他易于区分的颜色。现在左下角的三角形应该是一个肮脏的洋红色(由于过滤而混入一些绿色)。

至于你的解决方案,你需要禁用多纹理混合(而不是GL_BLEND - 它将光栅器提供的最终颜色混合到帧缓冲器中)。您可以通过

glActiveTexture(GL_TEXTURE0); 
    glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_REPLACE); 

与同为纹理单元1

glActiveTexture(GL_TEXTURE1); 
    glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_REPLACE); 

这应该做到这一点。

+0

是的,但为什么混合了颜色? – Qualphey

+0

我该如何解决这个问题? – Qualphey

+0

当绘制第二个三角形时,禁用混合或将第二个纹理绑定到纹理单元0 – Ani

0

如果你想多纹理的三角形,然后有两个问题:

在您设置的纹理第一三角坐标ONLY纹理1(应该是质地2也),

在第二个三角形你为ONLY纹理2设置纹理坐标(shoudl也适用于纹理1)。

第二个问题是不是glActiveTexture(GL_TEXTURE0),你应该使用glActiveTextureARB:

//glActiveTexture(GL_TEXTURE0); 
glActiveTextureARB(GL_TEXTURE0_ARB); 
glBindTexture(GL_TEXTURE_2D, texture1.getTextureID()); 

//glActiveTexture(GL_TEXTURE1); 
glActiveTextureARB(GL_TEXTURE1_ARB); 
glBindTexture(GL_TEXTURE_2D, texture2.getTextureID()); 

也就是多纹理的正确方法。

如果你想两个三角形有两个不同的纹理:

  • 要么使用阿特拉斯质感和设置的UV指向正确的坐标
  • 跳过多重纹理,设置第一纹理,画出三角形,设置第二个纹理,画出三角形。
+0

为什么建议在核心GL功能上使用'glActiveTextureARB'? – derhass

+0

这两个函数是相同的,除了明确获取指向'glActiveTextureARB'的指针,您肯定会了解函数的实现,而在老版本的OpenGL中'glActiveTexture'实现并不能保证。这取决于您的目标版本。如果您的目标版本是OpenGl 1.2,那么可以使用核心功能。 – Izzy