tesspot

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test.c	Sun Dec 02 08:23:51 2012 +0200
     1.3 @@ -0,0 +1,209 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <GL/glew.h>
     1.7 +#include <GL/glut.h>
     1.8 +#include "sdr.h"
     1.9 +
    1.10 +void disp(void);
    1.11 +void set_material(float dr, float dg, float db, float sr, float sg, float sb, float shin);
    1.12 +void reshape(int x, int y);
    1.13 +void keyb(unsigned char key, int x, int y);
    1.14 +void mouse(int bn, int state, int x, int y);
    1.15 +void motion(int x, int y);
    1.16 +
    1.17 +static float cam_theta, cam_phi = 25;
    1.18 +static float cam_dist = 8.0;
    1.19 +
    1.20 +static unsigned int prog;
    1.21 +
    1.22 +int main(int argc, char **argv)
    1.23 +{
    1.24 +	int max_tess_level;
    1.25 +
    1.26 +	glutInit(&argc, argv);
    1.27 +	glutInitWindowSize(1280, 800);
    1.28 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.29 +	glutCreateWindow("tesselation shaders test");
    1.30 +
    1.31 +	glutDisplayFunc(disp);
    1.32 +	glutReshapeFunc(reshape);
    1.33 +	glutKeyboardFunc(keyb);
    1.34 +	glutMouseFunc(mouse);
    1.35 +	glutMotionFunc(motion);
    1.36 +
    1.37 +	glewInit();
    1.38 +
    1.39 +	glClearColor(0.07, 0.07, 0.07, 1);
    1.40 +
    1.41 +	glEnable(GL_CULL_FACE);
    1.42 +
    1.43 +	if(!GLEW_ARB_tessellation_shader) {
    1.44 +		fprintf(stderr, "your OpenGL implementation does not support tesselation shaders\n");
    1.45 +		return 1;
    1.46 +	}
    1.47 +	glGetIntegerv(GL_MAX_TESS_GEN_LEVEL, &max_tess_level);
    1.48 +	printf("maximum tesselation levels: %d\n", max_tess_level);
    1.49 +
    1.50 +	glPatchParameteri(GL_PATCH_VERTICES, 16);
    1.51 +
    1.52 +	{
    1.53 +		unsigned int vsdr, tcsdr, tesdr, psdr;
    1.54 +
    1.55 +		if(!(vsdr = load_vertex_shader("sdr/bezier.v.glsl"))) {
    1.56 +			return 1;
    1.57 +		}
    1.58 +		if(!(tcsdr = load_tessctl_shader("sdr/bezier.tc.glsl"))) {
    1.59 +			return 1;
    1.60 +		}
    1.61 +		if(!(tesdr = load_tesseval_shader("sdr/bezier.te.glsl"))) {
    1.62 +			return 1;
    1.63 +		}
    1.64 +		if(!(psdr = load_pixel_shader("sdr/bezier.p.glsl"))) {
    1.65 +			return 1;
    1.66 +		}
    1.67 +
    1.68 +		if(!(prog = create_program_link(vsdr, tcsdr, tesdr, psdr, 0))) {
    1.69 +			return 1;
    1.70 +		}
    1.71 +	}
    1.72 +	set_uniform_int(prog, "tess_level", 1);
    1.73 +
    1.74 +	glutMainLoop();
    1.75 +	return 0;
    1.76 +}
    1.77 +
    1.78 +
    1.79 +void disp(void)
    1.80 +{
    1.81 +	float lpos[] = {-5, 10, 4, 1};
    1.82 +
    1.83 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.84 +
    1.85 +	glMatrixMode(GL_MODELVIEW);
    1.86 +	glLoadIdentity();
    1.87 +	glTranslatef(0, 0, -cam_dist);
    1.88 +	glRotatef(cam_phi, 1, 0, 0);
    1.89 +	glRotatef(cam_theta, 0, 1, 0);
    1.90 +
    1.91 +	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    1.92 +
    1.93 +	set_material(0.2, 0.35, 1.0, 1.0, 1.0, 1.0, 60.0);
    1.94 +
    1.95 +	glUseProgram(prog);
    1.96 +
    1.97 +	glBegin(GL_PATCHES);
    1.98 +	glVertex3f(-1, -0.8, 1);
    1.99 +	glVertex3f(-0.25, -1, 1);
   1.100 +	glVertex3f(0.25, -1, 1);
   1.101 +	glVertex3f(1, 0, 1);
   1.102 +
   1.103 +	glVertex3f(-1, -0.4, 0.25);
   1.104 +	glVertex3f(-0.25, 0, 0.25);
   1.105 +	glVertex3f(0.25, 0, 0.25);
   1.106 +	glVertex3f(1, -0.4, 0.25);
   1.107 +
   1.108 +	glVertex3f(-1, -0.4, -0.25);
   1.109 +	glVertex3f(-0.25, 0.6, -0.25);
   1.110 +	glVertex3f(0.25, 0.3, -0.25);
   1.111 +	glVertex3f(1, -0.4, -0.25);
   1.112 +
   1.113 +	glVertex3f(-1, 0, -1);
   1.114 +	glVertex3f(-0.25, 0.2, -1);
   1.115 +	glVertex3f(0.25, 0.2, -1);
   1.116 +	glVertex3f(1, 0, -1);
   1.117 +	glEnd();
   1.118 +
   1.119 +	glUseProgram(0);
   1.120 +
   1.121 +	glutSwapBuffers();
   1.122 +}
   1.123 +
   1.124 +void set_material(float dr, float dg, float db, float sr, float sg, float sb, float shin)
   1.125 +{
   1.126 +	float dcol[4], scol[4];
   1.127 +
   1.128 +	dcol[0] = dr;
   1.129 +	dcol[1] = dg;
   1.130 +	dcol[2] = db;
   1.131 +	dcol[3] = 1.0;
   1.132 +
   1.133 +	scol[0] = sr;
   1.134 +	scol[1] = sg;
   1.135 +	scol[2] = sb;
   1.136 +	scol[3] = 1.0;
   1.137 +
   1.138 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
   1.139 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
   1.140 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
   1.141 +}
   1.142 +
   1.143 +void reshape(int x, int y)
   1.144 +{
   1.145 +	glViewport(0, 0, x, y);
   1.146 +	glMatrixMode(GL_PROJECTION);
   1.147 +	glLoadIdentity();
   1.148 +	gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
   1.149 +}
   1.150 +
   1.151 +void keyb(unsigned char key, int x, int y)
   1.152 +{
   1.153 +	switch(key) {
   1.154 +	case 27:
   1.155 +		exit(0);
   1.156 +
   1.157 +	case 'w':
   1.158 +		{
   1.159 +			static int wire;
   1.160 +			wire = !wire;
   1.161 +			if(wire) {
   1.162 +				glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
   1.163 +			} else {
   1.164 +				glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
   1.165 +			}
   1.166 +		}
   1.167 +		glutPostRedisplay();
   1.168 +		break;
   1.169 +	}
   1.170 +
   1.171 +	if(key >= '1' && key <= '9') {
   1.172 +		int tess_level = key - '0';
   1.173 +		set_uniform_int(prog, "tess_level", tess_level);
   1.174 +
   1.175 +		glutPostRedisplay();
   1.176 +	}
   1.177 +}
   1.178 +
   1.179 +static int bnstate[16];
   1.180 +static int prev_x, prev_y;
   1.181 +
   1.182 +void mouse(int bn, int state, int x, int y)
   1.183 +{
   1.184 +	int idx = bn - GLUT_LEFT_BUTTON;
   1.185 +
   1.186 +	if(idx < sizeof bnstate / sizeof *bnstate) {
   1.187 +		bnstate[idx] = state == GLUT_DOWN;
   1.188 +	}
   1.189 +	prev_x = x;
   1.190 +	prev_y = y;
   1.191 +}
   1.192 +
   1.193 +void motion(int x, int y)
   1.194 +{
   1.195 +	int dx = x - prev_x;
   1.196 +	int dy = y - prev_y;
   1.197 +	prev_x = x;
   1.198 +	prev_y = y;
   1.199 +
   1.200 +	if(bnstate[0]) {
   1.201 +		cam_theta += dx * 0.5;
   1.202 +		cam_phi += dy * 0.5;
   1.203 +		if(cam_phi < -90) cam_phi = -90;
   1.204 +		if(cam_phi > 90) cam_phi = 90;
   1.205 +		glutPostRedisplay();
   1.206 +	}
   1.207 +
   1.208 +	if(bnstate[2]) {
   1.209 +		cam_dist += dy * 0.1;
   1.210 +		glutPostRedisplay();
   1.211 +	}
   1.212 +}