我有一个优化程序的想法,通过将屏幕转换为纹理,并在计算机完成计算时更新纹理,确保平滑缩放。
我实现了纹理部分,并设法将其写入屏幕。我确实遇到了一个问题,那就是屏幕非常模糊。当我做了更多的研究/调试时,我发现右上角的像素决定了整个屏幕。当它是黑色时,整个屏幕都会是黑色的。当它洗蓝时,整个屏幕会有一个蓝色的色调。我想它可能是乘以整个屏幕与右上角的像素?
它看起来是这样的:
x1c 0d1x的数据
它应该是什么样子:
的
下面是我的项目代码:
#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;
}
字符串
我试过重新编码纹理,试过重新编码。
1条答案
按热度按时间9lowa7mx1#
修好了!它确实被我调用的最后一个glColor 3f(x,y,z);函数所复用,我只需要在glBegin(GL_QUADS);之后添加glColor 3f(1,1,1);!感谢您的阅读,我希望这对您有所帮助:)