tesspot

diff src/test.c @ 1:befe01bbd27f

tessellated the teapot
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Dec 2012 17:16:32 +0200
parents 72b7f9f2eead
children 178a9e3c3c8c
line diff
     1.1 --- a/src/test.c	Sun Dec 02 08:23:51 2012 +0200
     1.2 +++ b/src/test.c	Sun Dec 02 17:16:32 2012 +0200
     1.3 @@ -1,10 +1,14 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6 +#include <math.h>
     1.7  #include <GL/glew.h>
     1.8  #include <GL/glut.h>
     1.9  #include "sdr.h"
    1.10 +#include "teapot_data.h"
    1.11  
    1.12  void disp(void);
    1.13 +void draw_teapot(void);
    1.14 +void draw_teapot_patch(struct vec3 *bez_cp, int *index, int flip, float rot);
    1.15  void set_material(float dr, float dg, float db, float sr, float sg, float sb, float shin);
    1.16  void reshape(int x, int y);
    1.17  void keyb(unsigned char key, int x, int y);
    1.18 @@ -16,10 +20,11 @@
    1.19  
    1.20  static unsigned int prog;
    1.21  
    1.22 +static int max_tess_level;
    1.23 +static int tess_level = 5;
    1.24 +
    1.25  int main(int argc, char **argv)
    1.26  {
    1.27 -	int max_tess_level;
    1.28 -
    1.29  	glutInit(&argc, argv);
    1.30  	glutInitWindowSize(1280, 800);
    1.31  	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.32 @@ -36,6 +41,7 @@
    1.33  	glClearColor(0.07, 0.07, 0.07, 1);
    1.34  
    1.35  	glEnable(GL_CULL_FACE);
    1.36 +	glEnable(GL_DEPTH_TEST);
    1.37  
    1.38  	if(!GLEW_ARB_tessellation_shader) {
    1.39  		fprintf(stderr, "your OpenGL implementation does not support tesselation shaders\n");
    1.40 @@ -66,7 +72,6 @@
    1.41  			return 1;
    1.42  		}
    1.43  	}
    1.44 -	set_uniform_int(prog, "tess_level", 1);
    1.45  
    1.46  	glutMainLoop();
    1.47  	return 0;
    1.48 @@ -89,33 +94,64 @@
    1.49  
    1.50  	set_material(0.2, 0.35, 1.0, 1.0, 1.0, 1.0, 60.0);
    1.51  
    1.52 +	draw_teapot();
    1.53 +
    1.54 +	glutSwapBuffers();
    1.55 +}
    1.56 +
    1.57 +void draw_teapot(void)
    1.58 +{
    1.59 +	int i;
    1.60 +
    1.61 +	glPushMatrix();
    1.62 +	glRotatef(-90.0, 1, 0, 0);
    1.63 +
    1.64  	glUseProgram(prog);
    1.65 +	set_uniform_int(prog, "tess_level", tess_level);
    1.66 +	set_uniform_float3(prog, "norm_scale", 1, 1, 1);
    1.67  
    1.68  	glBegin(GL_PATCHES);
    1.69 -	glVertex3f(-1, -0.8, 1);
    1.70 -	glVertex3f(-0.25, -1, 1);
    1.71 -	glVertex3f(0.25, -1, 1);
    1.72 -	glVertex3f(1, 0, 1);
    1.73  
    1.74 -	glVertex3f(-1, -0.4, 0.25);
    1.75 -	glVertex3f(-0.25, 0, 0.25);
    1.76 -	glVertex3f(0.25, 0, 0.25);
    1.77 -	glVertex3f(1, -0.4, 0.25);
    1.78 +	/* first render the front-facing patches */
    1.79 +	for(i=0; i<NUM_TEAPOT_PATCHES; i++) {
    1.80 +		if(teapot_part_flip[i] > 0.0) {
    1.81 +			draw_teapot_patch(teapot_verts, teapot_index + i * 16, 0, teapot_part_rot[i]);
    1.82 +		}
    1.83 +	}
    1.84 +	glEnd();
    1.85  
    1.86 -	glVertex3f(-1, -0.4, -0.25);
    1.87 -	glVertex3f(-0.25, 0.6, -0.25);
    1.88 -	glVertex3f(0.25, 0.3, -0.25);
    1.89 -	glVertex3f(1, -0.4, -0.25);
    1.90 +	set_uniform_float3(prog, "norm_scale", -1, -1, -1);
    1.91  
    1.92 -	glVertex3f(-1, 0, -1);
    1.93 -	glVertex3f(-0.25, 0.2, -1);
    1.94 -	glVertex3f(0.25, 0.2, -1);
    1.95 -	glVertex3f(1, 0, -1);
    1.96 +	glFrontFace(GL_CW);
    1.97 +	glBegin(GL_PATCHES);
    1.98 +	/* then render the flipped ones */
    1.99 +	for(i=0; i<NUM_TEAPOT_PATCHES; i++) {
   1.100 +		if(teapot_part_flip[i] < 0.0) {
   1.101 +			draw_teapot_patch(teapot_verts, teapot_index + i * 16, 1, teapot_part_rot[i]);
   1.102 +		}
   1.103 +	}
   1.104  	glEnd();
   1.105 +	glFrontFace(GL_CCW);
   1.106  
   1.107  	glUseProgram(0);
   1.108  
   1.109 -	glutSwapBuffers();
   1.110 +	glPopMatrix();
   1.111 +}
   1.112 +
   1.113 +void draw_teapot_patch(struct vec3 *bez_cp, int *index, int flip, float rot)
   1.114 +{
   1.115 +	int i;
   1.116 +	float cosr = cos(M_PI * rot / 180.0);
   1.117 +	float sinr = sin(M_PI * rot / 180.0);
   1.118 +
   1.119 +	for(i=0; i<16; i++) {
   1.120 +		struct vec3 cp = bez_cp[index[i]];
   1.121 +
   1.122 +		float x = cosr * cp.x + sinr * cp.y;
   1.123 +		float y = -sinr * cp.x + cosr * cp.y;
   1.124 +
   1.125 +		glVertex3f(x, flip ? -y : y, cp.z);
   1.126 +	}
   1.127  }
   1.128  
   1.129  void set_material(float dr, float dg, float db, float sr, float sg, float sb, float shin)
   1.130 @@ -162,14 +198,24 @@
   1.131  			}
   1.132  		}
   1.133  		glutPostRedisplay();
   1.134 +
   1.135 +	case '=':
   1.136 +		if(tess_level < max_tess_level) {
   1.137 +			tess_level++;
   1.138 +			printf("tessellation level: %d\n", tess_level);
   1.139 +			set_uniform_int(prog, "tess_level", tess_level);
   1.140 +			glutPostRedisplay();
   1.141 +		}
   1.142  		break;
   1.143 -	}
   1.144  
   1.145 -	if(key >= '1' && key <= '9') {
   1.146 -		int tess_level = key - '0';
   1.147 -		set_uniform_int(prog, "tess_level", tess_level);
   1.148 -
   1.149 -		glutPostRedisplay();
   1.150 +	case '-':
   1.151 +		if(tess_level > 1) {
   1.152 +			tess_level--;
   1.153 +			printf("tessellation level: %d\n", tess_level);
   1.154 +			set_uniform_int(prog, "tess_level", tess_level);
   1.155 +			glutPostRedisplay();
   1.156 +		}
   1.157 +		break;
   1.158  	}
   1.159  }
   1.160