1

基本上我有一个基本的形状,它由要操作的键盘控制。而且我还有想要在屏幕上呈现的文本,上面写着“Hello world”。但是,每当我运行它时,我都会得到一个空白屏幕。

这是我的代码,有什么想法吗?

#include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <string.h>
#include <GL/glut.h>
#include <math.h>

GLfloat g_xeye;
GLfloat g_yeye;

bool rollOn;

GLfloat g_light_position[] = {0.0, 10.0, 10.0, 0.0};

void roll(void)
{
    if (rollOn){
        Sleep(1);
        g_xeye = g_xeye + 00000.1;
        glutPostRedisplay();
        g_yeye = g_yeye + 00000.1;
        glutPostRedisplay();
    }
}

void drawBitmapText(char *string, float x, float y, float z)
{
    char *c;
    glRasterPos3f(x, y, z);

    for (c=string; *c != '\0'; c++)
    {
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, *c);
    }
}

void drawStrokeText(char*string, int x, int y, int z)
{
    char *c;
    glPushMatrix();
    glTranslatef(x, y+8, z);

    for (c=string; *c != '\0'; c++)
    {
        glutStrokeCharacter(GLUT_STROKE_ROMAN, *c);
    }
    glPopMatrix();
}

void display(void)
{
    GLfloat redDiffuseMaterial[] = {0.50, 0.0, 0.50, 0.0};

    glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    gluLookAt(0,0, 6 , 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, redDiffuseMaterial);
    //glPushMatrix();
    glRotatef(g_xeye, 1, 0, 0);
    glRotatef(g_yeye, 0, 1, 0);
    glutSolidCube(2.0);
    //glPopMatrix();

    //glColor3f(0.250, 0.0, 0.200);

    glClear(GL_COLOR_BUFFER_BIT); 
    glLoadIdentity();

    glColor3f(0,1,0);

    drawBitmapText("Hello world",200,200,0);

    glutSwapBuffers ();
}

void keyboard(unsigned char key, int x, int y)
{
    if (key==27)
        exit(0);
    if (key == ',')
    {

        g_light_position[0] = g_light_position[0] - 1;
        glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);

    }
    if (key == '.')
    {
        g_light_position[0] = g_light_position[0] + 1;
        glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);
        glutPostRedisplay();
    }
    if (key==32)
    {
    if(rollOn==false)
        rollOn=true;
    else
        rollOn=false;
    }
}

void special(int key, int x, int y)
{
    if (key==27)
        exit(0);
    if (key == GLUT_KEY_UP)
    {
        g_xeye = g_xeye + 2;
        glutPostRedisplay();
    }
    if (key == GLUT_KEY_DOWN)
    {
        g_xeye = g_xeye - 2;
        glutPostRedisplay();
    }
    if (key == GLUT_KEY_RIGHT)
    {
        g_yeye = g_yeye + 2;
        glutPostRedisplay();
    }
    if (key == GLUT_KEY_LEFT)
    {
        g_yeye = g_yeye - 2;
        glutPostRedisplay();
    }
}

void init(void)
{
    glEnable(GL_DEPTH_TEST);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);

    rollOn = false;  

    g_xeye = 0;
    g_yeye = 0;
}

void reshape (int width, int height)
{
    /* Called when window is created, moved or resized */
    GLfloat aspectRatio;

    /* Set the viewport to be the whole window */
    glViewport(0, 0, (GLsizei) width, (GLsizei) height);

    /* Prepare to set up the Projection matrix */
    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();
    //glColor3f(0.250, 0.0, 0.200);

    gluOrtho2D(0, width, height, 0);


    /* Set a prespective projection */
    aspectRatio = (GLfloat)width / (GLfloat) height;
    gluPerspective(60, aspectRatio, 1.0, 100.0);

    /* Prepare to set up the Model view matrix */
    glMatrixMode(GL_MODELVIEW);
    glClearColor (0.0, 0.0, 0.0, 0.0);

    glLoadIdentity();
}

/*void render(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    glColor3f(0,1,0);

    drawBitmapText("Hello world",200,200,0);
    glutSwapBuffers();
}*/

int main(int argc, char** argv)
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow ("Project");
    init();


    glutKeyboardFunc(keyboard);
    glutSpecialFunc(special);


    glutReshapeFunc(reshape);
    glutDisplayFunc(display);

    glutIdleFunc(roll);



    glutMainLoop();
    return 0;
}

4

1 回答 1

1

试试这个:

#include <GL/glut.h>

GLfloat g_xeye;
GLfloat g_yeye;

bool rollOn;

GLfloat g_light_position[] = {0.0, 10.0, 10.0, 0.0};

void timer( int extra )
{
    if (rollOn){
        g_xeye = g_xeye + 00000.1;
        g_yeye = g_yeye + 00000.1;
    }
    glutTimerFunc( 16, timer, 0 );
    glutPostRedisplay();
}

void drawBitmapText(char *string, float x, float y, float z)
{
    char *c;
    glRasterPos3f(x, y, z);

    for (c=string; *c != '\0'; c++)
    {
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, *c);
    }
}

void display(void)
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

    // draw cube in 3D
    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();
    gluLookAt(0, 0, 6, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    glPushMatrix();
    glRotatef(g_xeye, 1, 0, 0);
    glRotatef(g_yeye, 0, 1, 0);
    GLfloat redDiffuseMaterial[] = {0.50, 0.0, 0.50, 0.0};
    glMaterialfv(GL_FRONT, GL_DIFFUSE, redDiffuseMaterial);
    glutSolidCube(2.0);
    glPopMatrix();

    // switch to 2D for text overlay
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho( 0, w, 0, h, -1, 1 );

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glDisable( GL_LIGHTING );
    glColor3ub(255,255,255);
    drawBitmapText("Hello world",200,200,0);
    glEnable( GL_LIGHTING );

    glutSwapBuffers ();
}

void keyboard(unsigned char key, int x, int y)
{
    if (key==27)
        exit(0);
    if (key == ',')
    {
        g_light_position[0] = g_light_position[0] - 1;
    }
    if (key == '.')
    {
        g_light_position[0] = g_light_position[0] + 1;
        glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);
    }
    if (key==32)
    {
        rollOn = !rollOn;
    }
}

void special(int key, int x, int y)
{
    if (key==27)
        exit(0);
    if (key == GLUT_KEY_UP)
    {
        g_xeye = g_xeye + 2;
    }
    if (key == GLUT_KEY_DOWN)
    {
        g_xeye = g_xeye - 2;
    }
    if (key == GLUT_KEY_RIGHT)
    {
        g_yeye = g_yeye + 2;
    }
    if (key == GLUT_KEY_LEFT)
    {
        g_yeye = g_yeye - 2;
    }
}

void init(void)
{
    glEnable(GL_DEPTH_TEST);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);

    rollOn = false;  

    g_xeye = 0;
    g_yeye = 0;
}

int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow ("Project");
    init();

    glutKeyboardFunc(keyboard);
    glutSpecialFunc(special);
    glutDisplayFunc(display);
    glutTimerFunc( 0, timer, 0 );
    glutMainLoop();
    return 0;
}

如果您愿意使用glWindowPos()OpenGL 1.4而不是glRasterPos().

于 2013-03-29T03:21:49.777 回答