OpenGL在屏幕上显示对象

时间:2016-06-05 13:16:54

标签: c++ opengl

我刚开始学习OpenGL,所以我在屏幕上显示对象时遇到问题。

Display()包含此对象,我想通过glutDisplayFunc(Display);显示此对象,但它不起作用,编译后我只有灰色屏幕。 (是的,这个对象是轮子)

#include "stdafx.h"
#include <math.h>
#include <GL/freeglut.h>

GLint Width = 1024, Height = 640;

const int CubeSize = 500;


void Display (void) {
    glClearColor (0.7, 0.7, 0.7, 1);
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3ub (255, 200, 200);


    int iTimeElapsed = glutGet(GLUT_ELAPSED_TIME);
    float fRevolveScale1 = 0.2f;
    float fRevolveScale2 = 0.4f;
    long i;

    // clear all pixels
    glClear(GL_COLOR_BUFFER_BIT);

    // push temp state
    glPushMatrix();

    // translate to center
    glTranslatef(0.5f, 0.5f, 0.0);

    // rotate around pivot
    glRotatef(iTimeElapsed * fRevolveScale1, 0.0, 0.0, 1.0);

    // translate to planet location
    glTranslatef(0.25f, 0.25f, 0.0);

    glRotatef(iTimeElapsed * fRevolveScale2, 0.0, 0.0, 1.0);

    glColor3f(0.129412f, 0.129412f, 0.129412f);
    glutSolidSphere(0.1f, 16, 16);

    // five bolts, step 72 degree (72*5=360 degree)
    glColor3f(1.0, 1.0, 1.0);
    for (i = 0; i<5; ++i)
    {
        glPushMatrix();
        glRotatef(72.0f*i, 0.0, 0.0, 1.0); // rotate coordinate 72 degree
        glTranslatef(0.04f, 0.0, 0.0);// translate on the rotated coordinate
        glutSolidSphere(0.01f, 16, 16);
        glPopMatrix();
    }

    glTranslatef(0.0f, 0.0f, 0.0f);// translate on the rotated coordinate
    glutSolidSphere(0.01, 16, 16);

    // pop temp state
    glPopMatrix();

    glFlush();
    glutPostRedisplay();

    glFinish ();
}



void Reshape(GLint w, GLint h) {
    Width = w, Height = h;
    glViewport (0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity ();
    glOrtho (0, w, 0, h, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity ();
}


void Keyboard(unsigned char key, int x, int y) {
    const char ESCAPE = '\033 ';
    if (key == ESCAPE) exit (0);
}

void main(int argc, char *argv []) {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_RGB);
    glutInitWindowSize (Width, Height);
    glutCreateWindow("E X A M P L E");
    glutDisplayFunc(Display);
    glutReshapeFunc(Reshape);
    glutKeyboardFunc(Keyboard);
    glutMainLoop ();
}

0 个答案:

没有答案