为什么这个四边形没有渲染?或者,如果是为什么我不能看到它?

时间:2012-11-17 22:12:02

标签: opengl glut visual-c++-2010

我正在使用Visual C ++和GLUT。我错过了这样的法线吗?这是我正在使用的确切代码。我试图插入glNormal3f(0,0,1);在顶点语句之前但这并没有改变任何东西。 glutSolidSphere就像我期望的那样呈现,但不是四边形。

#include "stdafx.h"
#include <stdlib.h>
#include <GL/glut.h>


float spin = 0.0f;
float zDir = 0;
GLfloat diffuseIntensity[] = {.75, .75, .75, 1};
GLfloat specularHue[] = {0, 0, .5f, 1};
GLfloat shininess[] = {5};


void moveCamera();
void checkKeys(int, int, int);
void setUpLighting();
void changeSize(int w, int h) {


    if (h == 0)
        h = 1;

    float ratio =  w * 1.0 / h;


    glMatrixMode(GL_PROJECTION);    
    glLoadIdentity();   
    glViewport(0, 0, w, h); 
    gluPerspective(45,ratio,1,1000);    
    glMatrixMode(GL_MODELVIEW);
}

void renderScene(void) {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    moveCamera();
    glTranslatef(0, 0, zDir-5);
    glEnable(GL_COLOR_MATERIAL);
    glShadeModel(GL_FLAT);
    glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specularHue);
    glPushMatrix();
    glRotatef(90, 1, 0, 0);
    glRotatef(spin, 0, 0, 1);
    glutSolidSphere(.5f, 24, 24);
    glPopMatrix();
    spin += .01f;
    if(spin > 360) spin -= 360;

    glBegin(GL_QUADS);
    glVertex3f(-10, 0, -10);
    glVertex3f(-10, 0, 10);
    glVertex3f(10, 0, 10);
    glVertex3f(10, 0, -10);
    glEnd();
    glutSwapBuffers();
    glutPostRedisplay();
}

int main(int argc, char **argv) {


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(800,600);
    glutCreateWindow("Lighthouse3D - GLUT Tutorial");


    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutSpecialFunc(checkKeys);
    glEnable(GL_DEPTH_TEST);
    setUpLighting();

    glutMainLoop();

    return 1;
}
void moveCamera(){


    glTranslatef(0, 0, zDir);

}

void checkKeys(int key, int x, int y){
    switch(key){

    case GLUT_KEY_UP:
        zDir += .1f;
        glutPostRedisplay();
        break;

    case GLUT_KEY_DOWN:
        zDir += -.1f;
        glutPostRedisplay();
        break;

    case GLUT_KEY_END:
        exit(0);
    }
}

void setUpLighting(){
    GLfloat position0[] = {3, 1, 0, 1};
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, position0);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseIntensity);
}

2 个答案:

答案 0 :(得分:1)

试一试:

#include <GL/glut.h>

float zDir = 12;
void checkKeys(int key, int x, int y)
{
    switch(key)
    {
    case GLUT_KEY_UP:
        zDir += -.5f;
        glutPostRedisplay();
        break;

    case GLUT_KEY_DOWN:
        zDir += .5f;
        glutPostRedisplay();
        break;

    case GLUT_KEY_END:
        exit(0);
    }
}

float spin = 0;
void renderScene(void)
{
    glEnable(GL_DEPTH_TEST);

    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(45,w/h,0.1,100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef(0, 0, -zDir);

    // set up light
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    GLfloat diffuseIntensity[] = {.75, .75, .75, 1};
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseIntensity);

    // draw sphere at light position
    glDisable( GL_LIGHTING );
    glPushMatrix();
    // spin light position around the y axis
    glRotatef( -spin, 0, 1, 0 );
    GLfloat position0[] = {3,3,3, 1};
    glLightfv(GL_LIGHT0, GL_POSITION, position0);
    glTranslatef( position0[0], position0[1], position0[2] );
    glColor3ub(255,255,255);
    glutSolidSphere(0.1,8,8);
    glPopMatrix();
    glEnable( GL_LIGHTING );

    // draw sphere
    glEnable(GL_COLOR_MATERIAL);
    glShadeModel(GL_FLAT);
    GLfloat specularHue[] = {0, 0, .5f, 1};
    GLfloat shininess[] = {5};
    glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specularHue);

    spin += .01f;
    if(spin > 360) spin -= 360;

    glPushMatrix();
    glRotatef(spin, 0, 1, 0);
    glutSolidSphere(2, 24, 24);
    glPopMatrix();

    // draw quad
    glColor3ub(255,0,0);
    glPushMatrix();
    glScalef( 3, 3, 3 );
    glBegin(GL_QUADS);
    glNormal3f( 0, 0, 1 );
    glVertex2f( -1, -1 );
    glVertex2f(  1, -1 );
    glVertex2f(  1,  1 );
    glVertex2f( -1,  1 );
    glEnd();
    glPopMatrix();

    glutSwapBuffers();

    glutPostRedisplay();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(800,600);
    glutCreateWindow("Lighthouse3D - GLUT Tutorial");

    glutDisplayFunc(renderScene);
    glutSpecialFunc(checkKeys);
    glutMainLoop();
    return 1;
}

答案 1 :(得分:0)

渲染四边形,但你没有看到它,因为它的绘制非常像桌子的表面而你的眼睛处于桌子的同一层。因此,效果就像没有任何东西被画出来一样。那或者你会看到一条线。

上面的旋转,包裹在PushMatrix / PopMatrix中仅适用于实心球体。