OpenGL 纹理比应有的更暗,有点与右上角像素相乘

问题描述 投票:0回答:1

抱歉标题令人困惑,不太确定如何格式化它。 我有一个想法来优化我可怕的程序,将屏幕转换为纹理,并在计算机完成计算时更新纹理,确保平滑缩放。

我实现了纹理部分,并设法将其写入屏幕。我确实遇到了一个问题,那就是屏幕很暗。当我做了更多的研究/调试时,我发现右上角的像素决定了整个屏幕。当它是黑色的时候,整个屏幕都是黑色的。当它洗成蓝色时,整个屏幕都会呈现蓝色。我认为它可能是将整个屏幕与右上角的像素相乘?

它看起来是这样的: how it looks 它应该是什么样子: how it should look 这是我的项目的代码:

#include <GL/glew.h>
#define GLEW_STATIC
#include <GL/glut.h>
#include <iostream>

const int maxIter = 250;
long double xPos = -3.2;
long double yPos = -2.0;
long double zoom = 150;

long double cReal, i0, zReal, zImag, zRealTemp, iter;
const int WIDTH = 800, HEIGHT = 600;

GLuint mandelbrotTexture;
GLuint framebuffer;

void init() {
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutCreateWindow("OpenGL Mandelbrot");

    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if (err != GLEW_OK) {
        std::cerr << "GLEW initialization failed: " << glewGetErrorString(err) << std::endl;
        exit(EXIT_FAILURE);
    }

    // Generate texture
    glGenTextures(1, &mandelbrotTexture);
    glBindTexture(GL_TEXTURE_2D, mandelbrotTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    // Create FBO
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mandelbrotTexture, 0);

    // Check for framebuffer completeness
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE) {
        std::cerr << "Framebuffer not complete! Error code: " << status << std::endl;
        exit(EXIT_FAILURE);
    }

    // Set up OpenGL view
    glViewport(0, 0, WIDTH, HEIGHT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, WIDTH, 0, HEIGHT, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void mandel() {
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POINTS);
    for (int x = 0; x < WIDTH; x++) {
        for (int y = 0; y < HEIGHT; y++) {
            iter = 0;
            cReal = (x / zoom) + xPos;
            i0 = (y / zoom) + yPos;
            zReal = 0;
            zImag = 0;
            while (iter < maxIter && zReal * zReal + zImag * zImag < 4) {
                zRealTemp = ((zReal * zReal) - (zImag * zImag)) + cReal;
                zImag = 2 * zImag * zReal + i0;
                zReal = zRealTemp;
                iter++;
            }
            if (iter == maxIter) {
                glColor3f(0, 0, 0);
            } else {
                glColor3f(25 * iter / maxIter, 25 * iter / maxIter, 25 * iter / maxIter);
            }
            glVertex2i(x, y);
        }
    }
    glEnd();
}

void display() {
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    mandel(); // draw the mandelbrot set to the screen
    glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind the framebuffer

    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, mandelbrotTexture);

    glBegin(GL_QUADS); // render the texture
    glTexCoord2f(0.0f, 0.0f);
    glVertex2i(0, 0);
    glTexCoord2f(1.0f, 0.0f);
    glVertex2i(WIDTH, 0);
    glTexCoord2f(1.0f, 1.0f);
    glVertex2i(WIDTH, HEIGHT);
    glTexCoord2f(0.0f, 1.0f);
    glVertex2i(0, HEIGHT);
    glEnd();

    glDisable(GL_TEXTURE_2D);

    glFlush();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    init();
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

我调试了太长时间,尝试联系 ChatGPT,但都不起作用。同样的问题。我尝试过调整纹理大小,尝试再次编码。

opengl textures glut fractals mandelbrot
1个回答
0
投票

修好了!它确实被最后一个 glColor3f(x,y,z); 相乘;我调用的函数,我只需要添加 glColor3f(1,1,1);在 glBegin(GL_QUADS); 之后感谢您的阅读,希望这对您有所帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.