goat3dgfx

changeset 16:f61cc1df533c

added viewscn example (under dev)
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Nov 2013 20:53:26 +0200
parents 7d6b667821cf
children f2da87b02fcd
files examples/viewscn/Makefile examples/viewscn/src/main.cc
diffstat 2 files changed, 174 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/viewscn/Makefile	Sat Nov 30 20:53:26 2013 +0200
     1.3 @@ -0,0 +1,19 @@
     1.4 +src = $(wildcard src/*.cc)
     1.5 +obj = $(src:.cc=.o)
     1.6 +bin = viewscn
     1.7 +
     1.8 +CXXFLAGS = -pedantic -Wall -g
     1.9 +LDFLAGS = $(libgl) -lgoat3dgfx -lvmath
    1.10 +
    1.11 +ifeq ($(shell uname -s), Darwin)
    1.12 +	libgl = -framework OpenGL -framework GLUT -lGLEW
    1.13 +else
    1.14 +	libgl = -lGL -lGLU -lglut -lGLEW
    1.15 +endif
    1.16 +
    1.17 +$(bin): $(obj)
    1.18 +	$(CXX) -o $@ $(obj) $(LDFLAGS)
    1.19 +
    1.20 +.PHONY: clean
    1.21 +clean:
    1.22 +	rm -f $(obj) $(bin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/examples/viewscn/src/main.cc	Sat Nov 30 20:53:26 2013 +0200
     2.3 @@ -0,0 +1,155 @@
     2.4 +#include <stdio.h>
     2.5 +#include <stdlib.h>
     2.6 +#include <algorithm>
     2.7 +#include <goat3dgfx/goat3dgfx.h>
     2.8 +#include <vmath/vmath.h>
     2.9 +
    2.10 +using namespace goatgfx;
    2.11 +
    2.12 +static bool init();
    2.13 +static void cleanup();
    2.14 +static void display();
    2.15 +static void reshape(int x, int y);
    2.16 +static void keyboard(unsigned char key, int x, int y);
    2.17 +static void mouse(int bn, int st, int x, int y);
    2.18 +static void motion(int x, int y);
    2.19 +static bool parse_args(int argc, char **argv);
    2.20 +
    2.21 +static float cam_theta, cam_phi;
    2.22 +static const char *scene_filename;
    2.23 +static Scene *scn;
    2.24 +
    2.25 +int main(int argc, char **argv)
    2.26 +{
    2.27 +	glutInit(&argc, argv);
    2.28 +	glutInitWindowSize(800, 600);
    2.29 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    2.30 +	glutCreateWindow("viewscn");
    2.31 +
    2.32 +	glutDisplayFunc(display);
    2.33 +	glutReshapeFunc(reshape);
    2.34 +	glutKeyboardFunc(keyboard);
    2.35 +	glutMouseFunc(mouse);
    2.36 +	glutMotionFunc(motion);
    2.37 +
    2.38 +	if(!parse_args(argc, argv)) {
    2.39 +		return 1;
    2.40 +	}
    2.41 +
    2.42 +	if(!init()) {
    2.43 +		return 1;
    2.44 +	}
    2.45 +	atexit(cleanup);
    2.46 +
    2.47 +	glutMainLoop();
    2.48 +	return 0;
    2.49 +}
    2.50 +
    2.51 +static bool init()
    2.52 +{
    2.53 +	glewInit();
    2.54 +
    2.55 +	glEnable(GL_DEPTH_TEST);
    2.56 +	glEnable(GL_CULL_FACE);
    2.57 +
    2.58 +	scn = new Scene;
    2.59 +	if(!scn->load(scene_filename)) {
    2.60 +		fatal_log("failed to load scene: %s\n", scene_filename);
    2.61 +		return false;
    2.62 +	}
    2.63 +
    2.64 +	return true;
    2.65 +}
    2.66 +
    2.67 +static void cleanup()
    2.68 +{
    2.69 +	delete scn;
    2.70 +}
    2.71 +
    2.72 +static void display()
    2.73 +{
    2.74 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    2.75 +
    2.76 +	Matrix4x4 view_matrix;
    2.77 +	view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0);
    2.78 +	view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0);
    2.79 +	set_view_matrix(view_matrix);
    2.80 +
    2.81 +	setup_gl_matrices();
    2.82 +
    2.83 +	scn->draw();
    2.84 +
    2.85 +	glutSwapBuffers();
    2.86 +	CHECKGLERR;
    2.87 +}
    2.88 +
    2.89 +static void reshape(int x, int y)
    2.90 +{
    2.91 +	glViewport(0, 0, x, y);
    2.92 +
    2.93 +	Matrix4x4 proj;
    2.94 +	proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
    2.95 +	set_projection_matrix(proj);
    2.96 +}
    2.97 +
    2.98 +static void keyboard(unsigned char key, int x, int y)
    2.99 +{
   2.100 +	switch(key) {
   2.101 +	case 27:
   2.102 +		exit(0);
   2.103 +	}
   2.104 +}
   2.105 +
   2.106 +static bool bnstate[16];
   2.107 +static int prev_x, prev_y;
   2.108 +
   2.109 +static void mouse(int bn, int st, int x, int y)
   2.110 +{
   2.111 +	bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
   2.112 +	prev_x = x;
   2.113 +	prev_y = y;
   2.114 +}
   2.115 +
   2.116 +static void motion(int x, int y)
   2.117 +{
   2.118 +	int dx = x - prev_x;
   2.119 +	int dy = y - prev_y;
   2.120 +	prev_x = x;
   2.121 +	prev_y = y;
   2.122 +
   2.123 +	if(!dx && !dy) return;
   2.124 +
   2.125 +	if(bnstate[0]) {
   2.126 +		cam_theta += dx * 0.5;
   2.127 +		cam_phi += dy * 0.5;
   2.128 +		cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
   2.129 +		glutPostRedisplay();
   2.130 +	}
   2.131 +}
   2.132 +
   2.133 +static bool parse_args(int argc, char **argv)
   2.134 +{
   2.135 +	for(int i=1; i<argc; i++) {
   2.136 +		if(argv[i][0] == '-' && argv[i][2] == 0) {
   2.137 +			switch(argv[i][1]) {
   2.138 +			case 'h':
   2.139 +			default:
   2.140 +				printf("usage: %s <filename>\n", argv[i]);
   2.141 +				exit(0);
   2.142 +			}
   2.143 +		} else {
   2.144 +			if(scene_filename) {
   2.145 +				fprintf(stderr, "unexpected argument: %s\n", argv[i]);
   2.146 +				return false;
   2.147 +			}
   2.148 +			scene_filename = argv[i];
   2.149 +		}
   2.150 +	}
   2.151 +
   2.152 +	if(!scene_filename) {
   2.153 +		fprintf(stderr, "you must specify the scene file to view\n");
   2.154 +		return false;
   2.155 +	}
   2.156 +
   2.157 +	return true;
   2.158 +}