2012-07-02 230 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