tesspot

view src/test.c @ 0:72b7f9f2eead

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Dec 2012 08:23:51 +0200
parents
children befe01bbd27f
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <GL/glew.h>
4 #include <GL/glut.h>
5 #include "sdr.h"
7 void disp(void);
8 void set_material(float dr, float dg, float db, float sr, float sg, float sb, float shin);
9 void reshape(int x, int y);
10 void keyb(unsigned char key, int x, int y);
11 void mouse(int bn, int state, int x, int y);
12 void motion(int x, int y);
14 static float cam_theta, cam_phi = 25;
15 static float cam_dist = 8.0;
17 static unsigned int prog;
19 int main(int argc, char **argv)
20 {
21 int max_tess_level;
23 glutInit(&argc, argv);
24 glutInitWindowSize(1280, 800);
25 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
26 glutCreateWindow("tesselation shaders test");
28 glutDisplayFunc(disp);
29 glutReshapeFunc(reshape);
30 glutKeyboardFunc(keyb);
31 glutMouseFunc(mouse);
32 glutMotionFunc(motion);
34 glewInit();
36 glClearColor(0.07, 0.07, 0.07, 1);
38 glEnable(GL_CULL_FACE);
40 if(!GLEW_ARB_tessellation_shader) {
41 fprintf(stderr, "your OpenGL implementation does not support tesselation shaders\n");
42 return 1;
43 }
44 glGetIntegerv(GL_MAX_TESS_GEN_LEVEL, &max_tess_level);
45 printf("maximum tesselation levels: %d\n", max_tess_level);
47 glPatchParameteri(GL_PATCH_VERTICES, 16);
49 {
50 unsigned int vsdr, tcsdr, tesdr, psdr;
52 if(!(vsdr = load_vertex_shader("sdr/bezier.v.glsl"))) {
53 return 1;
54 }
55 if(!(tcsdr = load_tessctl_shader("sdr/bezier.tc.glsl"))) {
56 return 1;
57 }
58 if(!(tesdr = load_tesseval_shader("sdr/bezier.te.glsl"))) {
59 return 1;
60 }
61 if(!(psdr = load_pixel_shader("sdr/bezier.p.glsl"))) {
62 return 1;
63 }
65 if(!(prog = create_program_link(vsdr, tcsdr, tesdr, psdr, 0))) {
66 return 1;
67 }
68 }
69 set_uniform_int(prog, "tess_level", 1);
71 glutMainLoop();
72 return 0;
73 }
76 void disp(void)
77 {
78 float lpos[] = {-5, 10, 4, 1};
80 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
82 glMatrixMode(GL_MODELVIEW);
83 glLoadIdentity();
84 glTranslatef(0, 0, -cam_dist);
85 glRotatef(cam_phi, 1, 0, 0);
86 glRotatef(cam_theta, 0, 1, 0);
88 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
90 set_material(0.2, 0.35, 1.0, 1.0, 1.0, 1.0, 60.0);
92 glUseProgram(prog);
94 glBegin(GL_PATCHES);
95 glVertex3f(-1, -0.8, 1);
96 glVertex3f(-0.25, -1, 1);
97 glVertex3f(0.25, -1, 1);
98 glVertex3f(1, 0, 1);
100 glVertex3f(-1, -0.4, 0.25);
101 glVertex3f(-0.25, 0, 0.25);
102 glVertex3f(0.25, 0, 0.25);
103 glVertex3f(1, -0.4, 0.25);
105 glVertex3f(-1, -0.4, -0.25);
106 glVertex3f(-0.25, 0.6, -0.25);
107 glVertex3f(0.25, 0.3, -0.25);
108 glVertex3f(1, -0.4, -0.25);
110 glVertex3f(-1, 0, -1);
111 glVertex3f(-0.25, 0.2, -1);
112 glVertex3f(0.25, 0.2, -1);
113 glVertex3f(1, 0, -1);
114 glEnd();
116 glUseProgram(0);
118 glutSwapBuffers();
119 }
121 void set_material(float dr, float dg, float db, float sr, float sg, float sb, float shin)
122 {
123 float dcol[4], scol[4];
125 dcol[0] = dr;
126 dcol[1] = dg;
127 dcol[2] = db;
128 dcol[3] = 1.0;
130 scol[0] = sr;
131 scol[1] = sg;
132 scol[2] = sb;
133 scol[3] = 1.0;
135 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
136 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
137 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
138 }
140 void reshape(int x, int y)
141 {
142 glViewport(0, 0, x, y);
143 glMatrixMode(GL_PROJECTION);
144 glLoadIdentity();
145 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
146 }
148 void keyb(unsigned char key, int x, int y)
149 {
150 switch(key) {
151 case 27:
152 exit(0);
154 case 'w':
155 {
156 static int wire;
157 wire = !wire;
158 if(wire) {
159 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
160 } else {
161 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
162 }
163 }
164 glutPostRedisplay();
165 break;
166 }
168 if(key >= '1' && key <= '9') {
169 int tess_level = key - '0';
170 set_uniform_int(prog, "tess_level", tess_level);
172 glutPostRedisplay();
173 }
174 }
176 static int bnstate[16];
177 static int prev_x, prev_y;
179 void mouse(int bn, int state, int x, int y)
180 {
181 int idx = bn - GLUT_LEFT_BUTTON;
183 if(idx < sizeof bnstate / sizeof *bnstate) {
184 bnstate[idx] = state == GLUT_DOWN;
185 }
186 prev_x = x;
187 prev_y = y;
188 }
190 void motion(int x, int y)
191 {
192 int dx = x - prev_x;
193 int dy = y - prev_y;
194 prev_x = x;
195 prev_y = y;
197 if(bnstate[0]) {
198 cam_theta += dx * 0.5;
199 cam_phi += dy * 0.5;
200 if(cam_phi < -90) cam_phi = -90;
201 if(cam_phi > 90) cam_phi = 90;
202 glutPostRedisplay();
203 }
205 if(bnstate[2]) {
206 cam_dist += dy * 0.1;
207 glutPostRedisplay();
208 }
209 }