OpenGL设计小屋场景

Grading Scheme

Your assignment will be graded by the following marking scheme:
Basic (80%)

  • Planes (the left, right, and back walls, the ceiling and the floor) 10%
  • At least five different geometric primitives 20%
  • At least five keyboard events (mouse event is optional) 15%
  • Object transformation animation (rotation, translating, scaling) 15%
  • General lighting control (different material properties setting) 20%

Bonus (Up to 20%)

  • Well-organized room 5%
  • Complex meaningful object constructed by different primitives 5%
  • Additional light (with different properties, on/off or transformation) 10%
  • Other creativities 10%

Total 100%
Note: No grade will be given if the program is incomplete.

代码框架解读

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdlib.h>
#include <GL/glut.h>
const GLdouble FRUSTDIM = 100.0f;

void init(void) // All Setup For OpenGL Goes Here
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}

void display(void) // Here's Where We Do All The Drawing
{
// TODO:
// Place light source here

// TODO:
// Draw walls and objects here

// TODO:
// Add animation here

glutSwapBuffers();
glFlush();

}


void reshape(int w, int h) // Resize the GL Window. w=width, h=height
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-FRUSTDIM, FRUSTDIM, -FRUSTDIM, FRUSTDIM, 320., 640.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y) // Handle the keyboard events here
{
switch (key)
{
case'\033'://press 'esc' to quit
exit(0);
break;

// TODO:
// Add keyboard control here

}
}

void idle()
{

}


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

/*Initialization of GLUT Library */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);

/*Create a window with title specified */
glutCreateWindow("Assignment 1");
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);

init(); /*not GLUT call, initialize several parameters */

/*Register different CALLBACK function for GLUT to response
with different events, e.g. window sizing, mouse click or
keyboard stroke */
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);

/*Enter the GLUT event processing loop which never returns.
it will call different registered CALLBACK according
to different events. */
glutMainLoop();

}

修改和补充

作业框架中的一处bug:main函数中glutCreateWindow应当在glutInitWindowSizeglutInitWindowPosition之后。

在init函数中添加glEnable(GL_NORMALIZE);glEnable(GL_NORMALIZE)是使opengl自动normalize 法向量,之所以需要自动normalize 法向量是因为法向量会受到glScalef的影响,当glScalef将物体的坐标放大k倍时,法向量变为1/k,因为opengl自己的光照计算要求法向量单位化,所以不重新单位化,计算结果就是错误的。
举个例子就是如果不添加glEnable(GL_NORMALIZE);display函数中放大一个几何物体它的亮度就会变暗。

display函数中添加glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);用于清除缓冲区。

display函数中所有代码外套一个glPushMatrix();glPopMatrix();保证每次回调时的矩阵栈为空。

程序一:动画效果演示

源码

mark

程序二:3D room scene

源码

mark

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. Grading Scheme
  2. 2. 代码框架解读
  3. 3. 修改和补充
  4. 4. 程序一:动画效果演示
  5. 5. 程序二:3D room scene