rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@1
|
3 #include <math.h>
|
nuclear@0
|
4 #include <GL/glew.h>
|
nuclear@0
|
5 #include <GL/glut.h>
|
nuclear@0
|
6 #include "sdr.h"
|
nuclear@1
|
7 #include "teapot_data.h"
|
nuclear@0
|
8
|
nuclear@0
|
9 void disp(void);
|
nuclear@2
|
10 void draw_teapot(float scale);
|
nuclear@1
|
11 void draw_teapot_patch(struct vec3 *bez_cp, int *index, int flip, float rot);
|
nuclear@0
|
12 void set_material(float dr, float dg, float db, float sr, float sg, float sb, float shin);
|
nuclear@0
|
13 void reshape(int x, int y);
|
nuclear@0
|
14 void keyb(unsigned char key, int x, int y);
|
nuclear@0
|
15 void mouse(int bn, int state, int x, int y);
|
nuclear@0
|
16 void motion(int x, int y);
|
nuclear@0
|
17
|
nuclear@0
|
18 static float cam_theta, cam_phi = 25;
|
nuclear@0
|
19 static float cam_dist = 8.0;
|
nuclear@0
|
20
|
nuclear@0
|
21 static unsigned int prog;
|
nuclear@0
|
22
|
nuclear@1
|
23 static int max_tess_level;
|
nuclear@1
|
24 static int tess_level = 5;
|
nuclear@1
|
25
|
nuclear@0
|
26 int main(int argc, char **argv)
|
nuclear@0
|
27 {
|
nuclear@2
|
28 float amb[] = {0.05, 0.05, 0.05, 0.0};
|
nuclear@2
|
29
|
nuclear@0
|
30 glutInit(&argc, argv);
|
nuclear@0
|
31 glutInitWindowSize(1280, 800);
|
nuclear@0
|
32 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
|
nuclear@0
|
33 glutCreateWindow("tesselation shaders test");
|
nuclear@0
|
34
|
nuclear@0
|
35 glutDisplayFunc(disp);
|
nuclear@0
|
36 glutReshapeFunc(reshape);
|
nuclear@0
|
37 glutKeyboardFunc(keyb);
|
nuclear@0
|
38 glutMouseFunc(mouse);
|
nuclear@0
|
39 glutMotionFunc(motion);
|
nuclear@2
|
40 glutIdleFunc(glutPostRedisplay);
|
nuclear@0
|
41
|
nuclear@0
|
42 glewInit();
|
nuclear@0
|
43
|
nuclear@2
|
44 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
|
nuclear@2
|
45 glClearColor(amb[0], amb[1], amb[2], 1);
|
nuclear@0
|
46
|
nuclear@0
|
47 glEnable(GL_CULL_FACE);
|
nuclear@1
|
48 glEnable(GL_DEPTH_TEST);
|
nuclear@0
|
49
|
nuclear@0
|
50 if(!GLEW_ARB_tessellation_shader) {
|
nuclear@0
|
51 fprintf(stderr, "your OpenGL implementation does not support tesselation shaders\n");
|
nuclear@0
|
52 return 1;
|
nuclear@0
|
53 }
|
nuclear@0
|
54 glGetIntegerv(GL_MAX_TESS_GEN_LEVEL, &max_tess_level);
|
nuclear@0
|
55 printf("maximum tesselation levels: %d\n", max_tess_level);
|
nuclear@0
|
56
|
nuclear@0
|
57 glPatchParameteri(GL_PATCH_VERTICES, 16);
|
nuclear@2
|
58 glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
|
nuclear@0
|
59
|
nuclear@0
|
60 {
|
nuclear@0
|
61 unsigned int vsdr, tcsdr, tesdr, psdr;
|
nuclear@0
|
62
|
nuclear@0
|
63 if(!(vsdr = load_vertex_shader("sdr/bezier.v.glsl"))) {
|
nuclear@0
|
64 return 1;
|
nuclear@0
|
65 }
|
nuclear@0
|
66 if(!(tcsdr = load_tessctl_shader("sdr/bezier.tc.glsl"))) {
|
nuclear@0
|
67 return 1;
|
nuclear@0
|
68 }
|
nuclear@0
|
69 if(!(tesdr = load_tesseval_shader("sdr/bezier.te.glsl"))) {
|
nuclear@0
|
70 return 1;
|
nuclear@0
|
71 }
|
nuclear@0
|
72 if(!(psdr = load_pixel_shader("sdr/bezier.p.glsl"))) {
|
nuclear@0
|
73 return 1;
|
nuclear@0
|
74 }
|
nuclear@0
|
75
|
nuclear@0
|
76 if(!(prog = create_program_link(vsdr, tcsdr, tesdr, psdr, 0))) {
|
nuclear@0
|
77 return 1;
|
nuclear@0
|
78 }
|
nuclear@0
|
79 }
|
nuclear@0
|
80
|
nuclear@0
|
81 glutMainLoop();
|
nuclear@0
|
82 return 0;
|
nuclear@0
|
83 }
|
nuclear@0
|
84
|
nuclear@0
|
85
|
nuclear@0
|
86 void disp(void)
|
nuclear@0
|
87 {
|
nuclear@0
|
88 float lpos[] = {-5, 10, 4, 1};
|
nuclear@0
|
89
|
nuclear@0
|
90 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
91
|
nuclear@0
|
92 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
93 glLoadIdentity();
|
nuclear@0
|
94 glTranslatef(0, 0, -cam_dist);
|
nuclear@0
|
95 glRotatef(cam_phi, 1, 0, 0);
|
nuclear@0
|
96 glRotatef(cam_theta, 0, 1, 0);
|
nuclear@0
|
97
|
nuclear@0
|
98 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
nuclear@0
|
99
|
nuclear@0
|
100 set_material(0.2, 0.35, 1.0, 1.0, 1.0, 1.0, 60.0);
|
nuclear@0
|
101
|
nuclear@2
|
102 draw_teapot(2.0);
|
nuclear@1
|
103
|
nuclear@1
|
104 glutSwapBuffers();
|
nuclear@1
|
105 }
|
nuclear@1
|
106
|
nuclear@2
|
107 void draw_teapot(float scale)
|
nuclear@1
|
108 {
|
nuclear@1
|
109 int i;
|
nuclear@1
|
110
|
nuclear@2
|
111 scale /= 2.0;
|
nuclear@2
|
112
|
nuclear@1
|
113 glPushMatrix();
|
nuclear@2
|
114 glTranslatef(0, -3.15 * scale * 0.5, 0);
|
nuclear@1
|
115 glRotatef(-90.0, 1, 0, 0);
|
nuclear@2
|
116 glScalef(scale, scale, scale);
|
nuclear@1
|
117
|
nuclear@0
|
118 glUseProgram(prog);
|
nuclear@1
|
119 set_uniform_int(prog, "tess_level", tess_level);
|
nuclear@1
|
120 set_uniform_float3(prog, "norm_scale", 1, 1, 1);
|
nuclear@0
|
121
|
nuclear@0
|
122 glBegin(GL_PATCHES);
|
nuclear@0
|
123
|
nuclear@1
|
124 /* first render the front-facing patches */
|
nuclear@1
|
125 for(i=0; i<NUM_TEAPOT_PATCHES; i++) {
|
nuclear@1
|
126 if(teapot_part_flip[i] > 0.0) {
|
nuclear@1
|
127 draw_teapot_patch(teapot_verts, teapot_index + i * 16, 0, teapot_part_rot[i]);
|
nuclear@1
|
128 }
|
nuclear@1
|
129 }
|
nuclear@1
|
130 glEnd();
|
nuclear@0
|
131
|
nuclear@1
|
132 set_uniform_float3(prog, "norm_scale", -1, -1, -1);
|
nuclear@0
|
133
|
nuclear@1
|
134 glFrontFace(GL_CW);
|
nuclear@1
|
135 glBegin(GL_PATCHES);
|
nuclear@1
|
136 /* then render the flipped ones */
|
nuclear@1
|
137 for(i=0; i<NUM_TEAPOT_PATCHES; i++) {
|
nuclear@1
|
138 if(teapot_part_flip[i] < 0.0) {
|
nuclear@1
|
139 draw_teapot_patch(teapot_verts, teapot_index + i * 16, 1, teapot_part_rot[i]);
|
nuclear@1
|
140 }
|
nuclear@1
|
141 }
|
nuclear@0
|
142 glEnd();
|
nuclear@1
|
143 glFrontFace(GL_CCW);
|
nuclear@0
|
144
|
nuclear@0
|
145 glUseProgram(0);
|
nuclear@0
|
146
|
nuclear@1
|
147 glPopMatrix();
|
nuclear@1
|
148 }
|
nuclear@1
|
149
|
nuclear@1
|
150 void draw_teapot_patch(struct vec3 *bez_cp, int *index, int flip, float rot)
|
nuclear@1
|
151 {
|
nuclear@1
|
152 int i;
|
nuclear@1
|
153 float cosr = cos(M_PI * rot / 180.0);
|
nuclear@1
|
154 float sinr = sin(M_PI * rot / 180.0);
|
nuclear@1
|
155
|
nuclear@1
|
156 for(i=0; i<16; i++) {
|
nuclear@1
|
157 struct vec3 cp = bez_cp[index[i]];
|
nuclear@1
|
158
|
nuclear@1
|
159 float x = cosr * cp.x + sinr * cp.y;
|
nuclear@1
|
160 float y = -sinr * cp.x + cosr * cp.y;
|
nuclear@1
|
161
|
nuclear@1
|
162 glVertex3f(x, flip ? -y : y, cp.z);
|
nuclear@1
|
163 }
|
nuclear@0
|
164 }
|
nuclear@0
|
165
|
nuclear@0
|
166 void set_material(float dr, float dg, float db, float sr, float sg, float sb, float shin)
|
nuclear@0
|
167 {
|
nuclear@0
|
168 float dcol[4], scol[4];
|
nuclear@0
|
169
|
nuclear@0
|
170 dcol[0] = dr;
|
nuclear@0
|
171 dcol[1] = dg;
|
nuclear@0
|
172 dcol[2] = db;
|
nuclear@0
|
173 dcol[3] = 1.0;
|
nuclear@0
|
174
|
nuclear@0
|
175 scol[0] = sr;
|
nuclear@0
|
176 scol[1] = sg;
|
nuclear@0
|
177 scol[2] = sb;
|
nuclear@0
|
178 scol[3] = 1.0;
|
nuclear@0
|
179
|
nuclear@0
|
180 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
|
nuclear@0
|
181 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
|
nuclear@0
|
182 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
|
nuclear@0
|
183 }
|
nuclear@0
|
184
|
nuclear@0
|
185 void reshape(int x, int y)
|
nuclear@0
|
186 {
|
nuclear@0
|
187 glViewport(0, 0, x, y);
|
nuclear@0
|
188 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
189 glLoadIdentity();
|
nuclear@0
|
190 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
|
nuclear@0
|
191 }
|
nuclear@0
|
192
|
nuclear@0
|
193 void keyb(unsigned char key, int x, int y)
|
nuclear@0
|
194 {
|
nuclear@0
|
195 switch(key) {
|
nuclear@0
|
196 case 27:
|
nuclear@0
|
197 exit(0);
|
nuclear@0
|
198
|
nuclear@0
|
199 case 'w':
|
nuclear@0
|
200 {
|
nuclear@0
|
201 static int wire;
|
nuclear@0
|
202 wire = !wire;
|
nuclear@0
|
203 if(wire) {
|
nuclear@0
|
204 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
nuclear@0
|
205 } else {
|
nuclear@0
|
206 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
nuclear@0
|
207 }
|
nuclear@0
|
208 }
|
nuclear@0
|
209 glutPostRedisplay();
|
nuclear@1
|
210
|
nuclear@1
|
211 case '=':
|
nuclear@1
|
212 if(tess_level < max_tess_level) {
|
nuclear@1
|
213 tess_level++;
|
nuclear@1
|
214 printf("tessellation level: %d\n", tess_level);
|
nuclear@1
|
215 set_uniform_int(prog, "tess_level", tess_level);
|
nuclear@1
|
216 glutPostRedisplay();
|
nuclear@1
|
217 }
|
nuclear@0
|
218 break;
|
nuclear@0
|
219
|
nuclear@1
|
220 case '-':
|
nuclear@1
|
221 if(tess_level > 1) {
|
nuclear@1
|
222 tess_level--;
|
nuclear@1
|
223 printf("tessellation level: %d\n", tess_level);
|
nuclear@1
|
224 set_uniform_int(prog, "tess_level", tess_level);
|
nuclear@1
|
225 glutPostRedisplay();
|
nuclear@1
|
226 }
|
nuclear@1
|
227 break;
|
nuclear@0
|
228 }
|
nuclear@0
|
229 }
|
nuclear@0
|
230
|
nuclear@0
|
231 static int bnstate[16];
|
nuclear@0
|
232 static int prev_x, prev_y;
|
nuclear@0
|
233
|
nuclear@0
|
234 void mouse(int bn, int state, int x, int y)
|
nuclear@0
|
235 {
|
nuclear@0
|
236 int idx = bn - GLUT_LEFT_BUTTON;
|
nuclear@0
|
237
|
nuclear@0
|
238 if(idx < sizeof bnstate / sizeof *bnstate) {
|
nuclear@0
|
239 bnstate[idx] = state == GLUT_DOWN;
|
nuclear@0
|
240 }
|
nuclear@0
|
241 prev_x = x;
|
nuclear@0
|
242 prev_y = y;
|
nuclear@0
|
243 }
|
nuclear@0
|
244
|
nuclear@0
|
245 void motion(int x, int y)
|
nuclear@0
|
246 {
|
nuclear@0
|
247 int dx = x - prev_x;
|
nuclear@0
|
248 int dy = y - prev_y;
|
nuclear@0
|
249 prev_x = x;
|
nuclear@0
|
250 prev_y = y;
|
nuclear@0
|
251
|
nuclear@0
|
252 if(bnstate[0]) {
|
nuclear@0
|
253 cam_theta += dx * 0.5;
|
nuclear@0
|
254 cam_phi += dy * 0.5;
|
nuclear@0
|
255 if(cam_phi < -90) cam_phi = -90;
|
nuclear@0
|
256 if(cam_phi > 90) cam_phi = 90;
|
nuclear@0
|
257 glutPostRedisplay();
|
nuclear@0
|
258 }
|
nuclear@0
|
259
|
nuclear@0
|
260 if(bnstate[2]) {
|
nuclear@0
|
261 cam_dist += dy * 0.1;
|
nuclear@0
|
262 glutPostRedisplay();
|
nuclear@0
|
263 }
|
nuclear@0
|
264 }
|