为什么这个像素抽屉不能让我改变窗口大小/像素数?

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

我正在尝试构建一个简单的光线跟踪器,我正在将纹理映射到四边形以进行可视化。我开始的分辨率是250X250,但是当我把它更改为300X300时,它会编译并崩溃。

#include <stdlib.h>
#include <GL/glut.h>

using namespace std;

const int window_w = 250;
const int window_h = 250;

struct RGBType 
{
    float r;
    float g;
    float b;
};

GLuint tex = 0;

void init()
{
    RGBType pixels[ window_w*window_h ];

    RGBType* temp = pixels;
    for (int x = 0; x < window_w*window_h; x++) 
    {
            temp->r = 0;
            temp->g = 0;
            temp->b = 1;
            temp++;
    }

    glGenTextures( 1, &tex );
    glBindTexture( GL_TEXTURE_2D, tex );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, window_w, window_h, 0, GL_RGB, GL_FLOAT, NULL );
    glTexSubImage2D(GL_TEXTURE_2D,0,0,0,window_w,window_h,GL_RGB,GL_FLOAT,pixels);
}

void display(void)
{
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(0, 1, 0, 1, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3ub( 255, 255, 255 );
    glEnable( GL_TEXTURE_2D );
    glBindTexture( GL_TEXTURE_2D, tex );

    glBegin(GL_QUADS);
    glTexCoord2i( 0, 0 );
    glVertex2i( 0, 0 );
    glTexCoord2i( 1, 0 );
    glVertex2i( 1, 0 );
    glTexCoord2i( 1, 1 );
    glVertex2i( 1, 1 );
    glTexCoord2i( 0, 1 );
    glVertex2i( 0, 1 );
    glEnd();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(window_w,window_h);
    glutCreateWindow("Ray Tracer");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
opengl size pixel raycasting
1个回答
1
投票

可能是,你只是用完了堆栈。您正静态地分配一个大型数组,这可以轻松消耗分配给您的进程的整个堆栈。使用动态分配。你正在使用C ++,所以newdelete[]运营商:

RGBType *pixels = new RGBType[ window_w*window_h ];

for(int x = 0; x < window_w*window_h; x++) 
{
        pixels[x].r = 0;
        pixels[x].g = 0;
        pixels[x].b = 1;
}

glGenTextures( 1, &tex );
glBindTexture( GL_TEXTURE_2D, tex );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, window_w, window_h, 0, GL_RGB, GL_FLOAT, pixels );

delete[] pixels;
© www.soinside.com 2019 - 2024. All rights reserved.