cloth

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