gpt4 book ai didi

c++ - 如何在 OpenGL 中叠加文本

转载 作者:太空狗 更新时间:2023-10-29 23:05:18 25 4
gpt4 key购买 nike

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

最佳答案

您忘记禁用照明:

/***    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 != '\0'; 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 != '\0'; 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++ - 如何在 OpenGL 中叠加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20082576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com