c++ - 为什么 glDrawPixels 在这里不起作用?

标签 c++ opengl pixel trace gldrawpixels

这是代码。输出是一个灰色的方 block ——一直都是,不管输入是什么,而且明显是错误的。我的目标是能够将所有像素存储在某个地方并显示它们,这样我就可以继续使用简单的光线追踪器,但我似乎无法弄清楚这个 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 channel 。

不要对 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/

相关文章:

java - 在Java中查找一行上的所有像素

ios - 在 iOS 上设置独立于设备的 RGB

C++ 让 boost 线程等待 1 秒

c - glutMouseFunc 鼠标事件处理程序导致函数在 openGL 中无缘无故被再次调用

Java 鼠标在屏幕上的位置

C# - 转换 8 位或 16 位灰度原始像素数据

c++ - 受 void* 影响的属性函数样式约定

C++ 在派生类中重载模板函数

c++ - 指向删除并指向其他对象时,C++中的Seg Faulting Pointers

c++ - 如何创建匹配当前投影矩阵的 glClipPlanes?