使用glNormal3f法向量绘制立方体
Posted ambercctv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用glNormal3f法向量绘制立方体相关的知识,希望对你有一定的参考价值。
#if 0
#include <GLFW/glut.h>GLfloat light_diffuse[] = 1.0, 0.0, 0.0, 1.0 ; /* Red diffuse light. */
GLfloat light_position[] = 0.0, 0.0, 5.0, 0.0 ; /* Infinite light location. */GLuint cl1;
GLuint idleCounter = 0;
void
list1(void)
glBegin(GL_QUADS);
glNormal3f(-1, 0, 0);
glVertex3f(-1, -1, 1);
glVertex3f(-1, -1, -1);
glVertex3f(-1, 1, -1);
glVertex3f(-1, 1, 1);glNormal3f(0, 1, 0);
glVertex3f(-1, 1, 1);
glVertex3f(-1, 1, -1);
glVertex3f(1, 1, -1);
glVertex3f(1, 1, 1);glNormal3f(1, 0, 0);
glVertex3f(1, 1, 1);
glVertex3f(1, 1, -1);
glVertex3f(1, -1, -1);
glVertex3f(1, -1, 1);
//4glNormal3f(0, -1, 0);
glVertex3f(1, -1, 1);
glVertex3f(1, -1, -1);
glVertex3f(-1, -1, -1);
glVertex3f(-1, -1, 1);
//5 +zglNormal3f(0, 0, 0);
glVertex3f(-1, -1, 1);
glVertex3f(-1, 1, 1);
glVertex3f(1, 1, 1);
glVertex3f(1, -1, 1);
//6 -z
glNormal3f(1, 0, 0);
glVertex3f(-1, -1, -1);
glVertex3f(-1, 1, -1);
glVertex3f(1, 1, -1);
glVertex3f(1, -1, -1);
glEnd();glFlush();
glutSwapBuffers();
void display(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glCallList(cl1);
list1();
void idle(void)
if ((idleCounter++ % 0x10000) == 0)
glRotatef(0.1, 0.0, 0.0, 1.0);
glutPostRedisplay();
void reshape(void)
void
init(void)
/* Enable a single OpenGL light. */
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);/* Use depth buffering for hidden surface elimination. */
glEnable(GL_DEPTH_TEST);
cl1 = glGenLists(1);
glNewList(cl1, GL_COMPILE);
list1();
glEndList();/* Setup the view of the cube. */
glMatrixMode(GL_PROJECTION);
gluPerspective( /* field of view in degree */ 40.0,
/* aspect ratio */ 1.0,
/* Z near */ 1.0, /* Z far */ 10.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0, 0.0, -10.0, /* eye is at (0,0,5) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 1.0, 0.0); /* up is in positive Y direction */#if 0
gluLookAt(-10, 0.0, 0.0, /* eye is at (0,0,5) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 0.0, 1.0); /* up is in positive Y direction */
GLfloat light_position[] = -5.0, 0.0, 0.0, 0.0 ;
#endif/* Adjust cube position to be asthetic angle. */
//glTranslatef(0.0, 0.0, -1.0);
//glRotatef(60, 1.0, 0.0, 0.0);
//glRotatef(-20, 0.0, 0.0, 1.0);
int
main(int argc, char **argv)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("3D lighted cube");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
init();
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
#endif
以上是关于使用glNormal3f法向量绘制立方体的主要内容,如果未能解决你的问题,请参考以下文章