【发布时间】:2011-04-05 07:55:51
【问题描述】:
我使用 OpenGL 和 GLUT 编写了一个小示例程序,以使用 glDrawPixels 函数显示由四个彩色方块组成的 2×2 网格。不幸的是,我发现:
- 网格中的颜色显示不正确;和
- 当向 glPixelZoom 函数传递负参数以旋转像素图时,窗口中不会显示任何内容。
以下 C++ 代码 sn-p 显示示例图像。我在这里做错了什么,我应该改变什么才能查看预期的颜色和旋转像素图?
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();
}
【问题讨论】: