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