如何在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 != '\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;
}
Genpfault

您忘记了禁用照明:

/***    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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在html中的叠加层中添加文字?

来自分类Dev

如何在横幅上叠加文字

来自分类Dev

如何在图像上叠加文字

来自分类Dev

如何在WPF中叠加图像?

来自分类Dev

如何在活动中创建叠加视图

来自分类Dev

如何在活动中创建叠加视图

来自分类Dev

如何在RelativeLayout中叠加视图?

来自分类Dev

如何在jQuery中创建叠加形状

来自分类Dev

如何在 CSS 中添加图像叠加

来自分类Dev

如何在Android中结合叠加位图和捕获的图像?

来自分类Dev

如何在Windows中隐藏Firefox相机图标叠加层

来自分类Dev

如何在视频代码中添加按钮叠加层

来自分类Dev

如何在WPF中的图像上叠加图像?

来自分类Dev

如何在MKMapView Swift中添加叠加路径

来自分类Dev

如何在Apple Apple Watch UI中叠加项目

来自分类Dev

如何在Android布局中创建叠加按钮

来自分类Dev

如何在抖动中在图像上叠加图标

来自分类Dev

如何在Flutter应用程序中叠加元素?

来自分类Dev

如何在tkinter中的Canvas上使用叠加Vkeyboard?

来自分类Dev

如何在Apple Apple Watch UI中叠加项目

来自分类Dev

如何在视频标签中添加按钮叠加层

来自分类Dev

如何在父div中叠加多个子div?

来自分类Dev

定位如何在 cloudinary 中的叠加图像上工作?

来自分类Dev

如何在 Android 中创建“始终在顶部叠加层”

来自分类Dev

如何在OpenGL中绘制视锥

来自分类Dev

如何在OpenGL中构造glutSolidTorus的纹理?

来自分类Dev

如何在OpenGL中反转纹理颜色

来自分类Dev

如何在OpenGL中停止拉伸

来自分类Dev

如何在OpenGL中处理纹理动画?