cloth

diff src/main.cc @ 0:92983e143a03

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 11 Feb 2013 19:40:36 +0200
parents
children 28a31079dcdf
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.cc	Mon Feb 11 19:40:36 2013 +0200
     1.3 @@ -0,0 +1,144 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include "opengl.h"
     1.7 +
     1.8 +static bool init();
     1.9 +static void cleanup();
    1.10 +static void disp();
    1.11 +static void idle();
    1.12 +static void reshape(int x, int y);
    1.13 +static void keyb(unsigned char key, int x, int y);
    1.14 +static void mouse(int bn, int state, int x, int y);
    1.15 +static void motion(int x, int y);
    1.16 +
    1.17 +static float cam_theta, cam_phi, cam_dist = 8.0;
    1.18 +
    1.19 +static bool moving_cloth;
    1.20 +
    1.21 +
    1.22 +int main(int argc, char **argv)
    1.23 +{
    1.24 +	glutInit(&argc, argv);
    1.25 +	glutInitWindowSize(800, 600);
    1.26 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.27 +	glutCreateWindow("cloth");
    1.28 +
    1.29 +	glutDisplayFunc(disp);
    1.30 +	glutReshapeFunc(reshape);
    1.31 +	glutKeyboardFunc(keyb);
    1.32 +	glutMouseFunc(mouse);
    1.33 +	glutMotionFunc(motion);
    1.34 +	glutIdleFunc(idle);
    1.35 +
    1.36 +	if(!init()) {
    1.37 +		return 1;
    1.38 +	}
    1.39 +	atexit(cleanup);
    1.40 +
    1.41 +	glutMainLoop();
    1.42 +	return 0;
    1.43 +}
    1.44 +
    1.45 +
    1.46 +static bool init()
    1.47 +{
    1.48 +	glewInit();
    1.49 +
    1.50 +	glClearColor(0.2, 0.2, 0.2, 1.0);
    1.51 +
    1.52 +	glEnable(GL_DEPTH_TEST);
    1.53 +	glEnable(GL_CULL_FACE);
    1.54 +
    1.55 +	return true;
    1.56 +}
    1.57 +
    1.58 +static void cleanup()
    1.59 +{
    1.60 +}
    1.61 +
    1.62 +static void disp()
    1.63 +{
    1.64 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.65 +
    1.66 +	glMatrixMode(GL_MODELVIEW);
    1.67 +	glLoadIdentity();
    1.68 +	glTranslatef(0, 0, -cam_dist);
    1.69 +	glRotatef(cam_phi, 1.0, 0.0, 0.0);
    1.70 +	glRotatef(cam_theta, 0.0, 1.0, 0.0);
    1.71 +
    1.72 +	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    1.73 +
    1.74 +	glutSolidTeapot(1.0);
    1.75 +
    1.76 +	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    1.77 +
    1.78 +	glutSwapBuffers();
    1.79 +}
    1.80 +
    1.81 +static void idle()
    1.82 +{
    1.83 +	glutPostRedisplay();
    1.84 +}
    1.85 +
    1.86 +static void reshape(int x, int y)
    1.87 +{
    1.88 +	glViewport(0, 0, x, y);
    1.89 +
    1.90 +	glMatrixMode(GL_PROJECTION);
    1.91 +	glLoadIdentity();
    1.92 +	gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
    1.93 +}
    1.94 +
    1.95 +static void keyb(unsigned char key, int x, int y)
    1.96 +{
    1.97 +	switch(key) {
    1.98 +	case 27:
    1.99 +		exit(0);
   1.100 +	}
   1.101 +}
   1.102 +
   1.103 +static bool bnstate[16];
   1.104 +static int prev_x, prev_y;
   1.105 +
   1.106 +static void mouse(int bn, int state, int x, int y)
   1.107 +{
   1.108 +	int idx = bn - GLUT_LEFT_BUTTON;
   1.109 +	int st = state == GLUT_DOWN ? 1 : 0;
   1.110 +
   1.111 +	if(idx >= 0 && idx < 16) {
   1.112 +		bnstate[idx] = st;
   1.113 +	}
   1.114 +
   1.115 +	prev_x = x;
   1.116 +	prev_y = y;
   1.117 +}
   1.118 +
   1.119 +static void motion(int x, int y)
   1.120 +{
   1.121 +	int dx = x - prev_x;
   1.122 +	int dy = y - prev_y;
   1.123 +	prev_x = x;
   1.124 +	prev_y = y;
   1.125 +
   1.126 +	if(moving_cloth) {
   1.127 +	} else {
   1.128 +		if(bnstate[0]) {
   1.129 +			cam_theta += dx * 0.5;
   1.130 +			cam_phi += dy * 0.5;
   1.131 +
   1.132 +			if(cam_phi < -90.0) {
   1.133 +				cam_phi = -90.0;
   1.134 +			}
   1.135 +			if(cam_phi > 90.0) {
   1.136 +				cam_phi = 90.0;
   1.137 +			}
   1.138 +		}
   1.139 +		if(bnstate[2]) {
   1.140 +			cam_dist += dy * 0.1;
   1.141 +
   1.142 +			if(cam_dist < 0.0) {
   1.143 +				cam_dist = 0.0;
   1.144 +			}
   1.145 +		}
   1.146 +	}
   1.147 +}