OpenGL纹理比它应该的要暗,有点被右上角的像素放大了

bogh5gae  于 2024-01-07  发布在  其他
关注(0)|答案(1)|浏览(247)

我有一个优化程序的想法,通过将屏幕转换为纹理,并在计算机完成计算时更新纹理,确保平滑缩放。
我实现了纹理部分,并设法将其写入屏幕。我确实遇到了一个问题,那就是屏幕非常模糊。当我做了更多的研究/调试时,我发现右上角的像素决定了整个屏幕。当它是黑色时,整个屏幕都会是黑色的。当它洗蓝时,整个屏幕会有一个蓝色的色调。我想它可能是乘以整个屏幕与右上角的像素?
它看起来是这样的:
x1c 0d1x的数据
它应该是什么样子:



下面是我的项目代码:

  1. #include <GL/glew.h>
  2. #define GLEW_STATIC
  3. #include <GL/glut.h>
  4. #include <iostream>
  5. const int maxIter = 250;
  6. long double xPos = -3.2;
  7. long double yPos = -2.0;
  8. long double zoom = 150;
  9. long double cReal, i0, zReal, zImag, zRealTemp, iter;
  10. const int WIDTH = 800, HEIGHT = 600;
  11. GLuint mandelbrotTexture;
  12. GLuint framebuffer;
  13. void init() {
  14. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  15. glutInitWindowSize(WIDTH, HEIGHT);
  16. glutCreateWindow("OpenGL Mandelbrot");
  17. glewExperimental = GL_TRUE;
  18. GLenum err = glewInit();
  19. if (err != GLEW_OK) {
  20. std::cerr << "GLEW initialization failed: " << glewGetErrorString(err) << std::endl;
  21. exit(EXIT_FAILURE);
  22. }
  23. // Generate texture
  24. glGenTextures(1, &mandelbrotTexture);
  25. glBindTexture(GL_TEXTURE_2D, mandelbrotTexture);
  26. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  29. // Create FBO
  30. glGenFramebuffers(1, &framebuffer);
  31. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  32. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mandelbrotTexture, 0);
  33. // Check for framebuffer completeness
  34. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  35. if (status != GL_FRAMEBUFFER_COMPLETE) {
  36. std::cerr << "Framebuffer not complete! Error code: " << status << std::endl;
  37. exit(EXIT_FAILURE);
  38. }
  39. // Set up OpenGL view
  40. glViewport(0, 0, WIDTH, HEIGHT);
  41. glMatrixMode(GL_PROJECTION);
  42. glLoadIdentity();
  43. glOrtho(0, WIDTH, 0, HEIGHT, -1, 1);
  44. glMatrixMode(GL_MODELVIEW);
  45. glLoadIdentity();
  46. }
  47. void mandel() {
  48. glClear(GL_COLOR_BUFFER_BIT);
  49. glBegin(GL_POINTS);
  50. for (int x = 0; x < WIDTH; x++) {
  51. for (int y = 0; y < HEIGHT; y++) {
  52. iter = 0;
  53. cReal = (x / zoom) + xPos;
  54. i0 = (y / zoom) + yPos;
  55. zReal = 0;
  56. zImag = 0;
  57. while (iter < maxIter && zReal * zReal + zImag * zImag < 4) {
  58. zRealTemp = ((zReal * zReal) - (zImag * zImag)) + cReal;
  59. zImag = 2 * zImag * zReal + i0;
  60. zReal = zRealTemp;
  61. iter++;
  62. }
  63. if (iter == maxIter) {
  64. glColor3f(0, 0, 0);
  65. } else {
  66. glColor3f(25 * iter / maxIter, 25 * iter / maxIter, 25 * iter / maxIter);
  67. }
  68. glVertex2i(x, y);
  69. }
  70. }
  71. glEnd();
  72. }
  73. void display() {
  74. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  75. mandel(); // draw the mandelbrot set to the screen
  76. glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind the framebuffer
  77. glClear(GL_COLOR_BUFFER_BIT);
  78. glEnable(GL_TEXTURE_2D);
  79. glBindTexture(GL_TEXTURE_2D, mandelbrotTexture);
  80. glBegin(GL_QUADS); // render the texture
  81. glTexCoord2f(0.0f, 0.0f);
  82. glVertex2i(0, 0);
  83. glTexCoord2f(1.0f, 0.0f);
  84. glVertex2i(WIDTH, 0);
  85. glTexCoord2f(1.0f, 1.0f);
  86. glVertex2i(WIDTH, HEIGHT);
  87. glTexCoord2f(0.0f, 1.0f);
  88. glVertex2i(0, HEIGHT);
  89. glEnd();
  90. glDisable(GL_TEXTURE_2D);
  91. glFlush();
  92. }
  93. int main(int argc, char** argv) {
  94. glutInit(&argc, argv);
  95. init();
  96. glutDisplayFunc(display);
  97. glutMainLoop();
  98. return 0;
  99. }

字符串
我试过重新编码纹理,试过重新编码。

9lowa7mx

9lowa7mx1#

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

相关问题