cloth2

view src/main.cc @ 0:ef0c22554406

cloth sim test, initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 11 Jan 2016 16:51:16 +0200
parents
children dc15b741486c
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <GL/glew.h>
5 #ifdef __APPLE__
6 #include <GLUT/glut.h>
7 #else
8 #include <GL/glut.h>
9 #endif
10 #include "object.h"
11 #include "cloth.h"
13 bool init();
14 void cleanup();
15 void display();
16 void idle();
17 void reshape(int x, int y);
18 void keyboard(unsigned char key, int x, int y);
19 void mouse(int bn, int st, int x, int y);
20 void motion(int x, int y);
22 float cam_theta, cam_phi, cam_dist = 8;
24 Cloth cloth;
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("foo");
33 glutDisplayFunc(display);
34 glutIdleFunc(idle);
35 glutReshapeFunc(reshape);
36 glutKeyboardFunc(keyboard);
37 glutMouseFunc(mouse);
38 glutMotionFunc(motion);
40 if(!init()) {
41 return 1;
42 }
43 atexit(cleanup);
45 glutMainLoop();
46 }
49 bool init()
50 {
51 glewInit();
53 glEnable(GL_DEPTH_TEST);
54 glEnable(GL_CULL_FACE);
55 //glEnable(GL_LIGHTING);
56 glEnable(GL_LIGHT0);
58 cloth.create_rect(32, 32, 10, 10);
60 Matrix4x4 xform = Matrix4x4(1, 0, 0, 0,
61 0, 1, 0, 0,
62 0, 0, 1, 0,
63 0, 1, 0, 1);
64 cloth.transform(xform);
67 return true;
68 }
70 void cleanup()
71 {
72 }
74 void display()
75 {
76 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
78 glMatrixMode(GL_MODELVIEW);
79 glLoadIdentity();
80 glTranslatef(0, 0, -cam_dist);
81 glRotatef(cam_phi, 1, 0, 0);
82 glRotatef(cam_theta, 0, 1, 0);
84 cloth.draw();
86 glutSwapBuffers();
87 assert(glGetError() == GL_NO_ERROR);
88 }
90 void idle()
91 {
92 glutPostRedisplay();
93 }
95 void reshape(int x, int y)
96 {
97 glViewport(0, 0, x, y);
99 glMatrixMode(GL_PROJECTION);
100 glLoadIdentity();
101 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
102 }
104 void keyboard(unsigned char key, int x, int y)
105 {
106 switch(key) {
107 case 27:
108 exit(0);
109 }
110 }
112 static bool bnstate[16];
113 static int prev_x, prev_y;
115 void mouse(int bn, int st, int x, int y)
116 {
117 prev_x = x;
118 prev_y = y;
119 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
120 }
122 void motion(int x, int y)
123 {
124 int dx = x - prev_x;
125 int dy = y - prev_y;
126 prev_x = x;
127 prev_y = y;
129 if(!dx && !dy) return;
131 if(bnstate[0]) {
132 cam_theta += dx * 0.5;
133 cam_phi += dy * 0.5;
135 if(cam_phi < -90) cam_phi = -90;
136 if(cam_phi > 90) cam_phi = 90;
137 }
138 if(bnstate[2]) {
139 cam_dist += dy * 0.1;
140 if(cam_dist < 0.0) cam_dist = 0.0;
141 }
142 }