goat3dgfx

diff examples/viewscn/src/main.cc @ 16:f61cc1df533c

added viewscn example (under dev)
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Nov 2013 20:53:26 +0200
parents
children 6f82b9b6d6c3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/viewscn/src/main.cc	Sat Nov 30 20:53:26 2013 +0200
     1.3 @@ -0,0 +1,155 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <algorithm>
     1.7 +#include <goat3dgfx/goat3dgfx.h>
     1.8 +#include <vmath/vmath.h>
     1.9 +
    1.10 +using namespace goatgfx;
    1.11 +
    1.12 +static bool init();
    1.13 +static void cleanup();
    1.14 +static void display();
    1.15 +static void reshape(int x, int y);
    1.16 +static void keyboard(unsigned char key, int x, int y);
    1.17 +static void mouse(int bn, int st, int x, int y);
    1.18 +static void motion(int x, int y);
    1.19 +static bool parse_args(int argc, char **argv);
    1.20 +
    1.21 +static float cam_theta, cam_phi;
    1.22 +static const char *scene_filename;
    1.23 +static Scene *scn;
    1.24 +
    1.25 +int main(int argc, char **argv)
    1.26 +{
    1.27 +	glutInit(&argc, argv);
    1.28 +	glutInitWindowSize(800, 600);
    1.29 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.30 +	glutCreateWindow("viewscn");
    1.31 +
    1.32 +	glutDisplayFunc(display);
    1.33 +	glutReshapeFunc(reshape);
    1.34 +	glutKeyboardFunc(keyboard);
    1.35 +	glutMouseFunc(mouse);
    1.36 +	glutMotionFunc(motion);
    1.37 +
    1.38 +	if(!parse_args(argc, argv)) {
    1.39 +		return 1;
    1.40 +	}
    1.41 +
    1.42 +	if(!init()) {
    1.43 +		return 1;
    1.44 +	}
    1.45 +	atexit(cleanup);
    1.46 +
    1.47 +	glutMainLoop();
    1.48 +	return 0;
    1.49 +}
    1.50 +
    1.51 +static bool init()
    1.52 +{
    1.53 +	glewInit();
    1.54 +
    1.55 +	glEnable(GL_DEPTH_TEST);
    1.56 +	glEnable(GL_CULL_FACE);
    1.57 +
    1.58 +	scn = new Scene;
    1.59 +	if(!scn->load(scene_filename)) {
    1.60 +		fatal_log("failed to load scene: %s\n", scene_filename);
    1.61 +		return false;
    1.62 +	}
    1.63 +
    1.64 +	return true;
    1.65 +}
    1.66 +
    1.67 +static void cleanup()
    1.68 +{
    1.69 +	delete scn;
    1.70 +}
    1.71 +
    1.72 +static void display()
    1.73 +{
    1.74 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.75 +
    1.76 +	Matrix4x4 view_matrix;
    1.77 +	view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0);
    1.78 +	view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0);
    1.79 +	set_view_matrix(view_matrix);
    1.80 +
    1.81 +	setup_gl_matrices();
    1.82 +
    1.83 +	scn->draw();
    1.84 +
    1.85 +	glutSwapBuffers();
    1.86 +	CHECKGLERR;
    1.87 +}
    1.88 +
    1.89 +static void reshape(int x, int y)
    1.90 +{
    1.91 +	glViewport(0, 0, x, y);
    1.92 +
    1.93 +	Matrix4x4 proj;
    1.94 +	proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
    1.95 +	set_projection_matrix(proj);
    1.96 +}
    1.97 +
    1.98 +static void keyboard(unsigned char key, int x, int y)
    1.99 +{
   1.100 +	switch(key) {
   1.101 +	case 27:
   1.102 +		exit(0);
   1.103 +	}
   1.104 +}
   1.105 +
   1.106 +static bool bnstate[16];
   1.107 +static int prev_x, prev_y;
   1.108 +
   1.109 +static void mouse(int bn, int st, int x, int y)
   1.110 +{
   1.111 +	bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
   1.112 +	prev_x = x;
   1.113 +	prev_y = y;
   1.114 +}
   1.115 +
   1.116 +static void motion(int x, int y)
   1.117 +{
   1.118 +	int dx = x - prev_x;
   1.119 +	int dy = y - prev_y;
   1.120 +	prev_x = x;
   1.121 +	prev_y = y;
   1.122 +
   1.123 +	if(!dx && !dy) return;
   1.124 +
   1.125 +	if(bnstate[0]) {
   1.126 +		cam_theta += dx * 0.5;
   1.127 +		cam_phi += dy * 0.5;
   1.128 +		cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
   1.129 +		glutPostRedisplay();
   1.130 +	}
   1.131 +}
   1.132 +
   1.133 +static bool parse_args(int argc, char **argv)
   1.134 +{
   1.135 +	for(int i=1; i<argc; i++) {
   1.136 +		if(argv[i][0] == '-' && argv[i][2] == 0) {
   1.137 +			switch(argv[i][1]) {
   1.138 +			case 'h':
   1.139 +			default:
   1.140 +				printf("usage: %s <filename>\n", argv[i]);
   1.141 +				exit(0);
   1.142 +			}
   1.143 +		} else {
   1.144 +			if(scene_filename) {
   1.145 +				fprintf(stderr, "unexpected argument: %s\n", argv[i]);
   1.146 +				return false;
   1.147 +			}
   1.148 +			scene_filename = argv[i];
   1.149 +		}
   1.150 +	}
   1.151 +
   1.152 +	if(!scene_filename) {
   1.153 +		fprintf(stderr, "you must specify the scene file to view\n");
   1.154 +		return false;
   1.155 +	}
   1.156 +
   1.157 +	return true;
   1.158 +}