opengl 显示形状和绘制文本
Posted
技术标签:
【中文标题】opengl 显示形状和绘制文本【英文标题】:opengl display shape and draw text 【发布时间】:2013-03-28 23:55:23 【问题描述】:基本上我有一个由键盘控制的基本形状。而且我还有想要在屏幕上呈现的文本,上面写着“Hello world”。但是,每当我运行它时,我都会得到一个空白屏幕。
这是我的代码,有什么想法吗?
#include#include #include #include #include #include GLfloat g_xeye; GLfloat g_yeye; 布尔滚动; GLfloat g_light_position[] = 0.0, 10.0, 10.0, 0.0; 无效卷(无效) 如果(滚动) 睡眠(1); g_xeye = g_xeye + 00000.1; 过剩后重新显示(); g_yeye = g_yeye + 00000.1; 过剩后重新显示(); void drawBitmapText(char *string, float x, float y, float z) 字符 *c; glRasterPos3f(x, y, z); for (c=string; *c != '\0'; c++) glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, *c); 无效drawStrokeText(字符*字符串,int x,int y,int z) 字符 *c; glPushMatrix(); glTranslatef(x, y+8, z); for (c=string; *c != '\0'; c++) glutStrokeCharacter(GLUT_STROKE_ROMAN, *c); glPopMatrix(); 无效显示(无效) GLfloat redDiffuseMaterial[] = 0.50, 0.0, 0.50, 0.0; glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(0,0, 6, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glMaterialfv(GL_FRONT, GL_DIFFUSE, redDiffuseMaterial); //glPushMatrix(); glRotatef(g_xeye, 1, 0, 0); glRotatef(g_yeye, 0, 1, 0); glutSolidCube(2.0); //glPopMatrix(); //glColor3f(0.250, 0.0, 0.200); glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glColor3f(0,1,0); drawBitmapText("Hello world",200,200,0); glutSwapBuffers (); 无效键盘(无符号字符键,int x,int y) 如果(键==27) 退出(0); 如果(键==',') g_light_position[0] = g_light_position[0] - 1; glLightfv(GL_LIGHT0, GL_POSITION, g_light_position); if (key == '.') g_light_position[0] = g_light_position[0] + 1; glLightfv(GL_LIGHT0, GL_POSITION, g_light_position); 过剩后重新显示(); 如果(键==32) 如果(滚动==假) 滚动=真; 别的 滚动=假; 无效特殊(int键,int x,int y) 如果(键==27) 退出(0); 如果(键 == GLUT_KEY_UP) g_xeye = g_xeye + 2; 过剩后重新显示(); 如果(键 == GLUT_KEY_DOWN) g_xeye = g_xeye - 2; 过剩后重新显示(); 如果(键 == GLUT_KEY_RIGHT) g_yeye = g_yeye + 2; 过剩后重新显示(); 如果(键 == GLUT_KEY_LEFT) g_yeye = g_yeye - 2; 过剩后重新显示(); 无效初始化(无效) glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, g_light_position); 滚动=假; g_xeye = 0; g_yeye = 0; void reshape (int width, int height) /* 在创建、移动或调整窗口大小时调用 */ GLfloat aspectRatio; /* 设置视口为整个窗口 */ glViewport(0, 0, (GLsizei) 宽度, (GLsizei) 高度); /* 准备设置投影矩阵 */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); //glColor3f(0.250, 0.0, 0.200); gluOrtho2D(0, 宽度, 高度, 0); /* 设置一个透视投影 */ aspectRatio = (GLfloat)width / (GLfloat) 高度; gluPerspective(60, aspectRatio, 1.0, 100.0); /* 准备建立模型视图矩阵 */ glMatrixMode(GL_MODELVIEW); glClearColor (0.0, 0.0, 0.0, 0.0); glLoadIdentity(); /* 无效渲染(无效) glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glColor3f(0,1,0); drawBitmapText("Hello world",200,200,0); glutSwapBuffers(); */ int main(int argc, char** argv) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500,500); glutInitWindowPosition(100, 100); glutCreateWindow(“项目”); 在里面(); glutKeyboardFunc(键盘); glutSpecialFunc(特殊); glutReshapeFunc(重塑); 过剩显示函数(显示); glutIdleFunc(滚动); 过剩主循环(); 返回0;
【问题讨论】:
好吧,对于初学者来说,您的投影转换有点令人困惑。在reshape
中,您同时调用gluOrtho2D
和gluPerspective
。通常只使用其中一种,具体取决于您需要正交投影还是透视投影。
另外,您的 gluPerspective()
调用似乎将近剪裁平面设置为 z=1.0,但您在 z=0.0 处绘制文本,因此文本将被剪裁。
你的glutInit()
呢?这不是可选的。
【参考方案1】:
试试这个:
#include <GL/glut.h>
GLfloat g_xeye;
GLfloat g_yeye;
bool rollOn;
GLfloat g_light_position[] = 0.0, 10.0, 10.0, 0.0;
void timer( int extra )
if (rollOn)
g_xeye = g_xeye + 00000.1;
g_yeye = g_yeye + 00000.1;
glutTimerFunc( 16, timer, 0 );
glutPostRedisplay();
void drawBitmapText(char *string, float x, float y, float z)
char *c;
glRasterPos3f(x, y, z);
for (c=string; *c != '\0'; c++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, *c);
void display(void)
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
// draw cube in 3D
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective(60, w / h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 6, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glRotatef(g_xeye, 1, 0, 0);
glRotatef(g_yeye, 0, 1, 0);
GLfloat redDiffuseMaterial[] = 0.50, 0.0, 0.50, 0.0;
glMaterialfv(GL_FRONT, GL_DIFFUSE, redDiffuseMaterial);
glutSolidCube(2.0);
glPopMatrix();
// switch to 2D for text overlay
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0, w, 0, h, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable( GL_LIGHTING );
glColor3ub(255,255,255);
drawBitmapText("Hello world",200,200,0);
glEnable( GL_LIGHTING );
glutSwapBuffers ();
void keyboard(unsigned char key, int x, int y)
if (key==27)
exit(0);
if (key == ',')
g_light_position[0] = g_light_position[0] - 1;
if (key == '.')
g_light_position[0] = g_light_position[0] + 1;
glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);
if (key==32)
rollOn = !rollOn;
void special(int key, int x, int y)
if (key==27)
exit(0);
if (key == GLUT_KEY_UP)
g_xeye = g_xeye + 2;
if (key == GLUT_KEY_DOWN)
g_xeye = g_xeye - 2;
if (key == GLUT_KEY_RIGHT)
g_yeye = g_yeye + 2;
if (key == GLUT_KEY_LEFT)
g_yeye = g_yeye - 2;
void init(void)
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);
rollOn = false;
g_xeye = 0;
g_yeye = 0;
int main(int argc, char** argv)
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(100, 100);
glutCreateWindow ("Project");
init();
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutDisplayFunc(display);
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 0;
如果您愿意使用OpenGL 1.4 中的glWindowPos()
而不是glRasterPos()
,则可以避免2D 切换。
【讨论】:
以上是关于opengl 显示形状和绘制文本的主要内容,如果未能解决你的问题,请参考以下文章