cloth2
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main.cc Mon Jan 11 16:51:16 2016 +0200 1.3 @@ -0,0 +1,142 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <assert.h> 1.7 +#include <GL/glew.h> 1.8 +#ifdef __APPLE__ 1.9 +#include <GLUT/glut.h> 1.10 +#else 1.11 +#include <GL/glut.h> 1.12 +#endif 1.13 +#include "object.h" 1.14 +#include "cloth.h" 1.15 + 1.16 +bool init(); 1.17 +void cleanup(); 1.18 +void display(); 1.19 +void idle(); 1.20 +void reshape(int x, int y); 1.21 +void keyboard(unsigned char key, int x, int y); 1.22 +void mouse(int bn, int st, int x, int y); 1.23 +void motion(int x, int y); 1.24 + 1.25 +float cam_theta, cam_phi, cam_dist = 8; 1.26 + 1.27 +Cloth cloth; 1.28 + 1.29 +int main(int argc, char **argv) 1.30 +{ 1.31 + glutInit(&argc, argv); 1.32 + glutInitWindowSize(800, 600); 1.33 + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 1.34 + glutCreateWindow("foo"); 1.35 + 1.36 + glutDisplayFunc(display); 1.37 + glutIdleFunc(idle); 1.38 + glutReshapeFunc(reshape); 1.39 + glutKeyboardFunc(keyboard); 1.40 + glutMouseFunc(mouse); 1.41 + glutMotionFunc(motion); 1.42 + 1.43 + if(!init()) { 1.44 + return 1; 1.45 + } 1.46 + atexit(cleanup); 1.47 + 1.48 + glutMainLoop(); 1.49 +} 1.50 + 1.51 + 1.52 +bool init() 1.53 +{ 1.54 + glewInit(); 1.55 + 1.56 + glEnable(GL_DEPTH_TEST); 1.57 + glEnable(GL_CULL_FACE); 1.58 + //glEnable(GL_LIGHTING); 1.59 + glEnable(GL_LIGHT0); 1.60 + 1.61 + cloth.create_rect(32, 32, 10, 10); 1.62 + 1.63 + Matrix4x4 xform = Matrix4x4(1, 0, 0, 0, 1.64 + 0, 1, 0, 0, 1.65 + 0, 0, 1, 0, 1.66 + 0, 1, 0, 1); 1.67 + cloth.transform(xform); 1.68 + 1.69 + 1.70 + return true; 1.71 +} 1.72 + 1.73 +void cleanup() 1.74 +{ 1.75 +} 1.76 + 1.77 +void display() 1.78 +{ 1.79 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.80 + 1.81 + glMatrixMode(GL_MODELVIEW); 1.82 + glLoadIdentity(); 1.83 + glTranslatef(0, 0, -cam_dist); 1.84 + glRotatef(cam_phi, 1, 0, 0); 1.85 + glRotatef(cam_theta, 0, 1, 0); 1.86 + 1.87 + cloth.draw(); 1.88 + 1.89 + glutSwapBuffers(); 1.90 + assert(glGetError() == GL_NO_ERROR); 1.91 +} 1.92 + 1.93 +void idle() 1.94 +{ 1.95 + glutPostRedisplay(); 1.96 +} 1.97 + 1.98 +void reshape(int x, int y) 1.99 +{ 1.100 + glViewport(0, 0, x, y); 1.101 + 1.102 + glMatrixMode(GL_PROJECTION); 1.103 + glLoadIdentity(); 1.104 + gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0); 1.105 +} 1.106 + 1.107 +void keyboard(unsigned char key, int x, int y) 1.108 +{ 1.109 + switch(key) { 1.110 + case 27: 1.111 + exit(0); 1.112 + } 1.113 +} 1.114 + 1.115 +static bool bnstate[16]; 1.116 +static int prev_x, prev_y; 1.117 + 1.118 +void mouse(int bn, int st, int x, int y) 1.119 +{ 1.120 + prev_x = x; 1.121 + prev_y = y; 1.122 + bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; 1.123 +} 1.124 + 1.125 +void motion(int x, int y) 1.126 +{ 1.127 + int dx = x - prev_x; 1.128 + int dy = y - prev_y; 1.129 + prev_x = x; 1.130 + prev_y = y; 1.131 + 1.132 + if(!dx && !dy) return; 1.133 + 1.134 + if(bnstate[0]) { 1.135 + cam_theta += dx * 0.5; 1.136 + cam_phi += dy * 0.5; 1.137 + 1.138 + if(cam_phi < -90) cam_phi = -90; 1.139 + if(cam_phi > 90) cam_phi = 90; 1.140 + } 1.141 + if(bnstate[2]) { 1.142 + cam_dist += dy * 0.1; 1.143 + if(cam_dist < 0.0) cam_dist = 0.0; 1.144 + } 1.145 +}