libgoatvr

changeset 30:1a8343ea54ce

fixed on windows
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Apr 2015 04:01:47 +0300
parents ddaa9c764030
children ec6edfe7774c
files example/src/main.c src/mesh.c src/opengl.c src/opengl.h src/vr_libovr.c
diffstat 5 files changed, 45 insertions(+), 88 deletions(-) [+]
line diff
     1.1 --- a/example/src/main.c	Wed Apr 08 02:32:22 2015 +0300
     1.2 +++ b/example/src/main.c	Sat Apr 11 04:01:47 2015 +0300
     1.3 @@ -57,6 +57,10 @@
     1.4  	int x, y;
     1.5  	unsigned int flags;
     1.6  
     1.7 +	if(vr_init() == -1) {
     1.8 +		return -1;
     1.9 +	}
    1.10 +
    1.11  	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    1.12  
    1.13  	x = y = SDL_WINDOWPOS_UNDEFINED;
    1.14 @@ -72,10 +76,6 @@
    1.15  
    1.16  	glewInit();
    1.17  
    1.18 -	if(vr_init() == -1) {
    1.19 -		return -1;
    1.20 -	}
    1.21 -
    1.22  	/* resize our window to match the HMD resolution */
    1.23  	win_width = vr_geti(VR_DISPLAY_WIDTH);
    1.24  	win_height = vr_geti(VR_DISPLAY_HEIGHT);
     2.1 --- a/src/mesh.c	Wed Apr 08 02:32:22 2015 +0300
     2.2 +++ b/src/mesh.c	Sat Apr 11 04:01:47 2015 +0300
     2.3 @@ -9,34 +9,10 @@
     2.4  		const float *dist_factors);
     2.5  static float barrel_scale(float rad, const float *k);
     2.6  
     2.7 -/* let's avoid a glew dependency in the library just for this */
     2.8 -#ifndef GL_ARRAY_BUFFER
     2.9 -#define GL_ARRAY_BUFFER			0x8892
    2.10 -#define GL_ELEMENT_ARRAY_BUFFER 0x8893
    2.11 -#define GL_STATIC_DRAW			0x88E4
    2.12 -#endif
    2.13 -
    2.14 -#ifndef GL_VERSION_1_5
    2.15 -static void (*glGenBuffers)(GLsizei, GLuint*);
    2.16 -static void (*glDeleteBuffers)(GLsizei, GLuint*);
    2.17 -static void (*glBufferData)(GLenum, unsigned int, const GLvoid*, GLenum);
    2.18 -static void (*glBindBuffer)(GLenum, GLuint);
    2.19 -#else
    2.20 -#ifndef GL_GLEXT_PROTOTYPES
    2.21 -
    2.22 -#ifndef GLAPI
    2.23 -#define GLAPI
    2.24 -#endif
    2.25 -#ifndef APIENTRY
    2.26 -#define APIENTRY
    2.27 -#endif
    2.28 -
    2.29 -GLAPI void APIENTRY glGenBuffers(GLsizei, GLuint*);
    2.30 -GLAPI void APIENTRY glDeleteBuffers(GLsizei, const GLuint*);
    2.31 -GLAPI void APIENTRY glBufferData();
    2.32 -GLAPI void APIENTRY glBindBuffer(GLenum, GLuint);
    2.33 -#endif
    2.34 -#endif
    2.35 +static PFNGLGENBUFFERSARBPROC gl_gen_buffers;
    2.36 +static PFNGLDELETEBUFFERSARBPROC gl_delete_buffers;
    2.37 +static PFNGLBUFFERDATAARBPROC gl_buffer_data;
    2.38 +static PFNGLBINDBUFFERARBPROC gl_bind_buffer;
    2.39  
    2.40  int vrimp_mesh_init(struct mesh *m)
    2.41  {
    2.42 @@ -47,19 +23,17 @@
    2.43  	m->num_verts = m->num_faces = 0;
    2.44  	m->vbo = m->ibo = 0;
    2.45  
    2.46 -#ifndef GL_VERSION_1_5
    2.47 -	if(!glGenBuffers) {
    2.48 -		glGenBuffers = vrimp_glfunc("glGenBuffersARB");
    2.49 -		glDeleteBuffers = vrimp_glfunc("glDeleteBuffersARB");
    2.50 -		glBufferData = vrimp_glfunc("glBufferDataARB");
    2.51 -		glBindBuffer = vrimp_glfunc("glBindBufferARB");
    2.52 +	if(!gl_gen_buffers) {
    2.53 +		gl_gen_buffers = (PFNGLGENBUFFERSARBPROC)vrimp_glfunc("glGenBuffersARB");
    2.54 +		gl_delete_buffers = (PFNGLDELETEBUFFERSARBPROC)vrimp_glfunc("glDeleteBuffersARB");
    2.55 +		gl_buffer_data = (PFNGLBUFFERDATAARBPROC)vrimp_glfunc("glBufferDataARB");
    2.56 +		gl_bind_buffer = (PFNGLBINDBUFFERARBPROC)vrimp_glfunc("glBindBufferARB");
    2.57  
    2.58 -		if(!(glGenBuffers && glDeleteBuffers && glBufferData && glBindBuffer)) {
    2.59 +		if(!(gl_gen_buffers && gl_delete_buffers && gl_buffer_data && gl_bind_buffer)) {
    2.60  			fprintf(stderr, "Failed to load VBO functions\n");
    2.61  			return -1;
    2.62  		}
    2.63  	}
    2.64 -#endif
    2.65  
    2.66  	return 0;
    2.67  }
    2.68 @@ -69,8 +43,8 @@
    2.69  	free(m->varr);
    2.70  	free(m->iarr);
    2.71  
    2.72 -	if(m->vbo) glDeleteBuffers(1, &m->vbo);
    2.73 -	if(m->ibo) glDeleteBuffers(1, &m->ibo);
    2.74 +	if(m->vbo) gl_delete_buffers(1, &m->vbo);
    2.75 +	if(m->ibo) gl_delete_buffers(1, &m->ibo);
    2.76  }
    2.77  
    2.78  void vrimp_mesh_draw(struct mesh *m)
    2.79 @@ -78,15 +52,15 @@
    2.80  	glEnableClientState(GL_VERTEX_ARRAY);
    2.81  	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    2.82  
    2.83 -	glBindBuffer(GL_ARRAY_BUFFER, m->vbo);
    2.84 +	gl_bind_buffer(GL_ARRAY_BUFFER, m->vbo);
    2.85  	glVertexPointer(3, GL_FLOAT, sizeof(struct vertex), 0);
    2.86  	glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), (void*)offsetof(struct vertex, tx));
    2.87  
    2.88 -	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
    2.89 +	gl_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
    2.90  	glDrawElements(GL_TRIANGLES, m->num_faces * 3, GL_UNSIGNED_INT, 0);
    2.91  
    2.92 -	glBindBuffer(GL_ARRAY_BUFFER, 0);
    2.93 -	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    2.94 +	gl_bind_buffer(GL_ARRAY_BUFFER, 0);
    2.95 +	gl_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    2.96  
    2.97  	glDisableClientState(GL_VERTEX_ARRAY);
    2.98  	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    2.99 @@ -157,12 +131,12 @@
   2.100  		}
   2.101  	}
   2.102  
   2.103 -	glGenBuffers(1, &m->vbo);
   2.104 -	glGenBuffers(1, &m->ibo);
   2.105 -	glBindBuffer(GL_ARRAY_BUFFER, m->vbo);
   2.106 -	glBufferData(GL_ARRAY_BUFFER, num_verts * sizeof *varr, varr, GL_STATIC_DRAW);
   2.107 -	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
   2.108 -	glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_tris * 3 * sizeof *iarr, iarr, GL_STATIC_DRAW);
   2.109 +	gl_gen_buffers(1, &m->vbo);
   2.110 +	gl_gen_buffers(1, &m->ibo);
   2.111 +	gl_bind_buffer(GL_ARRAY_BUFFER, m->vbo);
   2.112 +	gl_buffer_data(GL_ARRAY_BUFFER, num_verts * sizeof *varr, varr, GL_STATIC_DRAW);
   2.113 +	gl_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
   2.114 +	gl_buffer_data(GL_ELEMENT_ARRAY_BUFFER, num_tris * 3 * sizeof *iarr, iarr, GL_STATIC_DRAW);
   2.115  
   2.116  	m->prim = GL_TRIANGLES;
   2.117  	m->num_verts = num_verts;
     3.1 --- a/src/opengl.c	Wed Apr 08 02:32:22 2015 +0300
     3.2 +++ b/src/opengl.c	Sat Apr 11 04:01:47 2015 +0300
     3.3 @@ -7,11 +7,6 @@
     3.4  	Drawable win = glXGetCurrentDrawable();
     3.5  	glXSwapBuffers(dpy, win);
     3.6  }
     3.7 -
     3.8 -void (*vrimp_glfunc(const char *name))()
     3.9 -{
    3.10 -	return glXGetProcAddress((const unsigned char*)name);
    3.11 -}
    3.12  #endif	/* __unix__ */
    3.13  
    3.14  #ifdef WIN32
    3.15 @@ -20,11 +15,6 @@
    3.16  	HDC dc = wglGetCurrentDC();
    3.17  	SwapBuffers(dc);
    3.18  }
    3.19 -
    3.20 -void (*vrimp_glfunc(const char *name))()
    3.21 -{
    3.22 -	return (void (*)())wglGetProcAddress(name);
    3.23 -}
    3.24  #endif	/* WIN32 */
    3.25  
    3.26  #ifdef __APPLE__
    3.27 @@ -34,11 +24,4 @@
    3.28  	 * to a GLView class or whatever the fuck it's called... investigate further
    3.29  	 */
    3.30  }
    3.31 -
    3.32 -
    3.33 -void (*vrimp_glfunc(const char *name))()
    3.34 -{
    3.35 -	/* TODO: whatever */
    3.36 -	return 0;
    3.37 -}
    3.38  #endif	/* __APPLE__ */
     4.1 --- a/src/opengl.h	Wed Apr 08 02:32:22 2015 +0300
     4.2 +++ b/src/opengl.h	Sat Apr 11 04:01:47 2015 +0300
     4.3 @@ -13,12 +13,18 @@
     4.4  #include <GL/gl.h>
     4.5  #endif
     4.6  
     4.7 +#include "glext.h"
     4.8 +
     4.9  #ifdef __unix__
    4.10  #include <GL/glx.h>
    4.11  #endif
    4.12  
    4.13  void vrimp_swap_buffers(void);
    4.14  
    4.15 -void (*vrimp_glfunc(const char *name))();
    4.16 +#if defined(__unix__)
    4.17 +#define vrimp_glfunc(n) glXGetProcAddress(n)
    4.18 +#elif defined(WIN32)
    4.19 +#define vrimp_glfunc(n)	wglGetProcAddress(n)
    4.20 +#endif
    4.21  
    4.22  #endif	/* VR_OPENGL_H_ */
     5.1 --- a/src/vr_libovr.c	Wed Apr 08 02:32:22 2015 +0300
     5.2 +++ b/src/vr_libovr.c	Sat Apr 11 04:01:47 2015 +0300
     5.3 @@ -13,20 +13,13 @@
     5.4  #include <stdlib.h>
     5.5  #include <string.h>
     5.6  #include <assert.h>
     5.7 +#include "opengl.h"
     5.8  #include "opt.h"
     5.9  
    5.10  #include <OVR_CAPI.h>
    5.11  #include <OVR_CAPI_GL.h>
    5.12  
    5.13 -#ifdef OVR_OS_LINUX
    5.14 -#include <GL/glx.h>
    5.15 -#endif
    5.16 -
    5.17 -/* undef this if you want the retarded health and safety warning screen */
    5.18 -#undef DISABLE_RETARDED_HEALTH_WARNING
    5.19 -
    5.20 -/* just dropping the prototype here to avoid including CAPI_HSWDisplay.h */
    5.21 -OVR_EXPORT void ovrhmd_EnableHSWDisplaySDKRender(ovrHmd hmd, ovrBool enabled);
    5.22 +static PFNGLUSEPROGRAMPROC gl_use_program;
    5.23  
    5.24  static ovrHmd hmd;
    5.25  static void *optdb;
    5.26 @@ -111,6 +104,10 @@
    5.27  		set_option_int(optdb, VR_WIN_YOFFS, hmd->WindowsPos.y);
    5.28  	}
    5.29  
    5.30 +	if(!(gl_use_program = (PFNGLUSEPROGRAMPROC)vrimp_glfunc("glUseProgram"))) {
    5.31 +		gl_use_program = (PFNGLUSEPROGRAMPROC)vrimp_glfunc("glUseProgramObjectARB");
    5.32 +	}
    5.33 +
    5.34  	deferred_init_done = 0;
    5.35  	return 0;
    5.36  }
    5.37 @@ -177,11 +174,6 @@
    5.38  		set_option_vec(optdb, VR_LEYE_OFFSET, leye_offs);
    5.39  		set_option_vec(optdb, VR_REYE_OFFSET, reye_offs);
    5.40  	}
    5.41 -
    5.42 -
    5.43 -#ifdef DISABLE_RETARDED_HEALTH_WARNING
    5.44 -	ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
    5.45 -#endif
    5.46  }
    5.47  
    5.48  static void cleanup(void)
    5.49 @@ -314,17 +306,19 @@
    5.50  
    5.51  static int present(void)
    5.52  {
    5.53 -	int cur_prog;
    5.54 +	int cur_prog = 0;
    5.55  
    5.56  	if(!hmd) return 0;
    5.57  
    5.58 -	glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
    5.59 +	if(gl_use_program) {
    5.60 +		glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
    5.61 +	}
    5.62  
    5.63  	ovrHmd_EndFrame(hmd, pose, &eye_tex[0].Texture);
    5.64  	inside_begin_end = 0;
    5.65  
    5.66 -	if(cur_prog) {
    5.67 -		/*glUseProgram(0);*/
    5.68 +	if(gl_use_program) {
    5.69 +		gl_use_program(cur_prog);
    5.70  	}
    5.71  
    5.72  	return 1;