vrseasons

changeset 0:393ef1143c9c

VR seasons
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 07 Apr 2015 11:16:56 +0300
parents
children 65c2e37c48b2
files .hgignore Makefile src/game.cc src/game.h src/main.h src/main_glut.cc src/opengl.cc src/opengl.h src/opt.cc src/opt.h src/rtarg.cc src/rtarg.h src/scene.cc src/scene.h src/timer.c src/timer.h
diffstat 16 files changed, 698 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Tue Apr 07 11:16:56 2015 +0300
     1.3 @@ -0,0 +1,3 @@
     1.4 +\.o$
     1.5 +\.d$
     1.6 +\.swp$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Makefile	Tue Apr 07 11:16:56 2015 +0300
     2.3 @@ -0,0 +1,41 @@
     2.4 +csrc = $(wildcard src/*.c)
     2.5 +ccsrc = $(wildcard src/*.cc)
     2.6 +obj = $(csrc:.c=.o) $(ccsrc:.cc=.o)
     2.7 +dep = $(obj:.o=.d)
     2.8 +bin = vrseasons
     2.9 +
    2.10 +sys = $(shell uname -s)
    2.11 +
    2.12 +use_glut = 1
    2.13 +
    2.14 +CFLAGS = -pedantic -Wall -g
    2.15 +CXXFLAGS = $(CFLAGS)
    2.16 +LDFLAGS = $(libgl_$(sys)) -ldl -lgoatvr
    2.17 +
    2.18 +libgl_Linux = -lGL -lGLU -lGLEW
    2.19 +libgl_Darwin = -framework OpenGL -lGLEW
    2.20 +
    2.21 +ifdef use_glut
    2.22 +	CFLAGS += -DUSE_GLUT
    2.23 +	libgl_Linux += -lglut
    2.24 +	libgl_Darwin += -framework GLUT
    2.25 +endif
    2.26 +
    2.27 +$(bin): $(obj)
    2.28 +	$(CXX) -o $@ $(obj) $(LDFLAGS)
    2.29 +
    2.30 +-include $(dep)
    2.31 +
    2.32 +%.d: %.c
    2.33 +	@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.34 +
    2.35 +%.d: %.cc
    2.36 +	@$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.37 +
    2.38 +.PHONY: clean
    2.39 +clean:
    2.40 +	rm -f $(obj) $(bin)
    2.41 +
    2.42 +.PHONY: cleandep
    2.43 +cleandep:
    2.44 +	rm -f $(dep)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/game.cc	Tue Apr 07 11:16:56 2015 +0300
     3.3 @@ -0,0 +1,137 @@
     3.4 +#include <assert.h>
     3.5 +#include "opengl.h"
     3.6 +#include "game.h"
     3.7 +#include <stdio.h>
     3.8 +#include <goatvr.h>
     3.9 +#include "main.h"
    3.10 +#include "scene.h"
    3.11 +#include "timer.h"
    3.12 +#include "rtarg.h"
    3.13 +#include "opt.h"
    3.14 +
    3.15 +static bool vr_mode = true;
    3.16 +static int win_width, win_height;
    3.17 +static RenderTarget vr_rtarg;
    3.18 +
    3.19 +bool game_init(int argc, char **argv)
    3.20 +{
    3.21 +	if(!parse_args(argc, argv)) {
    3.22 +		return false;
    3.23 +	}
    3.24 +
    3.25 +	init_gl();
    3.26 +
    3.27 +	if(vr_mode) {
    3.28 +		if(vr_init() == -1) {
    3.29 +			return false;
    3.30 +		}
    3.31 +
    3.32 +		win_width = vr_geti_def(VR_DISPLAY_WIDTH, 1280);
    3.33 +		win_height = vr_geti_def(VR_DISPLAY_HEIGHT, 800);
    3.34 +		resize_window(win_width, win_height);
    3.35 +
    3.36 +		int rt_width = vr_geti_def(VR_RENDER_XRES, win_width);
    3.37 +		int rt_height = vr_geti_def(VR_RENDER_YRES, win_height);
    3.38 +		vr_rtarg.create(rt_width, rt_height);
    3.39 +	}
    3.40 +
    3.41 +	return true;
    3.42 +}
    3.43 +
    3.44 +void game_shutdown()
    3.45 +{
    3.46 +	vr_shutdown();
    3.47 +}
    3.48 +
    3.49 +void game_draw()
    3.50 +{
    3.51 +	static unsigned long prev_msec;
    3.52 +	unsigned long msec = get_msec();
    3.53 +	float dt = (float)(msec - prev_msec) / 1000.0;
    3.54 +
    3.55 +	update_scene(msec, dt);
    3.56 +
    3.57 +	if(vr_mode) {
    3.58 +		set_render_target(&vr_rtarg);
    3.59 +
    3.60 +		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    3.61 +
    3.62 +		for(int i=0; i<2; i++) {
    3.63 +			vr_begin(i);
    3.64 +
    3.65 +			glViewport(i == VR_EYE_LEFT ? 0 : vr_rtarg.get_width() / 2, 0, vr_rtarg.get_width() / 2, vr_rtarg.get_height());
    3.66 +
    3.67 +			glMatrixMode(GL_PROJECTION);
    3.68 +			float proj[16];
    3.69 +			if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
    3.70 +				glLoadMatrixf(proj);
    3.71 +			} else {
    3.72 +				glLoadIdentity();
    3.73 +				gluPerspective(50.0, (float)win_width / 2.0 / (float)win_height, 0.5, 500.0);
    3.74 +			}
    3.75 +
    3.76 +			float view[16];
    3.77 +			vr_view_matrix(i, view);
    3.78 +
    3.79 +			glMatrixMode(GL_MODELVIEW);
    3.80 +			glLoadMatrixf(view);
    3.81 +			glTranslatef(0, -vr_getf(VR_EYE_HEIGHT), 0);
    3.82 +
    3.83 +			draw_scene();
    3.84 +
    3.85 +			vr_end();
    3.86 +		}
    3.87 +
    3.88 +		set_render_target(0);
    3.89 +		glViewport(0, 0, win_width, win_height);
    3.90 +
    3.91 +		vr_swap_buffers();
    3.92 +
    3.93 +	} else {
    3.94 +		glMatrixMode(GL_MODELVIEW);
    3.95 +		glLoadIdentity();
    3.96 +
    3.97 +		draw_scene();
    3.98 +
    3.99 +		swap_buffers();
   3.100 +	}
   3.101 +
   3.102 +	assert(glGetError() == GL_NO_ERROR);
   3.103 +}
   3.104 +
   3.105 +void game_reshape(int x, int y)
   3.106 +{
   3.107 +	glViewport(0, 0, x, y);
   3.108 +
   3.109 +	if(vr_mode) {
   3.110 +		vr_rtarg.resize(x, y);
   3.111 +
   3.112 +		float umax = (float)vr_rtarg.get_width() / (float)vr_rtarg.get_tex_width();
   3.113 +		float vmax = (float)vr_rtarg.get_height() / (float)vr_rtarg.get_tex_height();
   3.114 +		vr_output_texture(vr_rtarg.get_texture(), 0, 0, umax, vmax);
   3.115 +	}
   3.116 +}
   3.117 +
   3.118 +void game_keyboard(int key, bool press)
   3.119 +{
   3.120 +	if(press) {
   3.121 +		switch(key) {
   3.122 +		case 27:
   3.123 +			quit();
   3.124 +
   3.125 +		case ' ':
   3.126 +			if(vr_mode) {
   3.127 +				vr_recenter();
   3.128 +			}
   3.129 +			break;
   3.130 +		}
   3.131 +	}
   3.132 +}
   3.133 +
   3.134 +void game_mbutton(int bn, bool press, int x, int y)
   3.135 +{
   3.136 +}
   3.137 +
   3.138 +void game_mmotion(int x, int y)
   3.139 +{
   3.140 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/game.h	Tue Apr 07 11:16:56 2015 +0300
     4.3 @@ -0,0 +1,13 @@
     4.4 +#ifndef GAME_H_
     4.5 +#define GAME_H_
     4.6 +
     4.7 +bool game_init(int argc, char **argv);
     4.8 +void game_shutdown();
     4.9 +
    4.10 +void game_draw();
    4.11 +void game_reshape(int x, int y);
    4.12 +void game_keyboard(int key, bool press);
    4.13 +void game_mbutton(int bn, bool press, int x, int y);
    4.14 +void game_mmotion(int x, int y);
    4.15 +
    4.16 +#endif	/* GAME_H_ */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/main.h	Tue Apr 07 11:16:56 2015 +0300
     5.3 @@ -0,0 +1,16 @@
     5.4 +#ifndef MAIN_H_
     5.5 +#define MAIN_H_
     5.6 +
     5.7 +/* functions defined by the various main_whatever frontends */
     5.8 +
     5.9 +void quit();
    5.10 +void swap_buffers();
    5.11 +void redisplay();
    5.12 +void fullscreen();
    5.13 +void windowed();
    5.14 +void move_window(int x, int y);
    5.15 +void window_position(int *xout, int *yout);
    5.16 +void resize_window(int x, int y);
    5.17 +void window_size(int *xout, int *yout);
    5.18 +
    5.19 +#endif	/* MAIN_H_ */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/main_glut.cc	Tue Apr 07 11:16:56 2015 +0300
     6.3 @@ -0,0 +1,150 @@
     6.4 +#ifdef USE_GLUT
     6.5 +
     6.6 +#include <stdlib.h>
     6.7 +
     6.8 +#ifdef __APPLE__
     6.9 +#include <GLUT/glut.h>
    6.10 +#else
    6.11 +#include <GL/glut.h>
    6.12 +#endif
    6.13 +
    6.14 +#include "game.h"
    6.15 +
    6.16 +static void display();
    6.17 +static void idle();
    6.18 +static void reshape(int x, int y);
    6.19 +static void key_press(unsigned char key, int x, int y);
    6.20 +static void key_release(unsigned char key, int x, int y);
    6.21 +static void mouse(int bn, int st, int x, int y);
    6.22 +static void motion(int x, int y);
    6.23 +
    6.24 +static bool state_fullscreen;
    6.25 +static int prev_width, prev_height;
    6.26 +static int win_width, win_height;
    6.27 +
    6.28 +int main(int argc, char **argv)
    6.29 +{
    6.30 +	glutInit(&argc, argv);
    6.31 +	glutInitWindowSize(800, 600);
    6.32 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    6.33 +	glutCreateWindow("GLUT");
    6.34 +
    6.35 +	glutDisplayFunc(display);
    6.36 +	glutIdleFunc(idle);
    6.37 +	glutReshapeFunc(reshape);
    6.38 +	glutKeyboardFunc(key_press);
    6.39 +	glutKeyboardUpFunc(key_release);
    6.40 +	glutMouseFunc(mouse);
    6.41 +	glutMotionFunc(motion);
    6.42 +
    6.43 +	if(!game_init(argc, argv)) {
    6.44 +		return 1;
    6.45 +	}
    6.46 +	atexit(game_shutdown);
    6.47 +
    6.48 +	glutMainLoop();
    6.49 +	return 0;
    6.50 +}
    6.51 +
    6.52 +void quit()
    6.53 +{
    6.54 +	exit(0);
    6.55 +}
    6.56 +
    6.57 +void swap_buffers()
    6.58 +{
    6.59 +	glutSwapBuffers();
    6.60 +}
    6.61 +
    6.62 +void redisplay()
    6.63 +{
    6.64 +	glutPostRedisplay();
    6.65 +}
    6.66 +
    6.67 +void idle()
    6.68 +{
    6.69 +	glutPostRedisplay();
    6.70 +}
    6.71 +
    6.72 +void fullscreen()
    6.73 +{
    6.74 +	if(!state_fullscreen) {
    6.75 +		prev_width = win_width;
    6.76 +		prev_height = win_height;
    6.77 +
    6.78 +		glutFullScreen();
    6.79 +		state_fullscreen = true;
    6.80 +	}
    6.81 +}
    6.82 +
    6.83 +void windowed()
    6.84 +{
    6.85 +	if(state_fullscreen) {
    6.86 +		glutReshapeWindow(prev_width, prev_height);
    6.87 +		state_fullscreen = false;
    6.88 +	}
    6.89 +}
    6.90 +
    6.91 +void move_window(int x, int y)
    6.92 +{
    6.93 +	if(!state_fullscreen) {
    6.94 +		glutPositionWindow(x, y);
    6.95 +	}
    6.96 +}
    6.97 +
    6.98 +void window_position(int *xout, int *yout)
    6.99 +{
   6.100 +	*xout = glutGet(GLUT_WINDOW_X);
   6.101 +	*yout = glutGet(GLUT_WINDOW_Y);
   6.102 +}
   6.103 +
   6.104 +void resize_window(int x, int y)
   6.105 +{
   6.106 +	if(!state_fullscreen) {
   6.107 +		glutReshapeWindow(x, y);
   6.108 +	}
   6.109 +}
   6.110 +
   6.111 +void window_size(int *xout, int *yout)
   6.112 +{
   6.113 +	*xout = win_width;
   6.114 +	*yout = win_height;
   6.115 +}
   6.116 +
   6.117 +
   6.118 +static void display()
   6.119 +{
   6.120 +	game_draw();
   6.121 +}
   6.122 +
   6.123 +static void reshape(int x, int y)
   6.124 +{
   6.125 +	win_width = x;
   6.126 +	win_height = y;
   6.127 +	game_reshape(x, y);
   6.128 +}
   6.129 +
   6.130 +static void key_press(unsigned char key, int x, int y)
   6.131 +{
   6.132 +	game_keyboard(key, true);
   6.133 +}
   6.134 +
   6.135 +static void key_release(unsigned char key, int x, int y)
   6.136 +{
   6.137 +	game_keyboard(key, false);
   6.138 +}
   6.139 +
   6.140 +static void mouse(int bn, int st, int x, int y)
   6.141 +{
   6.142 +	game_mbutton(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
   6.143 +}
   6.144 +
   6.145 +static void motion(int x, int y)
   6.146 +{
   6.147 +	game_mmotion(x, y);
   6.148 +}
   6.149 +
   6.150 +int using_glut;
   6.151 +#else
   6.152 +int not_using_glut;
   6.153 +#endif
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/opengl.cc	Tue Apr 07 11:16:56 2015 +0300
     7.3 @@ -0,0 +1,11 @@
     7.4 +#include "opengl.h"
     7.5 +
     7.6 +void init_gl()
     7.7 +{
     7.8 +	glewInit();
     7.9 +}
    7.10 +
    7.11 +bool check_extension(const char *name)
    7.12 +{
    7.13 +	return glewIsSupported(name);
    7.14 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/opengl.h	Tue Apr 07 11:16:56 2015 +0300
     8.3 @@ -0,0 +1,10 @@
     8.4 +#ifndef OPENGL_H_
     8.5 +#define OPENGL_H_
     8.6 +
     8.7 +#include <GL/glew.h>
     8.8 +
     8.9 +void init_gl();
    8.10 +
    8.11 +bool check_extension(const char *name);
    8.12 +
    8.13 +#endif	/* OPENGL_H_ */
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/opt.cc	Tue Apr 07 11:16:56 2015 +0300
     9.3 @@ -0,0 +1,71 @@
     9.4 +#include <stdio.h>
     9.5 +#include "opt.h"
     9.6 +
     9.7 +enum {
     9.8 +	OPT_SIZE,
     9.9 +	OPT_FULLSCREEN,
    9.10 +	OPT_WINDOWED,
    9.11 +	OPT_VR,
    9.12 +	OPT_HELP,
    9.13 +
    9.14 +	NUM_OPTS
    9.15 +};
    9.16 +static const char *optname[] = {
    9.17 +	"size", "fullscreen", "windowed", "vr", "help", 0
    9.18 +};
    9.19 +
    9.20 +static int get_option(const char *str);
    9.21 +
    9.22 +void default_opt(Options *opt)
    9.23 +{
    9.24 +	opt->xres = 1280;
    9.25 +	opt->yres = 800;
    9.26 +	opt->fov = 50.0;
    9.27 +	opt->fullscreen = false;
    9.28 +	opt->use_vr = false;
    9.29 +}
    9.30 +
    9.31 +bool parse_args(Options *opt, int argc, char **argv)
    9.32 +{
    9.33 +	for(int i=1; i<argc; i++) {
    9.34 +		if(argv[i][0] == '-') {
    9.35 +			int o = get_option(argv[i] + 1);
    9.36 +			switch(o) {
    9.37 +			case OPT_SIZE:
    9.38 +				if(sscanf(argv[++i], "%dx%d", &opt->xres, &opt->yres) != 2) {
    9.39 +					fprintf(stderr, "invalid argument to option: %s\n", argv[i - 1]);
    9.40 +					return false;
    9.41 +				}
    9.42 +				break;
    9.43 +
    9.44 +			case OPT_FULLSCREEN:
    9.45 +				opt->fullscreen = true;
    9.46 +				break;
    9.47 +			case OPT_WINDOWED:
    9.48 +				opt->fullscreen = false;
    9.49 +				break;
    9.50 +
    9.51 +			case OPT_VR:
    9.52 +				opt->use_vr = true;
    9.53 +				break;
    9.54 +
    9.55 +			case OPT_HELP:
    9.56 +				printf("options:\n");
    9.57 +				for(int j=0; optname[j]; j++) {
    9.58 +					printf(" -%s\n", optname[j]);
    9.59 +				}
    9.60 +				break;
    9.61 +			}
    9.62 +		}
    9.63 +	}
    9.64 +}
    9.65 +
    9.66 +static int get_option(const char *str)
    9.67 +{
    9.68 +	for(int i=0; optname[i], i++) {
    9.69 +		if(strcmp(str, optname[i]) == 0) {
    9.70 +			return i;
    9.71 +		}
    9.72 +	}
    9.73 +	return -1;
    9.74 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/opt.h	Tue Apr 07 11:16:56 2015 +0300
    10.3 @@ -0,0 +1,16 @@
    10.4 +#ifndef OPT_H_
    10.5 +#define OPT_H_
    10.6 +
    10.7 +struct Options {
    10.8 +	int xres, yres;
    10.9 +	float fov;
   10.10 +	bool fullscreen;
   10.11 +	bool use_vr;
   10.12 +};
   10.13 +
   10.14 +void default_opt(Options *opt);
   10.15 +bool parse_args(Options *opt, int argc, char **argv);
   10.16 +bool parse_cfg(Options *opt, const char *cfgfname);
   10.17 +bool write_cfg(const Options *opt, const char *cfgname);
   10.18 +
   10.19 +#endif	/* OPT_H_ */
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/rtarg.cc	Tue Apr 07 11:16:56 2015 +0300
    11.3 @@ -0,0 +1,135 @@
    11.4 +#include "opengl.h"
    11.5 +#include "rtarg.h"
    11.6 +
    11.7 +static int next_pow2(int x);
    11.8 +
    11.9 +RenderTarget::RenderTarget()
   11.10 +{
   11.11 +	xsz = ysz = 0;
   11.12 +	fbo = color = depth = 0;
   11.13 +
   11.14 +	tex_fmt = 0;
   11.15 +	tex_xsz = tex_ysz = 0;
   11.16 +}
   11.17 +
   11.18 +RenderTarget::~RenderTarget()
   11.19 +{
   11.20 +	destroy();
   11.21 +}
   11.22 +
   11.23 +bool RenderTarget::create(int xsz, int ysz)
   11.24 +{
   11.25 +	destroy();
   11.26 +
   11.27 +	this->xsz = xsz;
   11.28 +	this->ysz = ysz;
   11.29 +
   11.30 +	tex_xsz = next_pow2(xsz);
   11.31 +	tex_ysz = next_pow2(ysz);
   11.32 +	tex_fmt = check_extension("GL_ARB_texture_float") ? GL_RGB16F : GL_RGB;
   11.33 +
   11.34 +	glGenFramebuffers(1, &fbo);
   11.35 +	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
   11.36 +
   11.37 +	glGenTextures(1, &color);
   11.38 +	glBindTexture(GL_TEXTURE_2D, color);
   11.39 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   11.40 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   11.41 +	glTexImage2D(GL_TEXTURE_2D, 0, tex_fmt, tex_xsz, tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
   11.42 +	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
   11.43 +
   11.44 +	glGenRenderbuffers(1, &depth);
   11.45 +	glBindRenderbuffer(GL_RENDERBUFFER, depth);
   11.46 +	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
   11.47 +	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
   11.48 +
   11.49 +	return true;
   11.50 +}
   11.51 +
   11.52 +bool RenderTarget::resize(int xsz, int ysz)
   11.53 +{
   11.54 +	if(!fbo) {
   11.55 +		return create(xsz, ysz);
   11.56 +	}
   11.57 +
   11.58 +	this->xsz = xsz;
   11.59 +	this->ysz = ysz;
   11.60 +	int new_tex_xsz = next_pow2(xsz);
   11.61 +	int new_tex_ysz = next_pow2(ysz);
   11.62 +
   11.63 +	if(tex_xsz != new_tex_xsz || tex_ysz != new_tex_ysz) {
   11.64 +		glBindTexture(GL_TEXTURE_2D, color);
   11.65 +		glTexImage2D(GL_TEXTURE_2D, 0, tex_fmt, tex_xsz, tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
   11.66 +
   11.67 +		glBindRenderbuffer(GL_RENDERBUFFER, depth);
   11.68 +		glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
   11.69 +
   11.70 +		tex_xsz = new_tex_xsz;
   11.71 +		tex_ysz = new_tex_ysz;
   11.72 +	}
   11.73 +	return true;
   11.74 +}
   11.75 +
   11.76 +void RenderTarget::destroy()
   11.77 +{
   11.78 +	if(fbo) {
   11.79 +		glDeleteFramebuffers(1, &fbo);
   11.80 +	}
   11.81 +	if(color) {
   11.82 +		glDeleteTextures(1, &color);
   11.83 +	}
   11.84 +	if(depth) {
   11.85 +		glDeleteRenderbuffers(1, &depth);
   11.86 +	}
   11.87 +	fbo = color = depth = 0;
   11.88 +}
   11.89 +
   11.90 +bool RenderTarget::is_valid() const
   11.91 +{
   11.92 +	return fbo != 0;
   11.93 +}
   11.94 +
   11.95 +unsigned int RenderTarget::get_texture() const
   11.96 +{
   11.97 +	return color;
   11.98 +}
   11.99 +
  11.100 +int RenderTarget::get_width() const
  11.101 +{
  11.102 +	return xsz;
  11.103 +}
  11.104 +
  11.105 +int RenderTarget::get_height() const
  11.106 +{
  11.107 +	return ysz;
  11.108 +}
  11.109 +
  11.110 +int RenderTarget::get_tex_width() const
  11.111 +{
  11.112 +	return tex_xsz;
  11.113 +}
  11.114 +
  11.115 +int RenderTarget::get_tex_height() const
  11.116 +{
  11.117 +	return tex_ysz;
  11.118 +}
  11.119 +
  11.120 +void set_render_target(const RenderTarget *rtarg)
  11.121 +{
  11.122 +	if(rtarg) {
  11.123 +		glBindFramebuffer(GL_FRAMEBUFFER, rtarg->color);
  11.124 +	} else {
  11.125 +		glBindFramebuffer(GL_FRAMEBUFFER, 0);
  11.126 +	}
  11.127 +}
  11.128 +
  11.129 +static int next_pow2(int x)
  11.130 +{
  11.131 +	x--;
  11.132 +	x = (x >> 1) | x;
  11.133 +	x = (x >> 2) | x;
  11.134 +	x = (x >> 4) | x;
  11.135 +	x = (x >> 8) | x;
  11.136 +	x = (x >> 16) | x;
  11.137 +	return x + 1;
  11.138 +}
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/rtarg.h	Tue Apr 07 11:16:56 2015 +0300
    12.3 @@ -0,0 +1,33 @@
    12.4 +#ifndef RTARG_H_
    12.5 +#define RTARG_H_
    12.6 +
    12.7 +class RenderTarget {
    12.8 +private:
    12.9 +	int xsz, ysz, tex_xsz, tex_ysz;
   12.10 +	unsigned int fbo;
   12.11 +	unsigned int color, depth;
   12.12 +	unsigned int tex_fmt;
   12.13 +
   12.14 +public:
   12.15 +	RenderTarget();
   12.16 +	~RenderTarget();
   12.17 +
   12.18 +	bool create(int xsz, int ysz);
   12.19 +	bool resize(int xsz, int ysz);
   12.20 +	void destroy();
   12.21 +
   12.22 +	bool is_valid() const;
   12.23 +
   12.24 +	unsigned int get_texture() const;
   12.25 +
   12.26 +	int get_width() const;
   12.27 +	int get_height() const;
   12.28 +	int get_tex_width() const;
   12.29 +	int get_tex_height() const;
   12.30 +
   12.31 +	friend void set_render_target(const RenderTarget *rtarg);
   12.32 +};
   12.33 +
   12.34 +void set_render_target(const RenderTarget *rtarg);
   12.35 +
   12.36 +#endif	/* RTARG_H_ */
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/src/scene.cc	Tue Apr 07 11:16:56 2015 +0300
    13.3 @@ -0,0 +1,17 @@
    13.4 +#include "opengl.h"
    13.5 +#include "scene.h"
    13.6 +
    13.7 +void update_scene(long tmsec, float dt)
    13.8 +{
    13.9 +}
   13.10 +
   13.11 +void draw_scene()
   13.12 +{
   13.13 +	glBegin(GL_QUADS);
   13.14 +	glNormal3f(0, 1, 0);
   13.15 +	glVertex3f(-10, 0, 10);
   13.16 +	glVertex3f(10, 0, 10);
   13.17 +	glVertex3f(10, 0, -10);
   13.18 +	glVertex3f(-10, 0, -10);
   13.19 +	glEnd();
   13.20 +}
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/src/scene.h	Tue Apr 07 11:16:56 2015 +0300
    14.3 @@ -0,0 +1,7 @@
    14.4 +#ifndef SCENE_H_
    14.5 +#define SCENE_H_
    14.6 +
    14.7 +void update_scene(long tmsec, float dt);
    14.8 +void draw_scene();
    14.9 +
   14.10 +#endif	/* SCENE_H_ */
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/src/timer.c	Tue Apr 07 11:16:56 2015 +0300
    15.3 @@ -0,0 +1,23 @@
    15.4 +#include "timer.h"
    15.5 +
    15.6 +#if defined(__unix__) || defined(__APPLE__)
    15.7 +#include <time.h>
    15.8 +
    15.9 +unsigned long get_msec(void)
   15.10 +{
   15.11 +	struct timespec ts;
   15.12 +	if(clock_gettime(CLOCK_MONOTONIC, &ts) == -1) {
   15.13 +		clock_gettime(CLOCK_REALTIME, &ts);
   15.14 +	}
   15.15 +	return ts.tv_sec * 1000ul + ts.tv_nsec / 1000000ul;
   15.16 +}
   15.17 +
   15.18 +#elif defined(WIN32)
   15.19 +#include <windows.h>
   15.20 +
   15.21 +unsigned long get_msec(void)
   15.22 +{
   15.23 +	return timeGetTime();
   15.24 +}
   15.25 +
   15.26 +#endif
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/src/timer.h	Tue Apr 07 11:16:56 2015 +0300
    16.3 @@ -0,0 +1,15 @@
    16.4 +#ifndef TIMER_H_
    16.5 +#define TIMER_H_
    16.6 +
    16.7 +#ifdef __cplusplus
    16.8 +extern "C" {
    16.9 +#endif
   16.10 +
   16.11 +unsigned long get_msec(void);
   16.12 +
   16.13 +#ifdef __cplusplus
   16.14 +}
   16.15 +#endif
   16.16 +
   16.17 +
   16.18 +#endif	/* TIMER_H_ */