gpt4 book ai didi

c++ - 使用 glDrawPixels 显示像素图

转载 作者:搜寻专家 更新时间:2023-10-31 01:21:42 24 4
gpt4 key购买 nike

我已经使用 OpenGL 和 GLUT 编写了一个小示例程序,以使用 glDrawPixels 函数显示由四个彩色方 block 组成的 2×2 网格。不幸的是,我发现:

  1. 网格中的颜色显示不正确;和
  2. 当向 glPixelZoom 函数传递负参数以旋转像素图时,窗口中不显示任何内容。

以下 C++ 代码片段显示示例图像。我在这里做错了什么,我应该更改什么才能查看预期的颜色并旋转像素图?


struct RGB
{
unsigned char r, g, b;
};

class Pixmap
{
public:

RGB color[4];

Pixmap()
{
color[0].r = 255;
color[0].g = 0;
color[0].b = 0;

color[1].r = 0;
color[1].g = 255;
color[1].b = 0;

color[2].r = 0;
color[2].g = 0;
color[2].b = 255;

color[3].r = 255;
color[3].g = 255;
color[3].b = 255;
}

void render()
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels( 2, 2, GL_RGB, GL_UNSIGNED_BYTE, color );
glFlush();
}
};

// Create an instance of class Pixmap
Pixmap myPixmap;

void myRender()
{
myPixmap.render();
}

int main( int argc, char *argv[] )
{
int screenWidth, screenHeight;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

screenWidth = glutGet(GLUT_SCREEN_WIDTH);
screenHeight = glutGet(GLUT_SCREEN_HEIGHT);

int windowWidth = screenHeight / 2;
int windowHeight = screenHeight / 2;
glutInitWindowSize(windowWidth, windowHeight);

int posX = (screenWidth - windowWidth) / 2;
int posY = (screenHeight - windowHeight) / 4;

glutInitWindowPosition(posX, posY);
glutCreateWindow("Picture");

GLfloat scaleX = 1.0f * windowWidth / 2;
GLfloat scaleY = 1.0f * windowHeight / 2;
glMatrixMode( GL_PROJECTION );

// If glPixelZoom(-scaleX, scaleY)
// then no image is displayed

glPixelZoom(scaleX, scaleY);

glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);

glutDisplayFunc( myRender );
glutMainLoop();
}




最佳答案

经过一些实验,我认为我找到了一个似乎适合我自己目的的解决方案。首先,给出我上面问题中的示例代码,似乎需要将像素图传递给 glDrawPixels用作数组 GLubyte color[2][2][4]带有 alpha channel 。正如 Vaayu 所提到的,glMatrixMode 也有可能功能需要正确设置。其次,根据 pg。 OpenGL Programming Guide (7th Edition)的356,如果glPixelZoom需要设置当前光栅位置使用负参数调用函数。本质上,为什么我看不到图像是由于光栅绘制在不在窗口可视区域内的位置。以下代码演示了如何为四种不同的情况设置旋转和当前光栅位置:


// case 0
glWindowPos2i(0, 0);
glPixelZoom(scaleX, scaleY);<p></p>

<p>// case 1
glWindowPos2i(windowHeight, 0);
glPixelZoom(-scaleX, scaleY);</p>

<p>// case 2
glWindowPos2i(0, windowHeight);
glPixelZoom(scaleX, -scaleY);</p>

<p>// case 3
glWindowPos2i(windowWidth, windowHeight);
glPixelZoom(-scaleX, -scaleY);
</p>

所以可以更新我原来问题中给出的例子:

class Pixmap{public:    GLubyte color[2][2][4];    Pixmap()    {        // red        color[0][0][0] = 255;        color[0][0][1] = 0;        color[0][0][2] = 0;        color[0][0][3] = 0;        // green        color[0][1][0] = 0;        color[0][1][1] = 255;        color[0][1][2] = 0;        color[0][1][3] = 0;        // blue        color[1][0][0] = 0;        color[1][0][1] = 0;        color[1][0][2] = 255;        color[1][0][3] = 0;        // white        color[1][1][0] = 255;        color[1][1][1] = 255;        color[1][1][2] = 255;        color[1][1][3] = 0;    }    void render()    {        glClear(GL_COLOR_BUFFER_BIT);        glDrawPixels( 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, color );        glFlush();    }};// Create an instance of class PixmapPixmap myPixmap;void myRender(){    myPixmap.render();}int main( int argc, char *argv[] ){    int screenWidth, screenHeight;    int rotation;    // Choose rotation case {0, 1, 2, 3}    rotation = 3;    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);    screenWidth = glutGet(GLUT_SCREEN_WIDTH);    screenHeight = glutGet(GLUT_SCREEN_HEIGHT);    int windowWidth = screenHeight / 2;    int windowHeight = screenHeight / 2;    glutInitWindowSize(windowWidth, windowHeight);    int posX = (screenWidth - windowWidth) / 2;    int posY = (screenHeight - windowHeight) / 4;    glutInitWindowPosition(posX, posY);    glutCreateWindow("Picture");    GLfloat scaleX = 1.0f * windowWidth / 2;    GLfloat scaleY = 1.0f * windowHeight / 2;    glMatrixMode(GL_MODELVIEW);    // Select rotation    switch(rotation)    {    case 0:        glWindowPos2i(0, 0);        glPixelZoom(scaleX, scaleY);        break;    case 1:        glWindowPos2i(windowHeight, 0);        glPixelZoom(-scaleX, scaleY);        break;    case 2:        glWindowPos2i(0, windowHeight);        glPixelZoom(scaleX, -scaleY);        break;    case 3:        glWindowPos2i(windowWidth, windowHeight);        glPixelZoom(-scaleX, -scaleY);        break;    default:    break;    }    glClearColor(1.0, 1.0, 1.0, 0.0);    glColor3f(0.0f, 0.0f, 0.0f);    glutDisplayFunc( myRender );    glutMainLoop();}

关于c++ - 使用 glDrawPixels 显示像素图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3593283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com