# HG changeset patch # User John Tsiombikas # Date 1385837606 -7200 # Node ID f61cc1df533c92b66ce1ad5e487d7436dba0289e # Parent 7d6b667821cf2d748d5052f51451cfba904b8ff2 added viewscn example (under dev) diff -r 7d6b667821cf -r f61cc1df533c examples/viewscn/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/viewscn/Makefile Sat Nov 30 20:53:26 2013 +0200 @@ -0,0 +1,19 @@ +src = $(wildcard src/*.cc) +obj = $(src:.cc=.o) +bin = viewscn + +CXXFLAGS = -pedantic -Wall -g +LDFLAGS = $(libgl) -lgoat3dgfx -lvmath + +ifeq ($(shell uname -s), Darwin) + libgl = -framework OpenGL -framework GLUT -lGLEW +else + libgl = -lGL -lGLU -lglut -lGLEW +endif + +$(bin): $(obj) + $(CXX) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r 7d6b667821cf -r f61cc1df533c examples/viewscn/src/main.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/viewscn/src/main.cc Sat Nov 30 20:53:26 2013 +0200 @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#include + +using namespace goatgfx; + +static bool init(); +static void cleanup(); +static void display(); +static void reshape(int x, int y); +static void keyboard(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 bool parse_args(int argc, char **argv); + +static float cam_theta, cam_phi; +static const char *scene_filename; +static Scene *scn; + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitWindowSize(800, 600); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutCreateWindow("viewscn"); + + glutDisplayFunc(display); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyboard); + glutMouseFunc(mouse); + glutMotionFunc(motion); + + if(!parse_args(argc, argv)) { + return 1; + } + + if(!init()) { + return 1; + } + atexit(cleanup); + + glutMainLoop(); + return 0; +} + +static bool init() +{ + glewInit(); + + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + scn = new Scene; + if(!scn->load(scene_filename)) { + fatal_log("failed to load scene: %s\n", scene_filename); + return false; + } + + return true; +} + +static void cleanup() +{ + delete scn; +} + +static void display() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + Matrix4x4 view_matrix; + view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0); + view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0); + set_view_matrix(view_matrix); + + setup_gl_matrices(); + + scn->draw(); + + glutSwapBuffers(); + CHECKGLERR; +} + +static void reshape(int x, int y) +{ + glViewport(0, 0, x, y); + + Matrix4x4 proj; + proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0); + set_projection_matrix(proj); +} + +static void keyboard(unsigned char key, int x, int y) +{ + switch(key) { + case 27: + exit(0); + } +} + +static bool bnstate[16]; +static int prev_x, prev_y; + +static void mouse(int bn, int st, int x, int y) +{ + bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; + prev_x = x; + prev_y = y; +} + +static void motion(int x, int y) +{ + int dx = x - prev_x; + int dy = y - prev_y; + prev_x = x; + prev_y = y; + + if(!dx && !dy) return; + + if(bnstate[0]) { + cam_theta += dx * 0.5; + cam_phi += dy * 0.5; + cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi)); + glutPostRedisplay(); + } +} + +static bool parse_args(int argc, char **argv) +{ + for(int i=1; i\n", argv[i]); + exit(0); + } + } else { + if(scene_filename) { + fprintf(stderr, "unexpected argument: %s\n", argv[i]); + return false; + } + scene_filename = argv[i]; + } + } + + if(!scene_filename) { + fprintf(stderr, "you must specify the scene file to view\n"); + return false; + } + + return true; +}