cloth

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "opengl.h"
5 static bool init();
6 static void cleanup();
7 static void disp();
8 static void idle();
9 static void reshape(int x, int y);
10 static void keyb(unsigned char key, int x, int y);
11 static void mouse(int bn, int state, int x, int y);
12 static void motion(int x, int y);
14 static float cam_theta, cam_phi, cam_dist = 8.0;
16 static bool moving_cloth;
19 int main(int argc, char **argv)
20 {
21 glutInit(&argc, argv);
22 glutInitWindowSize(800, 600);
23 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
24 glutCreateWindow("cloth");
26 glutDisplayFunc(disp);
27 glutReshapeFunc(reshape);
28 glutKeyboardFunc(keyb);
29 glutMouseFunc(mouse);
30 glutMotionFunc(motion);
31 glutIdleFunc(idle);
33 if(!init()) {
34 return 1;
35 }
36 atexit(cleanup);
38 glutMainLoop();
39 return 0;
40 }
43 static bool init()
44 {
45 glewInit();
47 glClearColor(0.2, 0.2, 0.2, 1.0);
49 glEnable(GL_DEPTH_TEST);
50 glEnable(GL_CULL_FACE);
52 return true;
53 }
55 static void cleanup()
56 {
57 }
59 static void disp()
60 {
61 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
63 glMatrixMode(GL_MODELVIEW);
64 glLoadIdentity();
65 glTranslatef(0, 0, -cam_dist);
66 glRotatef(cam_phi, 1.0, 0.0, 0.0);
67 glRotatef(cam_theta, 0.0, 1.0, 0.0);
69 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
71 glutSolidTeapot(1.0);
73 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
75 glutSwapBuffers();
76 }
78 static void idle()
79 {
80 glutPostRedisplay();
81 }
83 static void reshape(int x, int y)
84 {
85 glViewport(0, 0, x, y);
87 glMatrixMode(GL_PROJECTION);
88 glLoadIdentity();
89 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
90 }
92 static void keyb(unsigned char key, int x, int y)
93 {
94 switch(key) {
95 case 27:
96 exit(0);
97 }
98 }
100 static bool bnstate[16];
101 static int prev_x, prev_y;
103 static void mouse(int bn, int state, int x, int y)
104 {
105 int idx = bn - GLUT_LEFT_BUTTON;
106 int st = state == GLUT_DOWN ? 1 : 0;
108 if(idx >= 0 && idx < 16) {
109 bnstate[idx] = st;
110 }
112 prev_x = x;
113 prev_y = y;
114 }
116 static void motion(int x, int y)
117 {
118 int dx = x - prev_x;
119 int dy = y - prev_y;
120 prev_x = x;
121 prev_y = y;
123 if(moving_cloth) {
124 } else {
125 if(bnstate[0]) {
126 cam_theta += dx * 0.5;
127 cam_phi += dy * 0.5;
129 if(cam_phi < -90.0) {
130 cam_phi = -90.0;
131 }
132 if(cam_phi > 90.0) {
133 cam_phi = 90.0;
134 }
135 }
136 if(bnstate[2]) {
137 cam_dist += dy * 0.1;
139 if(cam_dist < 0.0) {
140 cam_dist = 0.0;
141 }
142 }
143 }
144 }