#include <GL/glut.h>
#endif
#include "app.h"
-#include "sdr.h"
-#include "shadow.h"
-#include "texture.h"
-#include "mesh.h"
-#include "meshgen.h"
static bool init();
-static void cleanup();
static void display();
static void idle();
-static void draw_scene();
static void reshape(int x, int y);
-static void keyb(unsigned char key, int x, int y);
+static void key_press(unsigned char key, int x, int y);
+static void key_release(unsigned char key, int x, int y);
static void mouse(int bn, int st, int x, int y);
-static void motion(int x, int y);
-static void passive_motion(int x, int y);
-
-static int win_width, win_height;
-
-static float cam_dist = 0.25;
-static float cam_theta, cam_phi = 20;
-static int prev_mx, prev_my;
-static bool bnstate[8];
-
-static Mat4 view_matrix;
-
-static unsigned int start_time, prev_msec;
-
-static TextureSet texman;
+static unsigned int start_time;
int main(int argc, char **argv)
{
glutDisplayFunc(display);
glutIdleFunc(idle);
glutReshapeFunc(reshape);
- glutKeyboardFunc(keyb);
+ glutKeyboardFunc(key_press);
+ glutKeyboardUpFunc(key_release);
glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutPassiveMotionFunc(passive_motion);
+ glutMotionFunc(app_mouse_motion);
+ glutPassiveMotionFunc(app_mouse_motion);
if(!init()) {
return 1;
}
- atexit(cleanup);
+ atexit(app_cleanup);
glutMainLoop();
return 0;
}
-static bool init()
+void app_swap_buffers()
{
- glewInit();
-
- glEnable(GL_MULTISAMPLE);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
- glEnable(GL_LIGHTING);
- glEnable(GL_NORMALIZE);
-
- Mesh::use_custom_sdr_attr = false;
-
- float ambient[] = {0.1, 0.1, 0.1, 0.0};
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
-
- glUseProgram(0);
-
- start_time = glutGet(GLUT_ELAPSED_TIME);
- return true;
+ glutSwapBuffers();
}
-static void cleanup()
+void app_quit()
{
- texman.clear();
+ exit(0);
}
-static void update(float dt)
+static bool init()
{
- texman.update();
-}
+ glewInit();
-static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
-{
- unsigned int lt = GL_LIGHT0 + idx;
- float posv[] = { pos.x, pos.y, pos.z, 1 };
- float colv[] = { color.x, color.y, color.z, 1 };
-
- glEnable(lt);
- glLightfv(lt, GL_POSITION, posv);
- glLightfv(lt, GL_DIFFUSE, colv);
- glLightfv(lt, GL_SPECULAR, colv);
+ if(!app_init()) {
+ return false;
+ }
+
+ start_time = glutGet(GLUT_ELAPSED_TIME);
+ return true;
}
static void display()
{
- unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
- float dt = (float)(msec - prev_msec) / 1000.0f;
- prev_msec = msec;
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- view_matrix = Mat4::identity;
- view_matrix.pre_translate(0, 0, -cam_dist);
- view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
- view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadMatrixf(view_matrix[0]);
-
- static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
- set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
- set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
- set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
-
- update(dt);
-
- draw_scene();
-
- glutSwapBuffers();
- assert(glGetError() == GL_NO_ERROR);
+ time_msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
+ app_display();
}
static void idle()
glutPostRedisplay();
}
-static void draw_scene()
-{
- glBegin(GL_QUADS);
- glNormal3f(0, 1, 0);
- glVertex3f(-30, -10, 30);
- glVertex3f(30, -10, 30);
- glVertex3f(30, -10, -30);
- glVertex3f(-30, -10, -30);
- glEnd();
-}
-
static void reshape(int x, int y)
{
win_width = x;
win_height = y;
- glViewport(0, 0, x, y);
+ app_reshape(x, y);
+}
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(50.0, (float)x / (float)y, 0.01, 50.0);
+static void key_press(unsigned char key, int x, int y)
+{
+ app_keyboard(key, true);
}
-static void keyb(unsigned char key, int x, int y)
+static void key_release(unsigned char key, int x, int y)
{
- switch(key) {
- case 27:
- exit(0);
- }
+ app_keyboard(key, false);
}
static void mouse(int bn, int st, int x, int y)
int bidx = bn - GLUT_LEFT_BUTTON;
bool down = st == GLUT_DOWN;
- prev_mx = x;
- prev_my = y;
- bnstate[bidx] = down;
-}
-
-static void motion(int x, int y)
-{
- int dx = x - prev_mx;
- int dy = y - prev_my;
- prev_mx = x;
- prev_my = y;
-
- if(!dx && !dy) return;
-
- if(bnstate[0]) {
- cam_theta += dx * 0.5;
- cam_phi += dy * 0.5;
-
- if(cam_phi < -90) cam_phi = -90;
- if(cam_phi > 90) cam_phi = 90;
- glutPostRedisplay();
- }
- if(bnstate[2]) {
- cam_dist += dy * 0.01;
- if(cam_dist < 0.0) cam_dist = 0.0;
- glutPostRedisplay();
- }
-}
-
-static void passive_motion(int x, int y)
-{
- prev_mx = x;
- prev_my = y;
+ app_mouse_button(bidx, down, x, y);
}