我已经使用 OpenGL 和 GLUT 编写了一个小示例程序,以使用 glDrawPixels
函数显示一个由四个彩色方块组成的 2×2 网格。不幸的是,我发现:
以下 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();
}
最佳答案
经过一些实验,我认为我找到了一个似乎适合我自己目的的解决方案。首先,根据我上面问题中的示例代码,似乎需要将像素图作为具有 alpha channel 的数组 glDrawPixels
传递给 GLubyte color[2][2][4]
函数。正如Vaayu提到的,还有可能需要正确设置glMatrixMode
函数。其次,根据 pg。 356 of the OpenGL Programming Guide (7th Edition),如果使用负参数调用 glPixelZoom
函数,则需要设置当前光栅位置。本质上,我看不到图像的原因是光栅绘制在不在窗口可视区域内的位置。以下代码演示了如何为四种不同的情况设置旋转和当前光栅位置:
// case 0
glWindowPos2i(0, 0);
glPixelZoom(scaleX, scaleY);
// case 1
glWindowPos2i(windowHeight, 0);
glPixelZoom(-scaleX, scaleY);
// case 2
glWindowPos2i(0, windowHeight);
glPixelZoom(scaleX, -scaleY);
// case 3
glWindowPos2i(windowWidth, windowHeight);
glPixelZoom(-scaleX, -scaleY);
所以我原来的问题中给出的例子可以更新:
类像素图
{
上市:
GLubyte 颜色[2][2][4];
像素图()
{
//红色的
颜色[0][0][0] = 255;
颜色[0][0][1] = 0;
颜色[0][0][2] = 0;
颜色[0][0][3] = 0;
//绿色
颜色[0][1][0] = 0;
颜色[0][1][1] = 255;
颜色[0][1][2] = 0;
颜色[0][1][3] = 0;
//蓝色的
颜色[1][0][0] = 0;
颜色[1][0][1] = 0;
颜色[1][0][2] = 255;
颜色[1][0][3] = 0;
//白色的
颜色[1][1][0] = 255;
颜色[1][1][1] = 255;
颜色[1][1][2] = 255;
颜色[1][1][3] = 0;
}
无效渲染()
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(2, 2, GL_RGBA, GL_UNSIGNED_BYTE, 颜色);
glFlush();
}
};
//创建 Pixmap 类的实例
像素图 myPixmap;
无效我的渲染()
{
myPixmap.render();
}
int main( int argc, char *argv[] )
{
int screenWidth, screenHeight;
整数轮换;
//选择旋转情况 {0, 1, 2, 3}
旋转 = 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 = 屏幕高度/2;
glutInitWindowSize(windowWidth, windowHeight);
int posX = (screenWidth - windowWidth)/2;
int posY = (screenHeight - windowHeight)/4;
glutInitWindowPosition(posX, posY);
glutCreateWindow("图片");
GLfloat scaleX = 1.0f * windowWidth/2;
GLfloat scaleY = 1.0f * windowHeight/2;
glMatrixMode(GL_MODELVIEW);
//选择旋转
开关(旋转)
{
案例0:
glWindowPos2i(0, 0);
glPixelZoom(scaleX, scaleY);
休息;
情况1:
glWindowPos2i(windowHeight, 0);
glPixelZoom(-scaleX, scaleY);
休息;
案例2:
glWindowPos2i(0, windowHeight);
glPixelZoom(scaleX, -scaleY);
休息;
案例3:
glWindowPos2i(windowWidth, windowHeight);
glPixelZoom(-scaleX, -scaleY);
休息;
默认:
休息;
}
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/