我目前有一个程序显示多个旋转立方体接近近平面。我想做的一件事是在屏幕的左下角显示一些文本。每当我尝试实现文本时,我的原始代码都不会显示。相反,我有一个只有文本的黑屏。我想我在推送和弹出我的矩阵时可能会遇到一些问题,但我无法弄清楚我在哪里搞砸了。在第 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 != '\0'; 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;
}