cloth

annotate src/main.cc @ 2:2eac424f58b2

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