这是代码。输出始终是一个灰色方块-始终都是输入,无论输入是什么,并且它显然是错误的。我的目标是能够将所有像素存储在某个位置并显示它们,以便我可以继续使用简单的光线跟踪器,而我似乎无法弄清楚这glDrawPixels的问题。

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

using namespace std;

struct RGBType {

    float r;
    float g;
    float b;
    //float a;
};


void display(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    RGBType *pixels = new RGBType[250*250];

    for (int x = 0; x < 250; x++) {
        for (int y = 0; y < 250; y++) {

               pixels->r = 0;
               pixels->g = 1;
               pixels->b = 1;
               //pixels->a = 200;
          }
     }


    glTexSubImage2D(GL_TEXTURE_2D,0,0,0,250,250,GL_RGB,GL_UNSIGNED_BYTE,pixels);


    //glColor3f(1.0,1.0,1.0);
    glBegin(GL_POLYGON);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(1.0, 1.0, 0.0);
        glVertex3f(0.0, 1.0, 0.0);
    glEnd();


    glutSwapBuffers();
}

void init(void)
{
    //select clearing (background) color
    glClearColor(0.0, 0.0, 0.0, 0.0);

    //initialize viewing values
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}


int main(int argc, char** argv)
{
    //Initialise GLUT with command-line parameters.
    glutInit(&argc, argv);

    //Set Display Mode
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    //Set the window size
    glutInitWindowSize(250,250);

    //Set the window position
    glutInitWindowPosition(100,100);

    //Create the window
    glutCreateWindow("Ray Tracer");

    //Call init (initialise GLUT
    init();

    //Call "display" function
    glutDisplayFunc(display);

    //Enter the GLUT event loop
    glutMainLoop();

    return 0;
}

最佳答案

struct RGBType {
    float r;
    float g;
    float b;
    ^^^^^^^^ all floats here...
    //float a;
};

...

RGBType *pixels = new RGBType[250*250];

...

glTexSubImage2D(GL_TEXTURE_2D,0,0,0,250,250,GL_RGB,GL_UNSIGNED_BYTE,pixels);
                                                   ^^^^^^^^^^^^^^^^ wat

您要告诉OpenGL将pixels解释为unsigned char的数组,以four bytes组的形式读取它们,并将每四个的前三个字节用作RGB通道。

不要对OpenGL说谎。最后很少为您解决问题。

请尝试GL_FLOAT

并首先创建一个实际的纹理对象。

在尝试向其上载数据之前,请绑定(bind)该纹理对象。

并为多边形指定一些纹理坐标。

并在绘制多边形之前启用纹理。

像这样:
#include <GL/glut.h>

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

GLuint tex = 0;
void init()
{
    RGBType pixels[ 250*250 ];

    RGBType* temp = pixels;
    for (int x = 0; x < 250; x++)
    {
        for (int y = 0; y < 250; y++)
        {
            temp->r = 0;
            temp->g = 1;
            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, 250, 250, 0, GL_RGB, GL_FLOAT, NULL );
    glTexSubImage2D(GL_TEXTURE_2D,0,0,0,250,250,GL_RGB,GL_FLOAT,pixels);
}

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

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2, 2, -2, 2, -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(250,250);
    glutCreateWindow("Ray Tracer");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

关于c++ - 为什么glDrawPixels在这里不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20009471/

10-13 05:02