我想在屏幕上写任何东西。绘制矩形很容易。我试图将文本字符串横向叠加在“书”上。看起来很简单但是......
正如笔记一样,我试图从这些论坛中获取建议,因此可能不需要glDisable(GL_DEPTH_TEST);
和glDisable(GL_LIGHTING);
。另外,有没有办法自己调试?我通常喜欢添加cout << "i reached this far yay" << endl;
,但是当我将它放在void Bookshelf()
时它似乎不喜欢我...它给了我一个C2381错误('function' : redefinition; __declspec(noreturn) differs
)
的main.cpp
#include "bookshelf.h"
void Initialize()
{
glClearColor (1.0, 1.0, 1.0, 0.0 );
glMatrixMode( GL_PROJECTION );
glOrtho(0,899,899,0,1,0);
}
void main( int argc, char *argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( 900, 900 );
glutInitWindowPosition( 510, 100 );
glutCreateWindow("Bookshelf - Ankit Ahuja");
Initialize();
glutDisplayFunc(Bookshelf);
glutMainLoop();
}
bookshelf.h
#include <GL/glut.h>
void Bookshelf()
{
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 0.0, 0.0, 0.0 );
glBegin( GL_QUADS );
//Code that draws a bookshelf
//Book1
glColor3f( 1.0, 0.25, 0.25 );
glVertex3i(240,70,0.5);
glVertex3i(260,70,0.5);
glVertex3i(260,180,0.5);
glVertex3i(240,180,0.5);
//BookTitle1
glPushMatrix();
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glRotatef(90f,0.0f,0.0f,0.0f);
glColor3f( 0.0, 1.0, 0.0 );
glDisable(GL_LIGHTING);
glRasterPos3i(100,100,1);
char text[50]="Alphabet Soup is the best guys";
for(int i=0; i<50; i++)
{
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,(int)text[i]);
}
glPopMatrix();
//Book2
glColor3f( 0.8, 0.8, 0.0 );
glVertex2i(270,70);
glVertex2i(290,70);
glVertex2i(290,180);
glVertex2i(270,180);
//More books and shelves
glEnd();
glFlush();
}
答案 0 :(得分:1)
在显示文字之前,您应使用glOrtho
设置正投影。
确保在编写文本之前正确推送/弹出投影和模型视图矩阵,以便查看类似的内容:
glMatrixMode(GL_PROJECTION); //Select projection matrix
glPushMatrix(); //save it
glLoadIdentity();
glMatrixMode(GL_MODELVIEW); //Select modelview matrix
glPushMatrix(); //save it
glLoadIdentity();
// set up ur glOrtho
glOrtho(...);
glutBitmapCharacter(...)
glMatrixMode(GL_PROJECTION);
glPopMatrix(); //Restore your old projection matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix(); //Restore old modelview matrix