[实例]OpenGL绘制茶壶(光照、三维变换)

时间:2022-02-16 06:56:58

这里仅仅提供一个实例程序,如果对opengl的相关知识了解不多,可以看看我的其他博客专题 (ฅ• . •ฅ)。

程序目标:

绘制一个茶壶,用“+、-”键来旋转茶壶,同时实现光照。

#include <windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>

double angle = 0.0;

void initialize()
{
glClearColor(1.0,1.0,1.0,0.0);
//设置光照
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

}

static void resize(int width, int height)
{
//指定视口
glViewport(0, 0, width, height);

//投影变换---平截头体
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)width/(GLdouble)height,0.1,10000.0);
}

static void display(void)
{
//视图变换,指定相机
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0, 0.0,0.0,0.0, 0.0,1.0,0.0);

//模型变换
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);

glPushMatrix();
glRotatef(angle,0.0,1.0,0.0);
glutSolidTeapot(1.0);
glPopMatrix();

glutSwapBuffers();
}


static void key(unsigned char key, int x, int y)
{
switch (key)
{
case 27 :
case 'q':
exit(0);
break;

case '+':
angle++;
break;

case '-':
angle--;
break;
}

glutPostRedisplay();
}



/* Program entry point */

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("GLUT Shapes");

initialize();

glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);

glutMainLoop();

return EXIT_SUCCESS;
}
程序的执行结果如下:

[实例]OpenGL绘制茶壶(光照、三维变换)