如何在OpenGL中覆盖文本
How to overlay text in OpenGL
我目前有一个程序,显示多个旋转的立方体接近近平面。我想做的一件事是在屏幕的左下角显示一些文本。每当我尝试实现文本时,我的原始代码都不会显示。相反,我有一个只有文本的黑色屏幕。我想我可能在推和弹出矩阵时遇到了一些问题,但我一直没能弄清楚我在哪里搞砸了。我的文本在第158行被drawString函数调用。
drawString("Why aren't the cubes being displayed?");
如果注释掉了这一点,我的原始代码就会完美地执行,旋转的立方体不断地飞向屏幕。
以下是我的全部代码。
#include <windows.h>
#include <gl/glut.h>
#include <gl/GL.h>
#include <ctime>
#include <math.h>
#define PI 3.14159265
#define CUBES 15
GLfloat angle[CUBES] = {0.0};
GLfloat fovy = 60.0;
GLfloat zNear = 0.0;
GLfloat zFar = 100.0;
GLfloat transX[CUBES]; //array for translated positions
GLfloat transY[CUBES];
GLfloat transZ[CUBES];
GLfloat red[CUBES]; //keep track of randomized colors
GLfloat blue[CUBES];
GLfloat green[CUBES];
GLfloat spin[CUBES]; //spin speeds for each cube
GLfloat ambred = 1.0; //ambient light color variables
GLfloat ambgreen = 1.0;
GLfloat ambblue = 1.0;
GLfloat lx = -10.0; //light position variables
GLfloat ly = 0.0;
GLfloat lz = 1.0;
GLfloat lw = 0.0;
/*** drawing text on screen ***/ //not working yet
void drawString(char *string){
glMatrixMode(GL_PROJECTION);
glPushMatrix(); //save
glLoadIdentity(); //and clear
gluOrtho2D(0, 1024, 0, 720);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(1, 0, 0); // red font
glDisable( GL_DEPTH_TEST ); //disable depth test so renders on top
glRasterPos2i(10, 10);
void *font = GLUT_BITMAP_HELVETICA_18;
for (char* c=string; *c != ' '; c++) {
glutBitmapCharacter(font, *c);
}
glEnable (GL_DEPTH_TEST); //turn depth test back on
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
/*** Creates random spin for blocks ***/
void initializeSpin(){
for(int i=0;i<CUBES;i++){
spin[i]= (float)rand()/((float)RAND_MAX/2.5);
}
}
/*** Creates Random Location on Far Plane ***/
void reset(GLfloat &x, GLfloat &y, GLfloat &z) {
x = rand() % 101 - 50;
y = rand() % 101 - 50;
z = -95.0;
}
/*** Create Random Colors ***/
void initializeColor(GLfloat &x, GLfloat &y, GLfloat &z) {
x = (float)((rand()%10)*0.1);
y = (float)((rand()%10)*0.1);
z = (float)((rand()%10)*0.1);
}
/*** Create the Cube ***/
void cube (int n) {
GLfloat calcThetaY;
GLfloat calcThetaX;
calcThetaY = abs (atan(transY[n]/transZ[n]) * 180 / PI); //thetas to determine if within viewing volume
calcThetaX = abs (atan(transX[n]/transZ[n]) * 180 / PI);
if((calcThetaY > (fovy/2)+10) || (calcThetaX > (fovy/2)+10) || transZ[n] > zNear+10){ //added +10 to clear perspective
reset(transX[n], transY[n], transZ[n]); //randomize location
initializeColor(red[n], green[n], blue[n]); //randomize color
spin[n]= (float)rand()/((float)RAND_MAX/2.5); //randomize spin
}
glPushMatrix();
glTranslatef(transX[n], transY[n], transZ[n]);
glRotatef(angle[n], 1.0, 0.0, 0.0); //rotate on the x axis
glRotatef(angle[n], 0.0, 1.0, 0.0); //rotate on the y axis
glRotatef(angle[n], 0.0, 0.0, 1.0); //rotate on the z axis
glColor3f(red[n], green[n], blue[n]); //color of cube
glutSolidCube(2); //draw the cube
glPopMatrix();
}
void myInit (void) {
glEnable(GL_COLOR_MATERIAL);
glEnable (GL_DEPTH_TEST); //enable the depth testing
glEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
glEnable (GL_LIGHT1); //enable LIGHT1, our Ambient Light
glShadeModel (GL_SMOOTH); //set the shader to smooth shader
for(int i=0;i<CUBES;i++){
reset(transX[i], transY[i], transZ[i]);
}
for(int i=0;i<CUBES;i++){
initializeColor(red[i], green[i], blue[i]);
}
initializeSpin();
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0); //black background
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color buffer and the depth buffer
glLoadIdentity();
GLfloat AmbientLight[] = {ambred, ambgreen, ambblue}; //set AmbientLight[]
glLightfv (GL_LIGHT1, GL_AMBIENT, AmbientLight); //change the light accordingly
GLfloat LightPosition[] = {lx, ly, lz, lw}; //set LightPosition
glLightfv (GL_LIGHT0, GL_POSITION, LightPosition); //change LightPosition
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //camera position, x,y,z, looking at x,y,z, Up Positions of the camera
for(int j=0;j<CUBES;j++){
cube(j);
angle[j]+= spin[j]; //speed of rotation
transZ[j]+=0.25; //speed of travel through Z-axis
}
drawString("Why aren't the cubes being displayed?");
glutSwapBuffers();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); //set the perspective (angle of sight, width, height, , depth)
glMatrixMode (GL_MODELVIEW);
}
void quitProgram (unsigned char key, int x, int y) {
if(key == 113) exit(0);
}
int main (int argc, char **argv) {
srand(time(NULL));
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); //set the display to Double buffer, with depth
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow ("boxes2");
myInit();
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc(reshape);
glutKeyboardFunc(quitProgram);
glutMainLoop();
return 0;
}
您忘记禁用照明:
/*** drawing text on screen ***/
void drawString(char *string)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
int w = glutGet( GLUT_WINDOW_WIDTH );
int h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, w, 0, h, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable( GL_DEPTH_TEST );
glDisable( GL_LIGHTING );
glColor3f(1, 0, 0);
glRasterPos2i(20, 20);
void *font = GLUT_BITMAP_HELVETICA_18;
for (char* c=string; *c != ' '; c++)
{
glutBitmapCharacter(font, *c);
}
glEnable( GL_LIGHTING );
glEnable (GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
上下文:
#include <GL/glut.h>
#include <ctime>
#include <cmath>
#define PI 3.14159265
#define CUBES 15
GLfloat angle[CUBES] = {0.0};
GLfloat fovy = 60.0;
GLfloat zNear = 0.0;
GLfloat zFar = 100.0;
GLfloat transX[CUBES];
GLfloat transY[CUBES];
GLfloat transZ[CUBES];
GLfloat red[CUBES];
GLfloat blue[CUBES];
GLfloat green[CUBES];
GLfloat spin[CUBES];
GLfloat ambred = 1.0;
GLfloat ambgreen = 1.0;
GLfloat ambblue = 1.0;
GLfloat lx = -10.0;
GLfloat ly = 0.0;
GLfloat lz = 1.0;
GLfloat lw = 0.0;
/*** drawing text on screen ***/
void drawString(char *string)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
int w = glutGet( GLUT_WINDOW_WIDTH );
int h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, w, 0, h, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable( GL_DEPTH_TEST );
glDisable( GL_LIGHTING );
glColor3f(1, 0, 0);
glRasterPos2i(20, 20);
void *font = GLUT_BITMAP_HELVETICA_18;
for (char* c=string; *c != ' '; c++)
{
glutBitmapCharacter(font, *c);
}
glEnable( GL_LIGHTING );
glEnable (GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
/*** Creates random spin for blocks ***/
void initializeSpin()
{
for(int i=0;i<CUBES;i++)
{
spin[i]= (float)rand()/((float)RAND_MAX/2.5);
}
}
/*** Creates Random Location on Far Plane ***/
void reset(GLfloat &x, GLfloat &y, GLfloat &z)
{
x = rand() % 101 - 50;
y = rand() % 101 - 50;
z = -95.0;
}
/*** Create Random Colors ***/
void initializeColor(GLfloat &x, GLfloat &y, GLfloat &z)
{
x = (float)((rand()%10)*0.1);
y = (float)((rand()%10)*0.1);
z = (float)((rand()%10)*0.1);
}
/*** Create the Cube ***/
void cube (int n)
{
GLfloat calcThetaY;
GLfloat calcThetaX;
calcThetaY = abs (atan(transY[n]/transZ[n]) * 180 / PI);
calcThetaX = abs (atan(transX[n]/transZ[n]) * 180 / PI);
if((calcThetaY > (fovy/2)+10) || (calcThetaX > (fovy/2)+10) || transZ[n] > zNear+10)
{
reset(transX[n], transY[n], transZ[n]);
initializeColor(red[n], green[n], blue[n]);
spin[n]= (float)rand()/((float)RAND_MAX/2.5);
}
glPushMatrix();
glTranslatef(transX[n], transY[n], transZ[n]);
glRotatef(angle[n], 1.0, 0.0, 0.0);
glRotatef(angle[n], 0.0, 1.0, 0.0);
glRotatef(angle[n], 0.0, 0.0, 1.0);
glColor3f(red[n], green[n], blue[n]);
glutSolidCube(2);
glPopMatrix();
}
void myInit (void)
{
glEnable(GL_COLOR_MATERIAL);
glEnable (GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable (GL_LIGHT1);
glShadeModel (GL_SMOOTH);
for(int i=0;i<CUBES;i++)
{
reset(transX[i], transY[i], transZ[i]);
}
for(int i=0;i<CUBES;i++)
{
initializeColor(red[i], green[i], blue[i]);
}
initializeSpin();
}
void display (void)
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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();
GLfloat AmbientLight[] = {ambred, ambgreen, ambblue};
glLightfv (GL_LIGHT1, GL_AMBIENT, AmbientLight);
GLfloat LightPosition[] = {lx, ly, lz, lw};
glLightfv (GL_LIGHT0, GL_POSITION, LightPosition);
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
for(int j=0;j<CUBES;j++)
{
cube(j);
angle[j]+= spin[j];
transZ[j]+=0.25;
}
drawString("Why aren't the cubes being displayed?");
glutSwapBuffers();
}
void quitProgram (unsigned char key, int x, int y)
{
if(key == 113) exit(0);
}
int main (int argc, char **argv)
{
srand(time(NULL));
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow ("boxes2");
myInit();
glutDisplayFunc (display);
glutIdleFunc (display);
glutKeyboardFunc(quitProgram);
glutMainLoop();
return 0;
}
相关文章:
- 文本文件中的单词链表
- 如何在 c++ 中覆盖相同的文本文件
- 如何让控制台输出覆盖控制台而不是纯文本输出?
- 在图像上覆盖文本的程序会产生无休止的字符串错误
- 覆盖现有的文本文件C
- 使用fstream在文件中覆盖一些文本,然后删除文件的其余部分
- 在文本文件中覆盖和追加行(C++)
- 尝试在文本文件中添加结构元素而不覆盖它
- 使用c++覆盖文件中的文本
- 如何在OpenGL中覆盖文本
- 是否可以在不覆盖输入中的当前文本的情况下进行 cout
- 如何防止用 ofstream 覆盖文本文件中的信息
- 为QTreeView覆盖QStyledItemDelegate中的文本
- 当文本文件再次打开时,C++会覆盖它
- 双重缓冲AlphaBlend矩形与文本覆盖
- 覆盖写入控制台的文本
- 如何重写/覆盖文本文件中的字符串/行
- 如何输出到控制台,并在不清除屏幕和重写所有内容的情况下覆盖文本
- 如何将字符串添加到现有的文本文件而不覆盖它
- 命令行界面 - C++/CLi - 使用流编写器写入文本文件会覆盖已有的内容