31 #if defined(VMDOPENGL) 35 #if defined(USEOPENGL) 41 #define GL_GLEXT_PROTOTYPES 1 42 #define GLX_GLXEXT_PROTOTYPES 1 44 #if (defined(WIN32) || defined(_WIN64)) && defined(_MSC_VER) 49 #define _WIN32_WINNT 0x0400 55 #if defined(VMDSPACEWARE) && (defined(WIN32) || defined(_WIN64)) 65 #include <X11/keysym.h> 66 #include <X11/Xatom.h> 89 #if (defined(__linux) || defined(_MSC_VER)) 94 #if 0 && defined(__APPLE__) 95 #include <OpenGL/glext.h> 114 #if (defined(WIN32) || defined(_WIN64)) && defined(_MSC_VER) 115 #if defined(USESPACEWARE) 119 SiGetEventData spwedata;
129 Atom ev_button_press;
130 Atom ev_button_release;
150 #if !(defined(WIN32) || defined(_WIN64)) 154 GLhandleARB ProgramObject;
155 GLhandleARB VertexShaderObject;
156 GLhandleARB FragmentShaderObject;
167 int hasglshaderobjectsarb;
168 int hasglvertexshaderarb;
169 int hasglfragmentshaderarb;
170 int hasglgeometryshader4arb;
171 int hasglsampleshadingarb;
172 int hasglshadinglangarb;
173 int hasglfborendertarget;
174 int hasgetvideosyncsgi;
177 #if defined(GL_ARB_shader_objects) 179 GLhandleARB (APIENTRY *p_glCreateShaderObjectARB)(GLenum shaderType);
180 GLhandleARB (APIENTRY *p_glCreateProgramObjectARB)(void);
181 void (APIENTRY *p_glUseProgramObjectARB)(GLhandleARB programObj);
182 void (APIENTRY *p_glDetachObjectARB)(GLhandleARB containerObj, GLhandleARB attachedObj);
183 void (APIENTRY *p_glGetInfoLogARB)(GLhandleARB obj,GLsizei maxLength, GLsizei *
length, GLcharARB *infoLog);
184 void (APIENTRY *p_glGetObjectParameterivARB)(GLhandleARB obj, GLenum pname, GLint *params);
185 void (APIENTRY *p_glLinkProgramARB)(GLhandleARB programObj);
186 void (APIENTRY *p_glDeleteObjectARB)(GLhandleARB obj);
187 void (APIENTRY *p_glAttachObjectARB)(GLhandleARB containerObj, GLhandleARB obj);
188 void (APIENTRY *p_glCompileShaderARB)(GLhandleARB shaderObj);
189 void (APIENTRY *p_glShaderSourceARB)(GLhandleARB shaderObj, GLsizei count,
const GLcharARB **strings,
const GLint *
length);
190 GLint (APIENTRY *p_glGetUniformLocationARB)(GLhandleARB programObject,
const GLcharARB *name);
191 void (APIENTRY *p_glUniform1iARB)(GLint location, GLint v0);
192 void (APIENTRY *p_glUniform1fvARB)(GLint location, GLsizei count, GLfloat *value);
193 void (APIENTRY *p_glUniform2fvARB)(GLint location, GLsizei count, GLfloat *value);
194 void (APIENTRY *p_glUniform3fvARB)(GLint location, GLsizei count, GLfloat *value);
195 void (APIENTRY *p_glUniform4fvARB)(GLint location, GLsizei count, GLfloat *value);
198 void (APIENTRY *p_glGenFramebuffers)(GLsizei n, GLuint * framebuffers);
199 void (APIENTRY *p_glBindFramebuffer)(GLenum target, GLuint framebuffer);
200 void (APIENTRY *p_glGenRenderbuffers)(GLsizei n, GLuint * renderbuffers);
201 void (APIENTRY *p_glBindRenderbuffer)(GLenum target, GLuint renderbuffer);
202 void (APIENTRY *p_glRenderbufferStorage)(GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
203 void (APIENTRY *p_glFramebufferTexture2D)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
204 void (APIENTRY *p_glFramebufferRenderbuffer)(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer);
205 GLenum (APIENTRY *p_glCheckFramebufferStatus)(GLenum target);
206 void (APIENTRY *p_glDeleteRenderbuffers)(GLsizei n,
const GLuint * renderbuffers);
207 void (APIENTRY *p_glDeleteFramebuffers)(GLsizei n,
const GLuint * framebuffers);
208 void (APIENTRY *p_glDrawBuffers)(GLsizei n,
const GLenum *bufs);
212 int (APIENTRY *p_glXGetVideoSyncSGI)(GLuint *count);
216 #if defined(GL_ARB_shader_objects) 218 #define GLCREATESHADEROBJECTARB ext->p_glCreateShaderObjectARB 219 #define GLCREATEPROGRAMOBJECTARB ext->p_glCreateProgramObjectARB 220 #define GLUSEPROGRAMOBJECTARB ext->p_glUseProgramObjectARB 221 #define GLDETACHOBJECTARB ext->p_glDetachObjectARB 222 #define GLGETINFOLOGARB ext->p_glGetInfoLogARB 223 #define GLGETOBJECTPARAMETERIVARB ext->p_glGetObjectParameterivARB 224 #define GLLINKPROGRAMARB ext->p_glLinkProgramARB 225 #define GLDELETEOBJECTARB ext->p_glDeleteObjectARB 226 #define GLATTACHOBJECTARB ext->p_glAttachObjectARB 227 #define GLCOMPILESHADERARB ext->p_glCompileShaderARB 228 #define GLSHADERSOURCEARB ext->p_glShaderSourceARB 229 #define GLGETUNIFORMLOCATIONARB ext->p_glGetUniformLocationARB 230 #define GLUNIFORM1IARB ext->p_glUniform1iARB 231 #define GLUNIFORM1FVARB ext->p_glUniform1fvARB 232 #define GLUNIFORM2FVARB ext->p_glUniform2fvARB 233 #define GLUNIFORM3FVARB ext->p_glUniform3fvARB 234 #define GLUNIFORM4FVARB ext->p_glUniform4fvARB 237 #define GLGENFRAMEBUFFERS ext->p_glGenFramebuffers 238 #define GLBINDFRAMEBUFFER ext->p_glBindFramebuffer 239 #define GLGENRENDERBUFFERS ext->p_glGenRenderbuffers 240 #define GLBINDRENDERBUFFER ext->p_glBindRenderbuffer 241 #define GLRENDERBUFFERSTORAGE ext->p_glRenderbufferStorage 242 #define GLFRAMEBUFFERTEXTURE2D ext->p_glFramebufferTexture2D 243 #define GLFRAMEBUFFERRENDERBUFFER ext->p_glFramebufferRenderbuffer 244 #define GLCHECKFRAMEBUFFERSTATUS ext->p_glCheckFramebufferStatus 245 #define GLDELETERENDERBUFFERS ext->p_glDeleteRenderbuffers 246 #define GLDELETEFRAMEBUFFERS ext->p_glDeleteFramebuffers 247 #define GLDRAWBUFFERS ext->p_glDrawBuffers 250 #define GLXGETVIDEOSYNCSGI ext->p_glXGetVideoSyncSGI 256 #if (defined(WIN32) || defined(_WIN64)) && defined(_MSC_VER) 293 spaceballhandle *sball;
294 spaceballevent sballevent;
296 glwin_ext_fctns *ext;
300 #if defined(USEOPENGL) && !defined(USEEGL) && !(defined(WIN32) || defined(_WIN64)) && !defined(_MSC_VER) 301 static int glx_query_extension(Display *dpy,
const char *extname) {
308 ext = (
char *) glXQueryExtensionsString(dpy, 0);
310 endext = ext + strlen(ext);
311 while (ext < endext) {
312 size_t n = strcspn(ext,
" ");
313 if ((strlen(extname) == n) && (strncmp(extname, ext, n) == 0)) {
327 static void quat_rot_matrix(
float *m,
const float *q) {
328 m[ 0] = 1.0f - 2.0f * (q[1] * q[1] + q[2] * q[2]);
329 m[ 1] = 2.0f * (q[0] * q[1] - q[2] * q[3]);
330 m[ 2] = 2.0f * (q[2] * q[0] + q[1] * q[3]);
333 m[ 4] = 2.0f * (q[0] * q[1] + q[2] * q[3]);
334 m[ 5] = 1.0f - 2.0f * (q[2] * q[2] + q[0] * q[0]);
335 m[ 6] = 2.0f * (q[1] * q[2] - q[0] * q[3]);
338 m[ 8] = 2.0f * (q[2] * q[0] - q[1] * q[3]);
339 m[ 9] = 2.0f * (q[1] * q[2] + q[0] * q[3]);
340 m[10] = 1.0f - 2.0f * (q[1] * q[1] + q[0] * q[0]);
351 typedef void (APIENTRY *glwin_fctnptr)(void);
354 void * glwin_get_procaddress(
const char * procname) {
359 #if defined(_MSC_VER) 364 fctn = (glwin_fctnptr) wglGetProcAddress((LPCSTR) procname);
367 #if !defined(_MSC_VER) && !defined(__APPLE__) 371 fctn = glXGetProcAddressARB((
const GLubyte *) procname);
373 printf(
"GL fctn '%s' %s\n", procname, (fctn) ?
"available" :
"NULL");
378 #if defined(GLX_ARB_get_proc_address) 384 fctn = glXGetProcAddressARB((
const GLubyte *) procname);
386 printf(
"GLARB fctn '%s' %s\n", procname, (fctn) ?
"available" :
"NULL");
393 printf(
"GL fctn '%s' %s\n", procname, (fctn) ?
"available" :
"NULL");
401 void glwin_init_exts(
void * voidhandle) {
402 oglhandle * handle = (oglhandle *) voidhandle;
405 glwin_ext_fctns *ext = handle->ext;
409 memset(ext, 0,
sizeof(glwin_ext_fctns));
411 #if defined(GL_ARB_shading_language_100) 414 ext->hasglshadinglangarb = 1;
418 #if defined(GL_ARB_shader_objects) 420 ext->p_glCreateShaderObjectARB = (GLhandleARB (APIENTRY *)(GLenum)) glwin_get_procaddress(
"glCreateShaderObjectARB");
421 ext->p_glCreateProgramObjectARB = (GLhandleARB (APIENTRY *)(void)) glwin_get_procaddress(
"glCreateProgramObjectARB");
422 ext->p_glUseProgramObjectARB = (void (APIENTRY *)(GLhandleARB)) glwin_get_procaddress(
"glUseProgramObjectARB");
423 ext->p_glDetachObjectARB = (void (APIENTRY *)(GLhandleARB, GLhandleARB)) glwin_get_procaddress(
"glDetachObjectARB");
424 ext->p_glGetInfoLogARB = (void (APIENTRY *)(GLhandleARB, GLsizei, GLsizei *, GLcharARB *)) glwin_get_procaddress(
"glGetInfoLogARB");
425 ext->p_glGetObjectParameterivARB = (void (APIENTRY *)(GLhandleARB, GLenum, GLint *)) glwin_get_procaddress(
"glGetObjectParameterivARB");
426 ext->p_glLinkProgramARB = (void (APIENTRY *)(GLhandleARB)) glwin_get_procaddress(
"glLinkProgramARB");
427 ext->p_glDeleteObjectARB = (void (APIENTRY *)(GLhandleARB)) glwin_get_procaddress(
"glDeleteObjectARB");
428 ext->p_glAttachObjectARB = (void (APIENTRY *)(GLhandleARB, GLhandleARB)) glwin_get_procaddress(
"glAttachObjectARB");
429 ext->p_glCompileShaderARB = (void (APIENTRY *)(GLhandleARB)) glwin_get_procaddress(
"glCompileShaderARB");
430 ext->p_glShaderSourceARB = (void (APIENTRY *)(GLhandleARB, GLsizei,
const GLcharARB **,
const GLint *)) glwin_get_procaddress(
"glShaderSourceARB");
431 ext->p_glGetUniformLocationARB = (GLint (APIENTRY *)(GLhandleARB programObject,
const GLcharARB *name)) glwin_get_procaddress(
"glGetUniformLocationARB");
432 ext->p_glUniform1iARB = (void (APIENTRY *)(GLint location, GLint v0)) glwin_get_procaddress(
"glUniform1iARB");
433 ext->p_glUniform1fvARB = (void (APIENTRY *)(GLint location, GLsizei count, GLfloat *value)) glwin_get_procaddress(
"glUniform1fvARB");
434 ext->p_glUniform2fvARB = (void (APIENTRY *)(GLint location, GLsizei count, GLfloat *value)) glwin_get_procaddress(
"glUniform2fvARB");
435 ext->p_glUniform3fvARB = (void (APIENTRY *)(GLint location, GLsizei count, GLfloat *value)) glwin_get_procaddress(
"glUniform3fvARB");
436 ext->p_glUniform4fvARB = (void (APIENTRY *)(GLint location, GLsizei count, GLfloat *value)) glwin_get_procaddress(
"glUniform4fvARB");
438 if (ext->p_glCreateShaderObjectARB != NULL && ext->p_glCreateProgramObjectARB != NULL &&
439 ext->p_glUseProgramObjectARB != NULL && ext->p_glDetachObjectARB != NULL &&
440 ext->p_glGetInfoLogARB != NULL && ext->p_glGetObjectParameterivARB != NULL &&
441 ext->p_glLinkProgramARB != NULL && ext->p_glDeleteObjectARB != NULL &&
442 ext->p_glAttachObjectARB != NULL && ext->p_glCompileShaderARB != NULL &&
443 ext->p_glShaderSourceARB != NULL && ext->p_glGetUniformLocationARB != NULL &&
444 ext->p_glUniform1iARB != NULL && ext->p_glUniform1fvARB != NULL &&
445 ext->p_glUniform2fvARB != NULL && ext->p_glUniform3fvARB != NULL &&
446 ext->p_glUniform4fvARB != NULL) {
447 ext->hasglshaderobjectsarb = 1;
452 #if defined(GL_ARB_vertex_shader) 454 ext->hasglvertexshaderarb = 1;
458 #if defined(GL_ARB_fragment_shader) 460 ext->hasglfragmentshaderarb = 1;
464 #if defined(GL_ARB_geometry_shader4) 466 ext->hasglgeometryshader4arb = 1;
471 ext->hasglsampleshadingarb = 1;
474 #if defined(GL_ARB_framebuffer_object) 476 ext->p_glGenFramebuffers = (void (APIENTRY *)(GLsizei n, GLuint * framebuffers)) glwin_get_procaddress(
"glGenFramebuffers");
477 ext->p_glBindFramebuffer = (void (APIENTRY *)(GLenum target, GLuint framebuffer)) glwin_get_procaddress(
"glBindFramebuffer");
478 ext->p_glGenRenderbuffers = (void (APIENTRY *)(GLsizei n, GLuint * renderbuffers)) glwin_get_procaddress(
"glGenRenderbuffers");
479 ext->p_glBindRenderbuffer = (void (APIENTRY *)(GLenum target, GLuint renderbuffer)) glwin_get_procaddress(
"glBindRenderbuffer");
480 ext->p_glRenderbufferStorage = (void (APIENTRY *)(GLenum target, GLenum internalformat, GLsizei width, GLsizei height)) glwin_get_procaddress(
"glRenderbufferStorage");
481 ext->p_glFramebufferTexture2D = (void (APIENTRY *)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)) glwin_get_procaddress(
"glFramebufferTexture2D");
482 ext->p_glFramebufferRenderbuffer = (void (APIENTRY *)(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)) glwin_get_procaddress(
"glFramebufferRenderbuffer");
483 ext->p_glCheckFramebufferStatus = (GLenum (APIENTRY *)(GLenum target)) glwin_get_procaddress(
"glCheckFramebufferStatus");
484 ext->p_glDeleteRenderbuffers = (void (APIENTRY *)(GLsizei n,
const GLuint * renderbuffers)) glwin_get_procaddress(
"glDeleteRenderbuffers");
485 ext->p_glDeleteFramebuffers = (void (APIENTRY *)(GLsizei n,
const GLuint * framebuffers)) glwin_get_procaddress(
"glDeleteFramebuffers");
486 ext->p_glDrawBuffers = (void (APIENTRY *)(GLsizei n,
const GLenum *bufs)) glwin_get_procaddress(
"glDrawBuffers");
488 if (ext->p_glGenFramebuffers != NULL &&
489 ext->p_glBindFramebuffer != NULL &&
490 ext->p_glGenRenderbuffers != NULL &&
491 ext->p_glBindRenderbuffer != NULL &&
492 ext->p_glRenderbufferStorage != NULL &&
493 ext->p_glFramebufferTexture2D != NULL &&
494 ext->p_glFramebufferRenderbuffer != NULL &&
495 ext->p_glCheckFramebufferStatus != NULL &&
496 ext->p_glDeleteRenderbuffers != NULL &&
497 ext->p_glDeleteFramebuffers != NULL &&
498 ext->p_glDrawBuffers != NULL) {
499 ext->hasglfborendertarget = 1;
503 #if !(defined(WIN32) || defined(_WIN64)) 504 if (glx_query_extension(handle->dpy,
"GLX_SGI_video_sync")) {
505 ext->p_glXGetVideoSyncSGI = (int (APIENTRY *)(GLuint *count)) glwin_get_procaddress(
"glXGetVideoSyncSGI");
507 if (ext->p_glXGetVideoSyncSGI != NULL)
508 ext->hasgetvideosyncsgi = 1;
515 #if !(defined(WIN32) || defined(_WIN64)) && !defined(_MSC_VER) 523 #define SBALL_COMMAND_NONE 0 524 #define SBALL_COMMAND_APP_WINDOW 27695 525 #define SBALL_COMMAND_APP_SENSITIVITY 27696 528 static spaceballhandle * spaceball_attach(Display *dpy, Window win) {
530 spaceballhandle *handle = (spaceballhandle *) calloc(1,
sizeof(spaceballhandle));
533 handle->ev_motion = XInternAtom(dpy,
"MotionEvent", True);
534 handle->ev_button_press = XInternAtom(dpy,
"ButtonPressEvent", True);
535 handle->ev_button_release = XInternAtom(dpy,
"ButtonReleaseEvent", True);
536 handle->ev_command = XInternAtom(dpy,
"CommandEvent", True);
538 if (!handle->ev_motion || !handle->ev_button_press ||
539 !handle->ev_button_release || !handle->ev_command) {
545 Window root = RootWindow(dpy, DefaultScreen(dpy));
550 unsigned long NItems, BytesReturn;
551 unsigned char *PropReturn = NULL;
552 XGetWindowProperty(dpy, root, handle->ev_command, 0, 1, 0,
553 AnyPropertyType, &ActualType, &ActualFormat, &NItems,
554 &BytesReturn, &PropReturn );
555 if (PropReturn == NULL) {
559 handle->drv_win = *(Window *) PropReturn;
562 XTextProperty sball_drv_winname;
563 if (XGetWMName(dpy, handle->drv_win, &sball_drv_winname) != 0) {
564 if (!strcmp(
"Magellan Window", (
char *) sball_drv_winname.value)) {
567 msg.type = ClientMessage;
568 msg.xclient.format = 16;
569 msg.xclient.send_event = 0;
570 msg.xclient.display = dpy;
571 msg.xclient.window = handle->drv_win;
572 msg.xclient.message_type = handle->ev_command;
574 msg.xclient.data.s[0] = (short) (((win)>>16)&0x0000FFFF);
575 msg.xclient.data.s[1] = (short) (((win)) &0x0000FFFF);
576 msg.xclient.data.s[2] = SBALL_COMMAND_APP_WINDOW;
578 int rc = XSendEvent(dpy, handle->drv_win, 0, 0x0000, &msg);
586 XFree(sball_drv_winname.value);
593 static void spaceball_close(spaceballhandle *handle) {
598 static int spaceball_decode_event(spaceballhandle *handle,
const XEvent *xev, spaceballevent *sballevent) {
601 if (handle == NULL || xev == NULL || sballevent == NULL)
604 if (xev->type != ClientMessage)
607 evtype = xev->xclient.message_type;
608 if (evtype == handle->ev_motion) {
611 sballevent->tx += xev->xclient.data.s[2];
612 sballevent->ty += xev->xclient.data.s[3];
613 sballevent->tz += xev->xclient.data.s[4];
614 sballevent->rx += xev->xclient.data.s[5];
615 sballevent->ry += xev->xclient.data.s[6];
616 sballevent->rz += xev->xclient.data.s[7];
617 sballevent->period += xev->xclient.data.s[8];
618 sballevent->event = 1;
620 }
else if (evtype == handle->ev_button_press) {
621 sballevent->buttons |= (1 << xev->xclient.data.s[2]);
622 sballevent->event = 1;
624 }
else if (evtype == handle->ev_button_release) {
625 sballevent->buttons &= ~(1 << xev->xclient.data.s[2]);
626 sballevent->event = 1;
634 static void spaceball_init_event(spaceballevent *sballevent) {
635 memset(sballevent, 0,
sizeof(spaceballevent));
639 static void spaceball_clear_event(spaceballevent *sballevent) {
646 sballevent->period = 0;
647 sballevent->event = 0;
651 static oglhandle *glwin_alloc_init(
void) {
652 oglhandle * handle = (oglhandle *) calloc(1,
sizeof(oglhandle));
656 #if defined(VMDOPENGL) 659 if (getenv(
"VMDGDISPLAY") != NULL) {
660 handle->dpy = XOpenDisplay(getenv(
"VMDGDISPLAY"));
662 handle->dpy = XOpenDisplay(getenv(
"DISPLAY"));
665 handle->dpy = XOpenDisplay(getenv(
"DISPLAY"));
667 if (handle->dpy == NULL) {
672 handle->scrnum = DefaultScreen(handle->dpy);
673 handle->root = RootWindow(handle->dpy, handle->scrnum);
674 handle->havestencil = 0;
675 handle->instereo = 0;
677 handle->havefocus = 0;
679 handle->ext = (glwin_ext_fctns *) calloc(1,
sizeof(glwin_ext_fctns));
688 void *
glwin_create(
const char * wintitle,
int width,
int height) {
692 XSetWindowAttributes attr;
695 XVisualInfo vistemplate;
696 XVisualInfo *vis=NULL;
697 XSizeHints sizeHints;
699 EGLint eglmaj, eglmin;
702 #if defined(USEOPENGLES2) 703 #define GLWIN_RENDERABLE_TYPE EGL_OPENGL_ES2_BIT 705 #define GLWIN_RENDERABLE_TYPE EGL_OPENGL_BIT 708 EGLint eglnormalattrib[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8,
713 EGL_RENDERABLE_TYPE, GLWIN_RENDERABLE_TYPE,
716 EGLint eglfailsafeattrib[] = { EGL_RED_SIZE, 1, EGL_GREEN_SIZE, 1,
720 EGL_RENDERABLE_TYPE, GLWIN_RENDERABLE_TYPE,
724 handle = glwin_alloc_init();
725 handle->width = width;
726 handle->height = height;
730 handle->egldpy = eglGetDisplay(handle->dpy);
732 handle->egldpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
734 if (handle->egldpy == EGL_NO_DISPLAY) {
735 printf(
"glwin_create(): Failed to connect to EGL display\n");
740 if (eglInitialize(handle->egldpy, &eglmaj, &eglmin) == EGL_FALSE) {
741 printf(
"glwin_create(): Failed to initialize EGL display connection\n");
746 printf(
"EGL init dpy version: %d.%d\n", eglmaj, eglmin);
751 handle->havestencil = 1;
752 handle->instereo = 0;
753 if (eglChooseConfig(handle->egldpy, eglnormalattrib, &handle->eglconf, 1, &num_config) == EGL_FALSE) {
754 printf(
"eglChooseConfig(1) %d configs\n", num_config);
755 if (eglChooseConfig(handle->egldpy, eglfailsafeattrib, &handle->eglconf, 1, &num_config) == EGL_FALSE) {
756 printf(
"Error: eglChooseConfig() failed\n");
760 handle->havestencil = 0;
762 printf(
"eglChooseConfig() %d configs\n", num_config);
766 if (eglGetConfigAttrib(handle->egldpy, handle->eglconf, EGL_NATIVE_VISUAL_ID, &vid) == EGL_FALSE) {
767 printf(
"Error: eglGetConfigAttrib() failed\n");
771 vistemplate.visualid = vid;
772 vis = XGetVisualInfo(handle->dpy, VisualIDMask, &vistemplate, &num_visuals);
774 printf(
"Error: failed to obtain EGL-compatible X visual...\n");
780 #if defined(USEOPENGLES2) 782 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
783 printf(
"Error: failed to bind OpenGL ES API\n");
789 if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE) {
790 printf(
"Error: failed to bind full OpenGL API\n");
798 attr.background_pixel = 0;
799 attr.border_pixel = 0;
800 attr.colormap = XCreateColormap(handle->dpy, handle->root,
801 vis->visual, AllocNone);
803 attr.event_mask = StructureNotifyMask | ExposureMask;
804 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
806 handle->win = XCreateWindow(handle->dpy, handle->root, 0, 0, width, height,
807 0, vis->depth, InputOutput,
808 vis->visual, mask, &attr );
812 Atom bypasscomp = X11_XInternAtom(handle->dpy,
"_NET_WM_BYPASS_COMPOSITOR", False);
813 const long bypasscomp_on = 1;
814 X11_XChangeProperty(handle->dpy, handle->win, bypasscomp, XA_CARDINAL, 32,
815 PropModeReplace, (
unsigned char *) bypasscomp_on, 1);
818 handle->eglctx = eglCreateContext(handle->dpy, handle->eglconf, EGL_NO_CONTEXT, NULL);
820 handle->eglsurf = eglCreateWindowSurface(handle->egldpy, handle->eglconf, handle->win, NULL);
821 eglMakeCurrent(handle->dpy, handle->eglsurf, handle->eglsurf, handle->eglctx);
824 glwin_init_exts(handle);
826 EGLint Context_RendererType=0;
827 eglQueryContext(handle->egldpy, handle->eglctx, EGL_CONTEXT_CLIENT_TYPE, &Context_RendererType);
829 const char *glstring=
"uninitialized";
831 switch (Context_RendererType) {
832 case EGL_OPENGL_API: glstring =
"OpenGL";
break;
833 case EGL_OPENGL_ES_API: glstring =
"OpenGL ES";
break;
834 case EGL_OPENVG_API: glstring =
"OpenVG???";
break;
836 sprintf(buf,
"Unknown API: %x", Context_RendererType);
840 printf(
"EGL_CONTEXT_CLIENT_TYPE: %s\n", glstring);
843 XStoreName(handle->dpy, handle->win, wintitle);
845 XSelectInput(handle->dpy, handle->win,
846 KeyPressMask | ButtonPressMask | ButtonReleaseMask |
847 PointerMotionMask | StructureNotifyMask | ExposureMask |
848 EnterWindowMask | LeaveWindowMask | FocusChangeMask);
851 memset((
void *) &(sizeHints), 0,
sizeof(sizeHints));
852 sizeHints.flags |= USSize;
853 sizeHints.flags |= USPosition;
854 sizeHints.width = width;
855 sizeHints.height = height;
858 XSetWMNormalHints(handle->dpy, handle->win, &sizeHints);
862 handle->wmDeleteWindow = XInternAtom(handle->dpy,
"WM_DELETE_WINDOW", False);
863 XSetWMProtocols(handle->dpy, handle->win, &handle->wmDeleteWindow, 1);
865 XMapRaised(handle->dpy, handle->win);
870 handle->sball = spaceball_attach(handle->dpy, handle->win);
873 handle->sball = spaceball_attach(handle->dpy, InputFocus);
876 spaceball_init_event(&handle->sballevent);
877 spaceball_clear_event(&handle->sballevent);
883 glGetIntegerv(GL_STENCIL_BITS, &stencilbits);
884 if (stencilbits > 0) {
885 handle->havestencil = 1;
889 glClearColor(1.0, 0.0, 0.0, 1.0);
890 glClear(GL_COLOR_BUFFER_BIT);
892 glClear(GL_COLOR_BUFFER_BIT);
902 XSetWindowAttributes attr;
904 XVisualInfo *vis=NULL;
905 XSizeHints sizeHints;
908 int glxstereoattrib[] = { GLX_RGBA, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8,
909 GLX_BLUE_SIZE, 8, GLX_DEPTH_SIZE, 16,
911 GLX_STEREO, GLX_DOUBLEBUFFER, None };
912 int glxnormalattrib[] = { GLX_RGBA, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8,
913 GLX_BLUE_SIZE, 8, GLX_DEPTH_SIZE, 16,
915 GLX_DOUBLEBUFFER, None };
916 int glxfailsafeattrib[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
917 GLX_BLUE_SIZE, 1, GLX_DEPTH_SIZE, 16,
918 GLX_DOUBLEBUFFER, None };
920 handle = glwin_alloc_init();
921 handle->width = width;
922 handle->height = height;
925 handle->havestencil = 1;
926 handle->instereo = 1;
927 vis=glXChooseVisual(handle->dpy, handle->scrnum, glxstereoattrib);
929 handle->havestencil = 1;
930 handle->instereo = 0;
932 vis=glXChooseVisual(handle->dpy, handle->scrnum, glxnormalattrib);
934 handle->havestencil = 0;
935 handle->instereo = 0;
937 vis=glXChooseVisual(handle->dpy, handle->scrnum, glxfailsafeattrib);
946 attr.background_pixel = 0;
947 attr.border_pixel = 0;
948 attr.colormap = XCreateColormap(handle->dpy, handle->root,
949 vis->visual, AllocNone);
951 attr.event_mask = StructureNotifyMask | ExposureMask;
952 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
954 handle->win = XCreateWindow(handle->dpy, handle->root, 0, 0, width, height,
955 0, vis->depth, InputOutput,
956 vis->visual, mask, &attr );
960 Atom bypasscomp = X11_XInternAtom(handle->dpy,
"_NET_WM_BYPASS_COMPOSITOR", False);
961 const long bypasscomp_on = 1;
962 X11_XChangeProperty(handle->dpy, handle->win, bypasscomp, XA_CARDINAL, 32,
963 PropModeReplace, (
unsigned char *) bypasscomp_on, 1);
966 handle->ctx = glXCreateContext( handle->dpy, vis, NULL, True );
968 glXMakeCurrent( handle->dpy, handle->win, handle->ctx );
971 glwin_init_exts(handle);
973 XStoreName(handle->dpy, handle->win, wintitle);
975 XSelectInput(handle->dpy, handle->win,
976 KeyPressMask | ButtonPressMask | ButtonReleaseMask |
977 PointerMotionMask | StructureNotifyMask | ExposureMask |
978 EnterWindowMask | LeaveWindowMask | FocusChangeMask);
981 memset((
void *) &(sizeHints), 0,
sizeof(sizeHints));
982 sizeHints.flags |= USSize;
983 sizeHints.flags |= USPosition;
984 sizeHints.width = width;
985 sizeHints.height = height;
988 XSetWMNormalHints(handle->dpy, handle->win, &sizeHints);
992 handle->wmDeleteWindow = XInternAtom(handle->dpy,
"WM_DELETE_WINDOW", False);
993 XSetWMProtocols(handle->dpy, handle->win, &handle->wmDeleteWindow, 1);
995 XMapRaised(handle->dpy, handle->win);
1000 handle->sball = spaceball_attach(handle->dpy, handle->win);
1003 handle->sball = spaceball_attach(handle->dpy, InputFocus);
1006 spaceball_init_event(&handle->sballevent);
1007 spaceball_clear_event(&handle->sballevent);
1012 glGetIntegerv(GL_STENCIL_BITS, &stencilbits);
1013 if (stencilbits > 0) {
1014 handle->havestencil = 1;
1017 glClearColor(0.0, 0.0, 0.0, 1.0);
1018 glClear(GL_COLOR_BUFFER_BIT);
1020 glClear(GL_COLOR_BUFFER_BIT);
1023 XFlush(handle->dpy);
1031 oglhandle * handle = (oglhandle *) voidhandle;
1036 if (handle->sball != NULL) {
1037 spaceball_close(handle->sball);
1038 handle->sball = NULL;
1042 if (handle->ext != NULL) {
1047 XUnmapWindow(handle->dpy, handle->win);
1051 eglMakeCurrent(handle->egldpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1052 eglDestroyContext(handle->egldpy, handle->eglctx);
1053 eglDestroySurface(handle->egldpy, handle->eglsurf);
1054 eglTerminate(handle->egldpy);
1057 glXMakeCurrent(handle->dpy, None, NULL);
1060 XDestroyWindow(handle->dpy, handle->win);
1061 XCloseDisplay(handle->dpy);
1066 oglhandle * handle = (oglhandle *) voidhandle;
1071 eglSwapBuffers(handle->egldpy, handle->eglsurf);
1074 glXSwapBuffers(handle->dpy, handle->win);
1080 oglhandle * handle = (oglhandle *) voidhandle;
1085 XComposeStatus comp;
1092 spaceball_clear_event(&handle->sballevent);
1097 XWindowAttributes xwa;
1098 XGetWindowAttributes(handle->dpy, handle->win, &xwa);
1099 handle->width = xwa.width;
1100 handle->height = xwa.height;
1106 while (!rc && (evblockmode || XPending(handle->dpy))) {
1108 unsigned int button;
1112 XNextEvent(handle->dpy, &event);
1115 handle->evkey =
'\0';
1117 switch(event.type) {
1119 case ReparentNotify:
1124 case ConfigureNotify:
1125 handle->width =
event.xconfigure.width;
1126 handle->height =
event.xconfigure.height;
1127 handle->xpos =
event.xconfigure.x;
1128 handle->ypos =
event.xconfigure.y;
1133 handle->mousex =
event.xbutton.x;
1134 handle->mousey =
event.xbutton.y;
1135 k = XLookupString(&(event.xkey), keybuf, keybuflen, &keysym, &comp);
1136 if (k > 0 && keybuf[0] !=
'\0') {
1138 handle->evkey = keybuf[0];
1177 handle->mousex =
event.xmotion.x;
1178 handle->mousey =
event.xmotion.y;
1184 button =
event.xbutton.button;
1185 handle->evval = (
event.type == ButtonPress);
1186 handle->mousex =
event.xbutton.x;
1187 handle->mousey =
event.xbutton.y;
1214 handle->havefocus=1;
1219 handle->havefocus=0;
1224 if (event.xclient.data.l[0] == handle->wmDeleteWindow) {
1231 spaceball_decode_event(handle->sball, &event, &handle->sballevent);
1235 if (handle->havefocus) {
1236 spaceball_decode_event(handle->sball, &event, &handle->sballevent);
1249 int glwin_resize(
void *voidhandle,
int width,
int height) {
1250 oglhandle * handle = (oglhandle *) voidhandle;
1254 XResizeWindow(handle->dpy, handle->win, width, height);
1257 XFlush(handle->dpy);
1265 oglhandle * handle = (oglhandle *) voidhandle;
1269 XMoveWindow(handle->dpy, handle->win, xpos, ypos);
1276 int glwin_switch_fullscreen_video_mode(
void * voidhandle, mode...) {
1277 XF86VidModeSwitchToMode(display,defaultscreen,video_mode);
1278 XF86VidModeSetViewPort(display,DefaultScreen,0,0);
1279 XMoveResizeWindow(display,window,0,0,width,height);
1280 XMapRaised(display,window);
1281 XGrabPointer(display,window,True,0,GrabModeAsync,GrabModeAsync,window,0L,CurrentTime);
1282 XGrabKeyboard(display,window,False,GrabModeAsync,GrabModeAsync,CurrentTime);
1289 unsigned long flags;
1290 unsigned long functions;
1291 unsigned long decorations;
1293 unsigned long status;
1297 oglhandle * handle = (oglhandle *) voidhandle;
1299 memset(&wmhints, 0,
sizeof(wmhints));
1302 wmhints.decorations = 0;
1304 wmhints.decorations = 1;
1306 wmproperty = XInternAtom(handle->dpy,
"_MOTIF_WM_HINTS", True);
1307 XChangeProperty(handle->dpy, handle->win, wmproperty, wmproperty, 32,
1308 PropModeReplace, (
unsigned char *) &wmhints, 5);
1312 XSetWindowAttributes xswa;
1313 xswa.override_redirect = False;
1314 XChangeWindowAttributes(handle->dpy, handle->win, CWOverrideRedirect, &xswa);
1318 #if 1 && defined(__linux) 1321 Atom fsatom = XInternAtom(handle->dpy,
"_NET_WM_STATE_FULLSCREEN", True);
1322 Atom stateatom = XInternAtom(handle->dpy,
"_NET_WM_STATE", True);
1323 if (fsatom != None && stateatom != None) {
1325 XChangeProperty(handle->dpy, handle->win, stateatom,
1326 XA_ATOM, 32, PropModeReplace, (
unsigned char*) &fsatom, 1);
1330 memset(&xev, 0,
sizeof(xev));
1331 xev.type = ClientMessage;
1332 xev.xclient.window = handle->win;
1333 xev.xclient.message_type = stateatom;
1334 xev.xclient.format = 32;
1336 xev.xclient.data.l[0] = 1;
1338 xev.xclient.data.l[0] = 0;
1339 xev.xclient.data.l[1] = fsatom;
1340 xev.xclient.data.l[2] = 0;
1342 XSendEvent(handle->dpy, handle->root, False,
1343 SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1345 XFlush(handle->dpy);
1349 printf(
"*** failed to obtain full screen X11 atom\n");
1358 int dpyScreen = DefaultScreen(handle->dpy);
1360 XSizeHints sizeHints;
1361 memset((
void *) &(sizeHints), 0,
sizeof(sizeHints));
1362 sizeHints.flags |= USSize;
1363 sizeHints.flags |= USPosition;
1365 sizeHints.width = DisplayWidth(handle->dpy, dpyScreen);
1366 sizeHints.height = DisplayHeight(handle->dpy, dpyScreen);
1370 #if defined(USEXINERAMA) 1371 if (xinescreen != -1) {
1372 int xinerr, xinevent, xinenumscreens;
1373 if (XineramaQueryExtension(handle->dpy, &xinevent, &xinerr) &&
1374 XineramaIsActive(handle->dpy)) {
1375 XineramaScreenInfo *screens =
1376 XineramaQueryScreens(handle->dpy, &xinenumscreens);
1377 if (xinescreen >= 0 && xinescreen < xinenumscreens) {
1378 sizeHints.width = screens[xinescreen].width;
1379 sizeHints.height = screens[xinescreen].height;
1380 sizeHints.x = screens[xinescreen].x_org;
1381 sizeHints.y = screens[xinescreen].y_org;
1382 #if 1 || defined(DEBUGOUTPUT) 1383 printf(
"*** OpenGL Stereo: Xinerama screen %d, +%d+%dx%dx%d\n",
1384 xinescreen, sizeHints.x, sizeHints.y,
1385 sizeHints.width, sizeHints.height);
1388 printf(
"*** OpenGL Stereo: no such Xinerama screen index %d\n",
1396 XMoveWindow(handle->dpy, handle->win, sizeHints.x, sizeHints.y);
1397 XResizeWindow(handle->dpy, handle->win, sizeHints.width, sizeHints.height);
1402 XSetWindowAttributes xswa;
1403 xswa.override_redirect = True;
1404 XChangeWindowAttributes(handle->dpy, handle->win, CWOverrideRedirect, &xswa);
1409 XSync(handle->dpy, 0);
1426 #if defined(USESPACEWARE) 1428 static spaceballhandle * spaceball_attach(HWND hWnd) {
1432 switch (SiInitialize()) {
1436 case SPW_DLL_LOAD_ERROR:
1442 spaceballhandle *handle = (spaceballhandle *) calloc(1,
sizeof(spaceballhandle));
1444 SiOpenWinInit(&oData, hWnd);
1445 SiSetUiMode(handle->sball, SI_UI_ALL_CONTROLS);
1449 handle->sball = SiOpen(
"OpenGL", SI_ANY_DEVICE, SI_NO_MASK, SI_EVENT, &oData);
1450 if ((handle->sball == NULL) || (handle->sball == SI_NO_HANDLE)) {
1456 res = SiBeep(handle->sball,
"CcCc");
1457 if ((handle->sball != NULL) && (handle->sball != SI_NO_HANDLE))
1465 static void spaceball_close(spaceballhandle *handle) {
1469 if (handle->sball != NULL) {
1471 res = SiClose(handle->sball);
1472 if (res != SPW_NO_ERROR)
1473 printf(
"An error occured during Spaceball shutdown.\n");
1481 static int spaceball_decode_event(spaceballhandle *handle, spaceballevent *sballevent, UINT msg, WPARAM wParam, LPARAM lParam) {
1485 if (handle->sball == NULL)
1489 SiGetEventWinInit(&handle->spwedata, msg, wParam, lParam);
1491 if (SiGetEvent(handle->sball, 0, &handle->spwedata, &handle->spwevent) == SI_IS_EVENT) {
1502 static void spaceball_init_event(spaceballevent *sballevent) {
1503 memset(sballevent, 0,
sizeof(spaceballevent));
1506 static void spaceball_clear_event(spaceballevent *sballevent) {
1513 sballevent->period = 0;
1514 sballevent->event = 0;
1521 LRESULT WINAPI myWindowProc( HWND, UINT, WPARAM, LPARAM );
1523 static const char *szClassName =
"OpenGLWindow";
1525 static int wc_initialized = 0;
1527 static int OpenWin32Connection(oglhandle * handle) {
1528 HINSTANCE hInstance = GetModuleHandle(NULL);
1530 if (!wc_initialized) {
1532 memset(&wc, 0,
sizeof(WNDCLASS));
1533 wc.style = CS_OWNDC;
1534 wc.lpfnWndProc = (WNDPROC) myWindowProc;
1535 wc.hInstance = hInstance;
1536 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
1537 wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
1538 wc.hbrBackground = NULL;
1539 wc.lpszMenuName = NULL;
1540 wc.lpszClassName = szClassName;
1542 if(!RegisterClass(&wc)) {
1543 printf(
"Cannot register window class.\n");
1550 handle->scrwidth = GetSystemMetrics(SM_CXSCREEN);
1551 handle->scrheight = GetSystemMetrics(SM_CYSCREEN);
1557 static HGLRC SetupOpenGL(oglhandle * handle) {
1558 int nMyPixelFormatID;
1561 PIXELFORMATDESCRIPTOR checkpfd;
1562 static PIXELFORMATDESCRIPTOR pfd = {
1563 sizeof (PIXELFORMATDESCRIPTOR),
1568 | PFD_SUPPORT_OPENGL,
1585 hDC = GetDC(handle->hWnd);
1586 nMyPixelFormatID = ChoosePixelFormat(hDC, &pfd);
1596 if (nMyPixelFormatID == 0) {
1597 printf(
"Error selecting OpenGL Pixel Format!!\n");
1602 DescribePixelFormat(hDC, nMyPixelFormatID,
1603 sizeof(PIXELFORMATDESCRIPTOR), &checkpfd);
1604 if (checkpfd.dwFlags & PFD_STEREO)
1605 handle->instereo = 1;
1607 handle->instereo = 0;
1609 SetPixelFormat(hDC, nMyPixelFormatID, &pfd);
1611 hRC = wglCreateContext(hDC);
1612 ReleaseDC(handle->hWnd, hDC);
1618 static int myCreateWindow(oglhandle * handle,
const char * wintitle,
1619 int xpos,
int ypos,
int xs,
int ys) {
1632 GetModuleHandle(NULL),
1636 if (!handle->hWnd) {
1637 printf(
"Couldn't Open Window!!\n");
1641 handle->hDC = GetDC(handle->hWnd);
1642 wglMakeCurrent(handle->hDC, handle->hRC);
1645 ShowWindow( handle->hWnd, SW_SHOW);
1646 UpdateWindow( handle->hWnd );
1652 static void win32decodemouse(oglhandle *handle, LPARAM lParam) {
1657 if (x & 1 << 15) x -= (1 << 16);
1658 if (y & 1 << 15) y -= (1 << 16);
1664 LRESULT WINAPI myWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1670 if (msg == WM_NCCREATE) {
1671 #if defined(_M_X64) || defined(_WIN64) || defined(_Wp64) 1672 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) (((CREATESTRUCT *) lParam)->lpCreateParams));
1674 SetWindowLong(hwnd, GWL_USERDATA, (LONG) (((CREATESTRUCT *) lParam)->lpCreateParams));
1676 SetProp(hwnd,
"OGLHANDLE", (((CREATESTRUCT *) lParam)->lpCreateParams));
1682 #if defined(_M_X64) || defined(_WIN64) || defined(_Wp64) 1683 handle = (oglhandle *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
1685 handle = (oglhandle *) GetWindowLong(hwnd, GWL_USERDATA);
1687 handle = (oglhandle *) GetProp(hwnd,
"OGLHANDLE");
1690 return DefWindowProc(hwnd, msg, wParam, lParam);
1694 handle->hWnd = hwnd;
1695 handle->hRC = SetupOpenGL(handle);
1699 wglMakeCurrent(handle->hDC, handle->hRC);
1700 handle->xpos = LOWORD(lParam);
1701 handle->ypos = HIWORD(lParam);
1705 wglMakeCurrent(handle->hDC, handle->hRC);
1706 handle->width = LOWORD(lParam);
1707 handle->height = HIWORD(lParam);
1714 handle->evkey = MapVirtualKey((UINT) wParam, 2);
1716 if (handle->evkey != 0) {
1721 int shiftstate = ((handle->MouseFlags & MK_SHIFT) != 0);
1722 handle->evkey = (shiftstate) ? toupper(handle->evkey) : tolower(handle->evkey);
1727 unsigned int keysym = (
unsigned int) wParam;
1764 win32decodemouse(handle, lParam);
1766 handle->MouseFlags = (long) wParam;
1767 if (!(handle->MouseFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)))
1773 int wheeldelta = ((short) HIWORD(wParam));
1774 if (wheeldelta > (WHEEL_DELTA / 2)) {
1776 }
else if (wheeldelta < -(WHEEL_DELTA / 2)) {
1782 case WM_LBUTTONDOWN:
1784 win32decodemouse(handle, lParam);
1785 handle->MouseFlags = (long) wParam;
1791 win32decodemouse(handle, lParam);
1792 handle->MouseFlags = (long) wParam;
1795 if (!(handle->MouseFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)))
1799 case WM_MBUTTONDOWN:
1801 win32decodemouse(handle, lParam);
1802 handle->MouseFlags = (long) wParam;
1808 win32decodemouse(handle, lParam);
1809 handle->MouseFlags = (long) wParam;
1812 if (!(handle->MouseFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)))
1816 case WM_RBUTTONDOWN:
1818 win32decodemouse(handle, lParam);
1819 handle->MouseFlags = (long) wParam;
1825 win32decodemouse(handle, lParam);
1826 handle->MouseFlags = (long) wParam;
1829 if (!(handle->MouseFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)))
1835 DestroyWindow(handle->hWnd);
1842 BeginPaint(hwnd, &ps);
1843 EndPaint(hwnd, &ps);
1847 glClear(GL_COLOR_BUFFER_BIT);
1848 SwapBuffers(handle->hDC);
1849 glDrawBuffer(GL_BACK);
1853 if (LOWORD(lParam) == HTCLIENT) {
1854 SetCursor(LoadCursor(NULL, IDC_ARROW));
1857 return DefWindowProc(hwnd, msg, wParam, lParam);
1860 return DefWindowProc(hwnd, msg, wParam, lParam);
1867 void *
glwin_create(
const char * wintitle,
int width,
int height) {
1872 handle = (oglhandle *) calloc(1,
sizeof(oglhandle));
1876 handle->havestencil=0;
1879 handle->width = width;
1880 handle->height = height;
1883 rc = OpenWin32Connection(handle);
1885 printf(
"OpenWin32Connection() returned an error!\n");
1890 handle->width = width;
1891 handle->height = height;
1893 rc = myCreateWindow(handle, wintitle, 0, 0, width, height);
1895 printf(
"CreateWindow() returned an error!\n");
1902 handle->sball = spaceball_attach(handle->win);
1905 spaceball_init_event(&handle->sballevent);
1906 spaceball_clear_event(&handle->sballevent);
1909 glGetIntegerv(GL_STENCIL_BITS, &stencilbits);
1910 if (stencilbits > 0) {
1911 handle->havestencil = 1;
1919 oglhandle * handle = (oglhandle *) voidhandle;
1921 wglDeleteContext(handle->hRC);
1923 DestroyWindow(handle->hWnd);
1925 PostQuitMessage( 0 );
1933 oglhandle * handle = (oglhandle *) voidhandle;
1935 SwapBuffers(handle->hDC);
1936 glDrawBuffer(GL_BACK);
1941 oglhandle * handle = (oglhandle *) voidhandle;
1948 handle->evkey =
'\0';
1952 pending=PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
1953 while (!rc && (evblockmode || pending)) {
1955 TranslateMessage(&msg);
1956 DispatchMessage(&msg);
1959 if (GetMessage(&msg, NULL, 0, 0)) {
1960 TranslateMessage(&msg);
1961 DispatchMessage(&msg);
1964 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1965 TranslateMessage(&msg);
1966 DispatchMessage(&msg);
1977 int glwin_resize(
void *voidhandle,
int width,
int height) {
1978 RECT rcClient, rcWindow;
1980 oglhandle * handle = (oglhandle *) voidhandle;
1984 GetClientRect(handle->hWnd, &rcClient);
1985 GetWindowRect(handle->hWnd, &rcWindow);
1986 ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
1987 ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
1988 MoveWindow(handle->hWnd, rcWindow.left, rcWindow.top, width + ptDiff.x, height + ptDiff.y, TRUE);
1995 RECT rcClient, rcWindow;
1996 oglhandle * handle = (oglhandle *) voidhandle;
2000 GetClientRect(handle->hWnd, &rcClient);
2001 GetWindowRect(handle->hWnd, &rcWindow);
2002 MoveWindow(handle->hWnd, xpos, ypos, rcWindow.right-rcWindow.left, rcWindow.bottom-rcWindow.top, TRUE);
2025 ext = (
char *) glGetString(GL_EXTENSIONS);
2027 endext = ext + strlen(ext);
2028 while (ext < endext) {
2029 size_t n = strcspn(ext,
" ");
2030 if ((strlen(extname) == n) && (strncmp(extname, ext, n) == 0)) {
2043 oglhandle * handle = (oglhandle *) voidhandle;
2047 #if defined(GLX_EXT_swap_control) 2048 if (glx_query_extension(handle->dpy,
"GLX_EXT_swap_control")) {
2050 unsigned int tmp = -1;
2051 glXQueryDrawable(handle->dpy, glXGetCurrentDrawable(), GLX_SWAP_INTERVAL_EXT, &tmp);
2062 glwin_ext_fctns *ext = handle->ext;
2063 if (ext->hasgetvideosyncsgi) {
2066 if (GLXGETVIDEOSYNCSGI(&count) == 0) {
2081 #if !(defined(WIN32) || defined(_WIN64)) 2085 GLenum drawbufs[16];
2093 oglhandle * handle = (oglhandle *) voidhandle;
2094 glwin_fbo_target * fb = (glwin_fbo_target *) voidtarget;
2095 if (handle == NULL || fb == NULL)
2097 glwin_ext_fctns *ext = handle->ext;
2099 GLBINDFRAMEBUFFER(GL_FRAMEBUFFER, fb->fbo);
2106 oglhandle * handle = (oglhandle *) voidhandle;
2107 glwin_fbo_target * fb = (glwin_fbo_target *) voidtarget;
2108 if (handle == NULL || fb == NULL)
2110 glwin_ext_fctns *ext = handle->ext;
2112 GLBINDFRAMEBUFFER(GL_FRAMEBUFFER, 0);
2119 oglhandle * handle = (oglhandle *) voidhandle;
2120 glwin_fbo_target * fb = (glwin_fbo_target *) voidtarget;
2121 if (handle == NULL || fb == NULL)
2123 glwin_ext_fctns *ext = handle->ext;
2125 GLDELETERENDERBUFFERS(1, &fb->depth);
2126 GLDELETEFRAMEBUFFERS(1, &fb->fbo);
2127 glDeleteTextures(1, &fb->tex);
2135 oglhandle * handle = (oglhandle *) voidhandle;
2136 glwin_fbo_target * fb = (glwin_fbo_target *) voidtarget;
2137 if (handle == NULL || fb == NULL)
2139 glwin_ext_fctns *ext = handle->ext;
2142 printf(
"\nglwin_fbo_target_resize(): W %d x %d\n", wsx, wsy);
2145 GLBINDFRAMEBUFFER(GL_FRAMEBUFFER, fb->fbo);
2147 glBindTexture(GL_TEXTURE_2D, fb->tex);
2148 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, wsx, wsy, 0,
2149 GL_RGBA, GL_UNSIGNED_BYTE, 0);
2150 GLFRAMEBUFFERTEXTURE2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2151 GL_TEXTURE_2D, fb->tex, 0);
2152 GLBINDRENDERBUFFER(GL_RENDERBUFFER, fb->depth);
2153 GLRENDERBUFFERSTORAGE(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, wsx, wsy);
2154 GLFRAMEBUFFERRENDERBUFFER(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
2155 GL_RENDERBUFFER, fb->depth);
2156 if (GLCHECKFRAMEBUFFERSTATUS(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
2160 fb->drawbufs[0] = GL_COLOR_ATTACHMENT0;
2161 GLDRAWBUFFERS(1, fb->drawbufs);
2168 oglhandle * handle = (oglhandle *) voidhandle;
2171 glwin_ext_fctns *ext = handle->ext;
2173 if (!ext->hasglfborendertarget)
2176 glwin_fbo_target *fb =
2177 (glwin_fbo_target *) calloc(1,
sizeof(glwin_fbo_target));
2181 glGenTextures(1, &fb->tex);
2182 glBindTexture(GL_TEXTURE_2D, fb->tex);
2185 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2192 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2193 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2198 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, wsx, wsy, 0,
2199 GL_RGBA, GL_UNSIGNED_BYTE, 0);
2201 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, wsx, wsy, 0,
2202 GL_RGBA, GL_UNSIGNED_BYTE, 0);
2204 glBindTexture(GL_TEXTURE_2D, 0);
2208 GLGENRENDERBUFFERS(1, &fb->depth);
2209 GLBINDRENDERBUFFER(GL_RENDERBUFFER, fb->depth);
2210 GLRENDERBUFFERSTORAGE(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, wsx, wsy);
2211 GLBINDRENDERBUFFER(GL_RENDERBUFFER, 0);
2215 GLGENFRAMEBUFFERS(1, &fb->fbo);
2216 GLBINDFRAMEBUFFER(GL_FRAMEBUFFER, fb->fbo);
2219 GLFRAMEBUFFERTEXTURE2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2220 GL_TEXTURE_2D, fb->tex, 0);
2223 GLFRAMEBUFFERRENDERBUFFER(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
2224 GL_RENDERBUFFER, fb->depth);
2226 if (GLCHECKFRAMEBUFFERSTATUS(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
2231 fb->drawbufs[0] = GL_COLOR_ATTACHMENT0;
2232 GLDRAWBUFFERS(1, fb->drawbufs);
2235 GLBINDFRAMEBUFFER(GL_FRAMEBUFFER, 0);
2250 oglhandle * handle = (oglhandle *) voidhandle;
2251 glwin_fbo_target * fb = (glwin_fbo_target *) voidtarget;
2252 if (handle == NULL || fb == NULL)
2254 glwin_ext_fctns *ext = handle->ext;
2256 GLBINDFRAMEBUFFER(GL_FRAMEBUFFER, 0);
2263 oglhandle * handle = (oglhandle *) voidhandle;
2264 glwin_fbo_target * fb = (glwin_fbo_target *) voidtarget;
2265 if (handle == NULL || fb == NULL)
2271 glBindTexture(GL_TEXTURE_2D, fb->tex);
2272 glEnable(GL_TEXTURE_2D);
2273 glColor3f(0.0, 0.0, 1.0);
2275 glTexCoord2f(0.0f, 0.0f);
2277 glTexCoord2f(0.0f, 1.0f);
2279 glTexCoord2f(1.0f, 1.0f);
2280 glVertex2f(wsx, wsy);
2281 glTexCoord2f(1.0f, 0.0f);
2284 glDisable(GL_TEXTURE_2D);
2294 #define HMD_DIVCNT 10 2296 static void hmd_compute_warped_coords(
int divcnt,
int wsx,
int wsy,
2297 float rscale,
float wscale,
2298 float *xcrds,
float *ycrds,
2299 const float *user_distort_coeff5) {
2300 float divs = (float) divcnt;
2301 float hwidth = wsx / 2.0f;
2302 float hwdiv = hwidth / divs;
2303 float hdiv = wsy / divs;
2304 float cx=hwidth / 2.0f;
2305 float cy=wsy / 2.0f;
2309 const float dk2_warp_coeff[5] = { 1.000f, 0.000f, 0.220f, 0.000f, 0.240f };
2311 const float msr_warp_coeff[5] = { 1.000f, 0.290f, 0.195f, 0.045f, 0.360f };
2313 const float *C = dk2_warp_coeff;
2319 if (user_distort_coeff5)
2320 C = user_distort_coeff5;
2323 for (iy=0; iy<=divcnt; iy++) {
2324 for (ix=0; ix<=divcnt; ix++) {
2325 float drx, dry, r, r2, rnew;
2326 int addr = iy*(divcnt+1) + ix;
2331 float rnorm = wsy * 1.0f;
2332 drx = (x - cx) / rnorm;
2333 dry = (y - cy) / rnorm;
2334 r2 = drx*drx + dry*dry;
2337 rnew = C[0] + r*C[1] + r2*C[2] + r*r2*C[3] + r2*r2*C[4];
2340 rnorm *= 1.0f * rscale;
2341 x = wscale * rnorm * rnew * drx + cx;
2342 y = rnorm * rnew * dry + cy;
2352 static void hmd_draw_eye_lines(
int divcnt,
int xoff,
int width,
int height,
2353 float *xcrds,
float *ycrds) {
2357 glDisable(GL_TEXTURE_2D);
2358 glColor3f(1.0f, 1.0f, 1.0f);
2360 for (iy=0; iy<=divcnt; iy++) {
2361 for (ix=0; ix<divcnt; ix++) {
2362 int addr = iy*(divcnt+1) + ix;
2364 x = xcrds[addr] + xoff;
2365 float xn = xcrds[addr+1] + xoff;
2366 float yn = ycrds[addr+1];
2373 for (ix=0; ix<=divcnt; ix++) {
2374 for (iy=0; iy<divcnt; iy++) {
2375 int addr = iy*(divcnt+1) + ix;
2376 x = xcrds[addr] + xoff;
2378 float xn = xcrds[addr + divcnt+1] + xoff;
2379 float yn = ycrds[addr + divcnt+1];
2390 static void hmd_draw_eye_texquads(
int divcnt,
int xoff,
int width,
int height,
2391 float *xcrds,
float *ycrds) {
2392 float divs = (float) divcnt;
2393 float xtxdiv = 0.5f / divs;
2394 float ytxdiv = 1.0f / divs;
2397 float txoff = xoff / ((float) width);
2400 for (iy=0,ty=1.0f; iy<divcnt; iy++,ty-=ytxdiv) {
2401 float tyn = ty-ytxdiv;
2402 for (ix=0,tx=0.0f; ix<divcnt; ix++,tx+=xtxdiv) {
2403 float txn = tx+xtxdiv;
2404 int addr = iy*(divcnt+1) + ix;
2405 float xx0y0 = xcrds[addr] + xoff;
2406 float yx0y0 = ycrds[addr];
2407 float xx1y0 = xcrds[addr + 1] + xoff;
2408 float yx1y0 = ycrds[addr + 1];
2409 float xx0y1 = xcrds[addr + divcnt+1] + xoff;
2410 float yx0y1 = ycrds[addr + divcnt+1];
2411 float xx1y1 = xcrds[addr + 1 + divcnt+1] + xoff;
2412 float yx1y1 = ycrds[addr + 1 + divcnt+1];
2414 glTexCoord2f(tx+txoff, ty);
2415 glVertex2f(xx0y0, yx0y0);
2416 glTexCoord2f(tx+txoff, tyn);
2417 glVertex2f(xx0y1, yx0y1);
2418 glTexCoord2f(txn+txoff, tyn);
2419 glVertex2f(xx1y1, yx1y1);
2420 glTexCoord2f(txn+txoff, ty);
2421 glVertex2f(xx1y0, yx1y0);
2456 int warpdivs,
int ixs,
int iys,
2457 const float *barrel_coeff,
int force) {
2458 glwin_warp_hmd * warp = (glwin_warp_hmd *) voidwarp;
2460 if (force || warp->divcnt!=warpdivs || warp->wsx!=wsx || warp->wsy!=wsy) {
2461 const float Oculus_DK2_coeff[4] = { 1.0f, 0.22f, 0.24f, 0.0f };
2464 barrel_coeff = Oculus_DK2_coeff;
2467 printf(
"glwin_spheremap_update_hmd_warp(): W %d x %d, I %d x %d\n",
2468 wsx, wsy, ixs, iys);
2469 printf(
"warp: %.3f, %.3f, %.3f, %.3f\n",
2470 barrel_coeff[0], barrel_coeff[1], barrel_coeff[2], barrel_coeff[3]);
2476 printf(
"\nglwin_spheremap_update_hmd_warp(): " 2477 "an error occured resizing the FBO!\n");
2483 if (warp->xcrds != NULL)
2485 if (warp->ycrds != NULL)
2488 if (warp->Rxcrds != NULL)
2490 if (warp->Rycrds != NULL)
2492 if (warp->Gxcrds != NULL)
2494 if (warp->Gycrds != NULL)
2496 if (warp->Bxcrds != NULL)
2498 if (warp->Bycrds != NULL)
2503 warp->divcnt = warpdivs;
2504 warp->xcrds = (
float *) calloc(1, warpdivs*warpdivs*
sizeof(
float));
2505 warp->ycrds = (
float *) calloc(1, warpdivs*warpdivs*
sizeof(
float));
2506 warp->Rxcrds = (
float *) calloc(1, warpdivs*warpdivs*
sizeof(
float));
2507 warp->Rycrds = (
float *) calloc(1, warpdivs*warpdivs*
sizeof(
float));
2508 warp->Gxcrds = (
float *) calloc(1, warpdivs*warpdivs*
sizeof(
float));
2509 warp->Gycrds = (
float *) calloc(1, warpdivs*warpdivs*
sizeof(
float));
2510 warp->Bxcrds = (
float *) calloc(1, warpdivs*warpdivs*
sizeof(
float));
2511 warp->Bycrds = (
float *) calloc(1, warpdivs*warpdivs*
sizeof(
float));
2514 hmd_compute_warped_coords(warpdivs-1, wsx, wsy, 1.0f, 1.0f,
2515 warp->xcrds, warp->ycrds, barrel_coeff);
2518 const float Rscale = 1.015f;
2519 const float Gscale = 1.000f;
2520 const float Bscale = 0.980f;
2522 hmd_compute_warped_coords(warpdivs-1, wsx,wsy, Rscale, 1.0f,
2523 warp->Rxcrds, warp->Rycrds, barrel_coeff);
2524 hmd_compute_warped_coords(warpdivs-1, wsx,wsy, Gscale, 1.0f,
2525 warp->Gxcrds, warp->Gycrds, barrel_coeff);
2526 hmd_compute_warped_coords(warpdivs-1, wsx,wsy, Bscale, 1.0f,
2527 warp->Bxcrds, warp->Bycrds, barrel_coeff);
2533 glwin_warp_hmd * warp = (glwin_warp_hmd *) voidwarp;
2536 if (warp->xcrds != NULL)
2538 if (warp->ycrds != NULL)
2541 if (warp->Rxcrds != NULL)
2543 if (warp->Rycrds != NULL)
2545 if (warp->Gxcrds != NULL)
2547 if (warp->Gycrds != NULL)
2549 if (warp->Bxcrds != NULL)
2551 if (warp->Bycrds != NULL)
2558 int warpdivs,
int ixs,
int iys,
2559 const float *user_coeff) {
2560 glwin_warp_hmd *warp = (glwin_warp_hmd *) calloc(1,
sizeof(glwin_warp_hmd));
2564 ixs, iys, user_coeff, 1);
2570 int drawimage,
int drawlines,
int chromcorr,
2571 int wsx,
int wsy,
int ixs,
int iys,
2572 const float *hmdquat,
2573 float fov,
float rad,
int hmd_spres) {
2574 oglhandle * handle = (oglhandle *) vwin;
2575 glwin_warp_hmd * warp = (glwin_warp_hmd *) voidwarp;
2576 glwin_fbo_target * fb = (glwin_fbo_target *) warp->hmd_fbo;
2577 if (handle == NULL || warp == NULL)
2580 glBindTexture(GL_TEXTURE_2D, 0);
2581 glEnable(GL_TEXTURE_2D);
2585 ixs, iys, hmdquat, fov, rad, hmd_spres);
2588 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2589 glClearColor(0.0, 0.0, 0.0, 1.0);
2590 glViewport(0, 0, wsx, wsy);
2591 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2593 glShadeModel(GL_SMOOTH);
2594 glMatrixMode(GL_PROJECTION);
2596 glOrtho(0.0, wsx, wsy, 0.0, -1.0, 1.0);
2597 glMatrixMode(GL_MODELVIEW);
2600 float hw = wsx * 0.5f;
2601 int dm1 = warp->divcnt-1;
2608 glBindTexture(GL_TEXTURE_2D, fb->tex);
2609 glEnable(GL_TEXTURE_2D);
2613 glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
2614 hmd_draw_eye_texquads(dm1, 0, wsx, wsy, warp->Rxcrds, warp->Rycrds);
2615 hmd_draw_eye_texquads(dm1, hw, wsx, wsy, warp->Rxcrds, warp->Rycrds);
2616 glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_TRUE);
2617 hmd_draw_eye_texquads(dm1, 0, wsx, wsy, warp->Gxcrds, warp->Gycrds);
2618 hmd_draw_eye_texquads(dm1, hw, wsx, wsy, warp->Gxcrds, warp->Gycrds);
2619 glColorMask(GL_FALSE, GL_FALSE, GL_TRUE, GL_TRUE);
2620 hmd_draw_eye_texquads(dm1, 0, wsx, wsy, warp->Bxcrds, warp->Bycrds);
2621 hmd_draw_eye_texquads(dm1, hw, wsx, wsy, warp->Bxcrds, warp->Bycrds);
2622 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2624 hmd_draw_eye_texquads(dm1, 0, wsx, wsy, warp->xcrds, warp->ycrds);
2625 hmd_draw_eye_texquads(dm1, hw, wsx, wsy, warp->xcrds, warp->ycrds);
2628 glDisable(GL_TEXTURE_2D);
2636 glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
2637 hmd_draw_eye_lines(dm1, 0, wsx, wsy, warp->Rxcrds, warp->Rycrds);
2638 hmd_draw_eye_lines(dm1, hw, wsx, wsy, warp->Rxcrds, warp->Rycrds);
2639 glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_TRUE);
2640 hmd_draw_eye_lines(dm1, 0, wsx, wsy, warp->Gxcrds, warp->Gycrds);
2641 hmd_draw_eye_lines(dm1, hw, wsx, wsy, warp->Gxcrds, warp->Gycrds);
2642 glColorMask(GL_FALSE, GL_FALSE, GL_TRUE, GL_TRUE);
2643 hmd_draw_eye_lines(dm1, 0, wsx, wsy, warp->Bxcrds, warp->Bycrds);
2644 hmd_draw_eye_lines(dm1, hw, wsx, wsy, warp->Bxcrds, warp->Bycrds);
2645 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2647 hmd_draw_eye_lines(dm1, 0, wsx, wsy, warp->xcrds, warp->ycrds);
2648 hmd_draw_eye_lines(dm1, hw, wsx, wsy, warp->xcrds, warp->ycrds);
2662 static void glwin_print_glsl_infolog(
void *voidhandle, GLhandleARB obj,
2664 oglhandle *handle = (oglhandle *) voidhandle;
2667 glwin_ext_fctns *ext = handle->ext;
2673 GLGETOBJECTPARAMETERIVARB(obj, GL_OBJECT_INFO_LOG_LENGTH_ARB , &blen);
2675 if ((infoLog = (GLcharARB *) calloc(1, blen)) == NULL) {
2676 printf(
"GLSL shader compiler could not allocate InfoLog buffer\n");
2680 GLGETINFOLOGARB(obj, blen, &slen, infoLog);
2681 printf(
" %s\n", msg);
2682 printf(
" %s\n", (
char *) infoLog);
2688 static int glwin_compile_shaders(
void *voidhandle, glsl_shader *sh,
2689 const GLubyte *vertexShader,
2690 const GLubyte *fragmentShader,
2692 oglhandle *handle = (oglhandle *) voidhandle;
2693 glwin_ext_fctns *ext = handle->ext;
2695 GLint vert_compiled = 0;
2696 GLint frag_compiled = 0;
2697 GLint shaders_linked = 0;
2701 memset(sh, 0,
sizeof(glsl_shader));
2704 if (vertexShader == NULL || fragmentShader == NULL) {
2709 length = strlen((
const char *) vertexShader);
2710 GLSHADERSOURCEARB(sh->VertexShaderObject, 1, (
const char **) &vertexShader, &
length);
2712 length = strlen((
const char *) fragmentShader);
2713 GLSHADERSOURCEARB(sh->FragmentShaderObject, 1, (
const char **) &fragmentShader, &
length);
2717 GLCOMPILESHADERARB(sh->VertexShaderObject);
2718 GLGETOBJECTPARAMETERIVARB(sh->VertexShaderObject,
2719 GL_OBJECT_COMPILE_STATUS_ARB, &vert_compiled);
2722 glwin_print_glsl_infolog(voidhandle, sh->VertexShaderObject,
"OpenGL vertex shader compilation log: ");
2724 GLCOMPILESHADERARB(sh->FragmentShaderObject);
2725 GLGETOBJECTPARAMETERIVARB(sh->FragmentShaderObject,
2726 GL_OBJECT_COMPILE_STATUS_ARB, &frag_compiled);
2729 glwin_print_glsl_infolog(voidhandle, sh->FragmentShaderObject,
"OpenGL fragment shader compilation log: ");
2731 if (vert_compiled && frag_compiled) {
2733 GLATTACHOBJECTARB(sh->ProgramObject, sh->VertexShaderObject);
2734 GLATTACHOBJECTARB(sh->ProgramObject, sh->FragmentShaderObject);
2737 GLLINKPROGRAMARB(sh->ProgramObject);
2738 GLGETOBJECTPARAMETERIVARB(sh->ProgramObject, GL_OBJECT_LINK_STATUS_ARB, &shaders_linked);
2741 glwin_print_glsl_infolog(voidhandle, sh->ProgramObject,
"OpenGL shader linkage log: " );
2752 GLDELETEOBJECTARB(sh->VertexShaderObject);
2754 GLDELETEOBJECTARB(sh->FragmentShaderObject);
2756 if (vert_compiled && frag_compiled && shaders_linked) {
2760 memset(sh, 0,
sizeof(glsl_shader));
2766 int glwin_destroy_shaders(
void *voidhandle, glsl_shader *sh) {
2767 oglhandle *handle = (oglhandle *) voidhandle;
2768 glwin_ext_fctns *ext = handle->ext;
2772 GLDELETEOBJECTARB(sh->ProgramObject);
2773 memset(sh, 0,
sizeof(glsl_shader));
2785 const char *hmd_vert =
2786 "// requires GLSL version 1.10 \n" 2791 "void main(void) { \n" 2792 " // transform vertex to Eye space for user clipping plane calculations \n" 2793 " vec4 ecpos = gl_ModelViewMatrix * gl_Vertex; \n" 2794 " gl_ClipVertex = ecpos; \n" 2796 " // transform, normalize, and output normal. \n" 2797 " oglnormal = normalize(gl_NormalMatrix * gl_Normal); \n" 2799 " // pass along vertex color for use fragment shading, \n" 2800 " // fragment shader will get an interpolated color. \n" 2801 " oglcolor = vec3(gl_Color); \n" 2805 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n" 2807 " gl_Position = ftransform(); \n" 2817 const char *hmd_frag =
2822 "void main(void) { \n" 2825 " // Flip the surface normal if it is facing away from the viewer, \n" 2826 " // determined by polygon winding order provided by OpenGL. \n" 2827 " vec3 N = normalize(oglnormal); \n" 2828 " if (!gl_FrontFacing) { \n" 2842 int glwin_compile_hmd_shaders(
void *voidhandle, glsl_shader *sh) {
2843 int rc = glwin_compile_shaders(voidhandle, sh,
2844 (GLubyte *) hmd_vert, (GLubyte *) hmd_frag, 1);
2851 void glwin_draw_image(
void * voidhandle,
int ixs,
int iys,
unsigned char * img) {
2852 glRasterPos2i(0, 0);
2853 glDrawPixels(ixs, iys, GL_RGB, GL_UNSIGNED_BYTE, img);
2859 const unsigned char *rgb3u) {
2862 glViewport(0, 0, wxs, wys);
2864 glDrawBuffer(GL_BACK);
2865 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2866 glClearColor(0.0, 0.0, 0.0, 1.0);
2867 glClear(GL_COLOR_BUFFER_BIT);
2869 glShadeModel(GL_FLAT);
2870 glMatrixMode(GL_PROJECTION);
2872 glOrtho(0.0, wxs, 0.0, wys, -1.0, 1.0);
2873 glMatrixMode(GL_MODELVIEW);
2874 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2875 glPixelZoom(1.0, 1.0);
2879 const unsigned char *leftimg = rgb3u;
2880 const unsigned char *rightimg = leftimg + ((ixs * (iys/2)) * 4);
2882 glDrawBuffer(GL_BACK_LEFT);
2883 glRasterPos2i(0, 0);
2885 glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
2887 glDrawPixels(ixs, iys/2, GL_RGBA, GL_UNSIGNED_BYTE, leftimg);
2889 glDrawBuffer(GL_BACK_RIGHT);
2890 glRasterPos2i(0, 0);
2892 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2894 glDrawPixels(ixs, iys/2, GL_RGBA, GL_UNSIGNED_BYTE, rightimg);
2896 glRasterPos2i(0, 0);
2897 glDrawPixels(ixs, iys, GL_RGBA, GL_UNSIGNED_BYTE, rgb3u);
2905 int stereomode,
int ixs,
int iys,
2906 const unsigned char *rgb3u) {
2909 glViewport(0, 0, wxs, wys);
2911 glDrawBuffer(GL_BACK);
2912 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2913 glClearColor(0.0, 0.0, 0.0, 1.0);
2914 glClear(GL_COLOR_BUFFER_BIT);
2916 glShadeModel(GL_FLAT);
2917 glMatrixMode(GL_PROJECTION);
2919 glOrtho(0.0, wxs, 0.0, wys, -1.0, 1.0);
2920 glMatrixMode(GL_MODELVIEW);
2923 GLfloat texborder[4] = {0.0, 0.0, 0.0, 1.0};
2924 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2925 glBindTexture(GL_TEXTURE_2D, texName);
2928 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, texborder);
2929 #if defined(GL_CLAMP_TO_BORDER) 2930 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
2931 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
2933 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2934 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2937 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2938 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2939 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2942 glColor3f(1.0, 1.0, 1.0);
2945 const unsigned char *leftimg = rgb3u;
2946 const unsigned char *rightimg = leftimg + ((ixs * (iys/2)) * 4);
2948 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ixs, iys, 0,
2949 GL_RGBA, GL_UNSIGNED_BYTE, leftimg);
2950 glEnable(GL_TEXTURE_2D);
2952 glDrawBuffer(GL_BACK_LEFT);
2954 glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
2957 glTexCoord2f(0.0f, 0.0f);
2958 glVertex2f(0.0f, 0.0f);
2959 glTexCoord2f(0.0f, 0.5f);
2960 glVertex2f(0.0f, wys);
2961 glTexCoord2f(1.0f, 0.5f);
2962 glVertex2f(wxs, wys);
2963 glTexCoord2f(1.0f, 0.0f);
2964 glVertex2f(wxs, 0.0f);
2967 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ixs, iys, 0,
2968 GL_RGBA, GL_UNSIGNED_BYTE, rightimg);
2969 glEnable(GL_TEXTURE_2D);
2971 glDrawBuffer(GL_BACK_RIGHT);
2973 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2976 glTexCoord2f(0.0f, 0.5f);
2977 glVertex2f(0.0f, 0.0f);
2978 glTexCoord2f(0.0f, 1.0f);
2979 glVertex2f(0.0f, wys);
2980 glTexCoord2f(1.0f, 1.0f);
2981 glVertex2f(wxs, wys);
2982 glTexCoord2f(1.0f, 0.5f);
2983 glVertex2f(wxs, 0.0f);
2986 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ixs, iys, 0,
2987 GL_RGBA, GL_UNSIGNED_BYTE, rgb3u);
2988 glEnable(GL_TEXTURE_2D);
2991 glTexCoord2f(0.0f, 0.0f);
2992 glVertex2f(0.0f, 0.0f);
2993 glTexCoord2f(0.0f, 1.0f);
2994 glVertex2f(0.0f, wys);
2995 glTexCoord2f(1.0f, 1.0f);
2996 glVertex2f(wxs, wys);
2997 glTexCoord2f(1.0f, 0.0f);
2998 glVertex2f(wxs, 0.0f);
3002 glDisable(GL_TEXTURE_2D);
3017 #define SPHEREMAXRES 64 3020 float zLo, zHi, res_1;
3022 float sinLong[SPHEREMAXRES];
3023 float cosLong[SPHEREMAXRES];
3024 float sinLatVert[SPHEREMAXRES];
3025 float cosLatVert[SPHEREMAXRES];
3026 float sinLatNorm[SPHEREMAXRES];
3027 float cosLatNorm[SPHEREMAXRES];
3028 float texLat[SPHEREMAXRES];
3029 float texLong[SPHEREMAXRES];
3032 float txlatsz = txlatend - txlatstart;
3037 if (res >= SPHEREMAXRES)
3038 res = SPHEREMAXRES-1;
3043 float ang_twopi_res = 6.28318530718f * res_1;
3044 for (i=0; i<res; i++) {
3045 float angle = i * ang_twopi_res;
3046 sinLong[i] = sinf(angle);
3047 cosLong[i] = cosf(angle);
3048 texLong[i] = (res-i) * res_1;
3051 sinLong[res] = 0.0f;
3052 cosLong[res] = 1.0f;
3053 texLong[res] = 0.0f;
3056 float ang_pi_res = 3.14159265359f * res_1;
3057 for (i=0; i<=res; i++) {
3058 float angle = i * ang_pi_res;
3059 sinLatNorm[i] = sinf(angle);
3060 cosLatNorm[i] = cosf(angle);
3061 sinLatVert[i] = rad * sinLatNorm[i];
3062 cosLatVert[i] = rad * cosLatNorm[i];
3063 texLat[i] = txlatstart + (i * res_1 * txlatsz);
3067 sinLatVert[res] = 0;
3069 for (j=0; j<res; j++) {
3070 zLo = cosLatVert[j];
3071 zHi = cosLatVert[j+1];
3073 float stv1 = sinLatVert[j];
3074 float stv2 = sinLatVert[j+1];
3076 float stn1 = sinLatNorm[j];
3077 float ctn1 = cosLatNorm[j];
3078 float stn2 = sinLatNorm[j+1];
3079 float ctn2 = cosLatNorm[j+1];
3081 glBegin(GL_QUAD_STRIP);
3082 for (i=0; i<=res; i++) {
3083 glNormal3f(sinLong[i] * stn2, cosLong[i] * stn2, ctn2);
3084 glTexCoord2f(texLong[i], texLat[j+1]);
3085 glVertex3f(stv2 * sinLong[i], stv2 * cosLong[i], zHi);
3087 glNormal3f(sinLong[i] * stn1, cosLong[i] * stn1, ctn1);
3088 glTexCoord2f(texLong[i], texLat[j]);
3089 glVertex3f(stv1 * sinLong[i], stv1 * cosLong[i], zLo);
3097 glShadeModel(GL_FLAT);
3098 glDepthFunc(GL_LEQUAL);
3099 glEnable(GL_DEPTH_TEST);
3102 glDrawBuffer(GL_BACK);
3103 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3104 glClearColor(0.0, 0.0, 0.0, 1.0);
3105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3108 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3109 glBindTexture(GL_TEXTURE_2D, texName);
3111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3112 #if !(defined(WIN32) || defined(_WIN64)) 3113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3121 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3126 const unsigned char *rgb3u) {
3127 glDisable(GL_TEXTURE_2D);
3129 glBindTexture(GL_TEXTURE_2D, texName);
3130 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ixs, iys, 0,
3131 GL_RGBA, GL_UNSIGNED_BYTE, rgb3u);
3132 glEnable(GL_TEXTURE_2D);
3137 int ixs,
int iys,
const float *hmdquat,
3138 float fov,
float rad,
int res) {
3140 float n, f, a, t, b, r, l;
3143 glViewport(0, 0, wxs, wys);
3145 glDrawBuffer(GL_BACK);
3146 glClearColor(0.0, 0.0, 0.0, 1.0);
3147 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3149 glMatrixMode(GL_PROJECTION);
3154 a = wxs / ((float) wys);
3155 t = n * tanf(fov * 3.14159265359f / (180.0f*2.0f));
3159 glFrustum(l, r, b, t, n, f);
3161 glMatrixMode(GL_MODELVIEW);
3164 if (hmdquat != NULL) {
3166 quat_rot_matrix(hmdmat, hmdquat);
3167 glMultMatrixf(hmdmat);
3170 glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
3171 glColor3f(1.0, 1.0, 1.0);
3178 glViewport(wxs/2, 0, wxs/2, wys);
3182 glViewport(0, 0, wxs/2, wys);
3191 oglhandle * handle = (oglhandle *) voidhandle;
3195 if (instereo != NULL)
3196 *instereo = handle->instereo;
3198 if (havestencil != NULL)
3199 *havestencil = handle->havestencil;
3206 oglhandle * handle = (oglhandle *) voidhandle;
3213 XWindowAttributes xwa;
3214 XGetWindowAttributes(handle->dpy, handle->win, &xwa);
3215 handle->width = xwa.width;
3216 handle->height = xwa.height;
3221 *xsize = handle->width;
3224 *ysize = handle->height;
3231 oglhandle * handle = (oglhandle *) voidhandle;
3236 *xpos = handle->xpos;
3239 *ypos = handle->ypos;
3246 oglhandle * handle = (oglhandle *) voidhandle;
3251 *x = handle->mousex;
3254 *y = handle->mousey;
3261 oglhandle * handle = (oglhandle *) voidhandle;
3266 *evdev = handle->evdev;
3269 *evval = handle->evval;
3272 *evkey = handle->evkey;
3279 oglhandle * handle = (oglhandle *) voidhandle;
3282 if (handle->sball != NULL)
3289 int glwin_get_spaceball(
void *voidhandle,
int *rx,
int *ry,
int *rz,
int *tx,
int *ty,
int *tz,
int *buttons) {
3290 oglhandle * handle = (oglhandle *) voidhandle;
3294 if ((handle->sball != NULL) && (handle->sballevent.event == 1)) {
3295 *rx = handle->sballevent.rx;
3296 *ry = handle->sballevent.ry;
3297 *rz = handle->sballevent.rz;
3298 *tx = handle->sballevent.tx;
3299 *ty = handle->sballevent.ty;
3300 *tz = handle->sballevent.tz;
3301 *buttons = handle->sballevent.buttons;
3364 const unsigned char *rgb3u) {
3369 int stereomode,
int ixs,
int iys,
3370 const unsigned char *rgb3u) {
3379 const unsigned char *rgb3u) {
3388 const float *hmdquat,
float fov,
float rad,
int res) {
3408 int glwin_get_spaceball(
void *voidhandle,
int *rx,
int *ry,
int *rz,
int *tx,
int *ty,
int *tz,
int *buttons) {
int glwin_get_lastevent(void *voidhandle, int *evdev, int *evval, char *evkey)
void * glwin_fbo_target_create(void *voidhandle, int width, int height)
void * glwin_create(const char *wintitle, int width, int height)
#define GLWIN_EV_WINDOW_CLOSE
window manager close event
void glwin_spheremap_destroy_hmd_warp(void *vwin, void *voidwarp)
void glwin_destroy(void *voidhandle)
#define GLWIN_EV_MOUSE_WHEELUP
int glwin_handle_events(void *voidhandle, int evblockmode)
int glwin_spheremap_draw_hmd_warp(void *vwin, void *voidwarp, int drawimage, int drawlines, int chromcorr, int wsx, int wsy, int ixs, int iys, const float *hmdquat, float fov, float rad, int hmd_spres)
int glwin_query_extension(const char *extname)
void glwin_draw_sphere_tex(float rad, int res, float txlatstart, float txlatend)
void glwin_draw_image(void *voidhandle, int xsize, int ysize, unsigned char *img)
int glwin_fbo_target_draw_fbo(void *voidhandle, void *voidtarget, int width, int height)
#define GLWIN_EV_KBD_PAGE_UP
#define GLWIN_EV_MOUSE_RIGHT
int glwin_get_spaceball(void *voidhandle, int *rx, int *ry, int *rz, int *tx, int *ty, int *tz, int *buttons)
#define GLWIN_EV_MOUSE_WHEELDOWN
#define GLWIN_EV_KBD_DELETE
#define GLWIN_EV_MOUSE_MIDDLE
#define GLWIN_EV_KBD_INSERT
int glwin_query_vsync(void *voidhandle, int *onoff)
#define GLWIN_EV_POLL_NONBLOCK
int glwin_get_winsize(void *voidhandle, int *xsize, int *ysize)
void glwin_spheremap_upload_tex_rgb3u(void *voidhandle, int ixs, int iys, const unsigned char *rgb3u)
int glwin_fbo_target_destroy(void *voidhandle, void *voidtarget)
int glwin_fullscreen(void *voidhandle, int fson, int xinescreen)
int glwin_fbo_target_bind(void *voidhandle, void *voidtarget)
void glwin_draw_image_tex_rgb3u(void *voidhandle, int stereomode, int ixs, int iys, const unsigned char *rgb3u)
void glwin_spheremap_draw_tex(void *voidhandle, int stereomode, int ixs, int iys, const float *hmdquat, float fov, float rad, int res)
void glwin_swap_buffers(void *voidhandle)
#define GLWIN_EV_KBD
all non-special chars
void glwin_spheremap_draw_prepare(void *voidhandle)
void glwin_spheremap_update_hmd_warp(void *vwin, void *voidwarp, int wsx, int wsy, int warpdivs, int ixs, int iys, const float *user_coeffs, int forceupdate)
#define GLWIN_EV_KBD_RIGHT
#define GLWIN_EV_MOUSE_LEFT
int glwin_spaceball_available(void *voidhandle)
int glwin_fbo_target_unbind(void *voidhandle, void *voidtarget)
int glwin_fbo_target_draw_normal(void *voidhandle, void *voidtarget)
#define GLWIN_EV_KBD_LEFT
#define GLWIN_NOT_IMPLEMENTED
void glwin_spheremap_draw(void *voidhandle, int stereomode, int ixs, int iys, const float *hmdquat, float fov, float rad, int res)
int glwin_get_mousepointer(void *voidhandle, int *x, int *y)
void glwin_draw_image_rgb3u(void *voidhandle, int stereomode, int ixs, int iys, const unsigned char *rgb3u)
int glwin_resize(void *voidhandle, int width, int height)
#define GLWIN_EV_MOUSE_MOVE
#define GLWIN_EV_POLL_BLOCK
void * glwin_spheremap_create_hmd_warp(void *vwin, int wsx, int wsy, int wrot, int warpdivs, int ixs, int iys, const float *user_coeffs)
__host__ __device__ float length(const float3 &v)
#define GLWIN_STEREO_OVERUNDER
int glwin_get_winpos(void *voidhandle, int *xpos, int *ypos)
#define GLWIN_EV_KBD_DOWN
#define GLWIN_EV_KBD_HOME
int glwin_reposition(void *voidhandle, int xpos, int ypos)
int glwin_get_wininfo(void *voidhandle, int *instereo, int *havestencil)
int glwin_fbo_target_resize(void *voidhandle, void *voidtarget, int width, int height)