cloth
view src/main.cc @ 3:28a31079dcdf
disc
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 04 Jan 2016 10:52:15 +0200 |
parents | 92983e143a03 |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <vector>
4 #include "opengl.h"
5 #include "plane.h"
6 #include "disc.h"
7 #include "particle.h"
8 #include "simworld.h"
10 static bool init();
11 static void cleanup();
12 static void disp();
13 static void idle();
14 static void reshape(int x, int y);
15 static void keyb(unsigned char key, int x, int y);
16 static void mouse(int bn, int state, int x, int y);
17 static void motion(int x, int y);
19 static float cam_theta, cam_phi, cam_dist = 8.0;
21 static bool moving_cloth;
23 static SimWorld simworld;
26 int main(int argc, char **argv)
27 {
28 glutInit(&argc, argv);
29 glutInitWindowSize(800, 600);
30 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
31 glutCreateWindow("cloth");
33 glutDisplayFunc(disp);
34 glutReshapeFunc(reshape);
35 glutKeyboardFunc(keyb);
36 glutMouseFunc(mouse);
37 glutMotionFunc(motion);
38 glutIdleFunc(idle);
40 if(!init()) {
41 return 1;
42 }
43 atexit(cleanup);
45 glutMainLoop();
46 return 0;
47 }
50 static bool init()
51 {
52 glewInit();
54 glClearColor(0.2, 0.2, 0.2, 1.0);
56 glEnable(GL_DEPTH_TEST);
57 glEnable(GL_CULL_FACE);
59 Disc *disc = new Disc;
60 disc->normal = Vector3(0, 1, 0);
61 simworld.add_object(disc);
63 return true;
64 }
66 static void cleanup()
67 {
68 }
70 static void disp()
71 {
72 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74 glMatrixMode(GL_MODELVIEW);
75 glLoadIdentity();
76 glTranslatef(0, 0, -cam_dist);
77 glRotatef(cam_phi, 1.0, 0.0, 0.0);
78 glRotatef(cam_theta, 0.0, 1.0, 0.0);
80 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
82 for(int i=0; i<simworld.get_object_count(); i++) {
83 Object *obj = simworld.get_object(i);
84 obj->draw();
85 }
87 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
89 glutSwapBuffers();
90 }
92 static void idle()
93 {
94 glutPostRedisplay();
95 }
97 static void reshape(int x, int y)
98 {
99 glViewport(0, 0, x, y);
101 glMatrixMode(GL_PROJECTION);
102 glLoadIdentity();
103 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
104 }
106 static void keyb(unsigned char key, int x, int y)
107 {
108 switch(key) {
109 case 27:
110 exit(0);
111 }
112 }
114 static bool bnstate[16];
115 static int prev_x, prev_y;
117 static void mouse(int bn, int state, int x, int y)
118 {
119 int idx = bn - GLUT_LEFT_BUTTON;
120 int st = state == GLUT_DOWN ? 1 : 0;
122 if(idx >= 0 && idx < 16) {
123 bnstate[idx] = st;
124 }
126 prev_x = x;
127 prev_y = y;
128 }
130 static void motion(int x, int y)
131 {
132 int dx = x - prev_x;
133 int dy = y - prev_y;
134 prev_x = x;
135 prev_y = y;
137 if(moving_cloth) {
138 } else {
139 if(bnstate[0]) {
140 cam_theta += dx * 0.5;
141 cam_phi += dy * 0.5;
143 if(cam_phi < -90.0) {
144 cam_phi = -90.0;
145 }
146 if(cam_phi > 90.0) {
147 cam_phi = 90.0;
148 }
149 }
150 if(bnstate[2]) {
151 cam_dist += dy * 0.1;
153 if(cam_dist < 0.0) {
154 cam_dist = 0.0;
155 }
156 }
157 }
158 }