Reputation: 1
I had an idea for optimizing my program, by converting the screen to a texture, and updating the texture when the computer is done calculating, assuring a smooth zoom.
I implemented the texture part, and managed to write it to the screen. I did run into a problem, that was that the screen was very faint. When I did a little more research/debugging, I found out that the top right pixel kind-of dictated the entire screen. When it was black, the entire screen would be black. When it wash blue-ish, the entire screen would have a blue tint. I think it might be multiplying the entire screen with the top right pixel?
This is how it looks:
How it should look:
Here's the code to my project:
#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;
}
I've tried resizing the texture, tried to code it again.
Upvotes: 0
Views: 39
Reputation: 1
Fixed it! It was indeed being mutliplied by the last glColor3f(x,y,z); function I called, I just needed to add glColor3f(1,1,1); after glBegin(GL_QUADS);! Thanks for reading, and I hope this helped you :)
Upvotes: 0