gpt4 book ai didi

java - Opengl 多种纹理混合颜色

转载 作者:行者123 更新时间:2023-12-01 15:18:31 24 4
gpt4 key购买 nike

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

这就是我绘制和初始化的方式:

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

这些是具有正常颜色的纹理

enter image description here

enter image description here

最佳答案

因为您是从“renderGL”内部调用“renderGL_3D”。该函数绘制两个三角形,一个使用texture0,另一个使用texture1。

左下角三角形的第 0 级纹理坐标全为零。这使得它对绿色纹理的左上角进行采样,因此它也是绿色的。由于您已启用混合,因此第二个白色纹理会混合在顶部,使其全部变为绿色。

要对此进行测试,请尝试将白色纹理的左上角像素设为洋红色或其他易于区分的颜色。现在左下角的三角形应该是脏洋红色(由于过滤而混入了一些绿色)。

对于您的解决方案,您需要禁用多重纹理混合(不是 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);

这应该有效。

关于java - Opengl 多种纹理混合颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11301741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com