graphics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit bf80f00fa0d29d77189336609ffab1979f73d7d7
parent 79a2edd6564ab14294b9d58320bca9b740da5c19
Author: Brian Swetland <swetland@frotz.net>
Date:   Sat,  9 Feb 2013 19:01:08 -0800

multiplatform: opengl on win32

Diffstat:
Mcommon/app.h | 7++++---
Mcommon/glapp.cc | 51+++++++++++++++++++++++++++++++++++++++++++++++----
Mcommon/glapp.h | 12++++++++++--
Acommon/opengl.h | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 147 insertions(+), 9 deletions(-)

diff --git a/common/app.h b/common/app.h @@ -1,5 +1,6 @@ -#ifdef _WIN32 -#include "dxapp.h" -#else + +#if defined(USE_OPENGL) || !defined(_WIN32) #include "glapp.h" +#else +#include "dxapp.h" #endif diff --git a/common/glapp.cc b/common/glapp.cc @@ -15,7 +15,10 @@ #include <stdio.h> #include <stdlib.h> -#include <unistd.h> + +#ifdef _WIN32 +#define GLUE_DEFINE_EXTENSIONS 1 +#endif #include "glapp.h" #include "util.h" @@ -46,10 +49,36 @@ void __check_gl_error(const char *fn, int line) { #endif static void die(const char *fmt, ...) { - fprintf(stderr,"ERROR: %s\n", fmt); + printx("ERROR: %s\n", fmt); exit(-1); } + +#ifdef _WIN32 +static int SDL_GL_ExtensionSupported(const char *name) { + if (strstr((char*)glGetString(GL_EXTENSIONS), name)) + return 1; + else + return 0; +} + +static void gl_map_functions(void) { + int n; + if (!SDL_GL_ExtensionSupported("GL_ARB_shader_objects") || + !SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") || + !SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") || + !SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) + die("missing glsl extensions"); + for (n = 0; n < sizeof(fntb)/sizeof(fntb[0]); n++) { + *fntb[n].func = SDL_GL_GetProcAddress(fntb[n].name); + if (!(*fntb[n].func)) + die("cannot find func '%s'", fntb[n].name); + } +} +#else +void gl_map_functions(void) {} +#endif + static void quit(void) { SDL_Quit(); exit(0); @@ -117,6 +146,8 @@ int App::start(void) { SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL) == NULL) die("sdl cannot set mode"); + gl_map_functions(); + if (init()) return -1; @@ -155,7 +186,7 @@ int main(int argc, char **argv) { // ---- -static inline void IGNORE(int x) {} +static inline void ignore(int x) {} void printx(const char *fmt, ...) { char buf[128]; @@ -164,7 +195,11 @@ void printx(const char *fmt, ...) { vsnprintf(buf, sizeof(buf), fmt, ap); buf[127] = 0; va_end(ap); - IGNORE(write(2, buf, strlen(buf))); +#ifdef _WIN32 + OutputDebugString(buf); +#else + fwrite(buf, strlen(buf), 1, stderr); +#endif } void printmtx(float *m, const char *name) { @@ -567,3 +602,10 @@ void App::drawIndexed(unsigned numindices) { glDrawElements(GL_TRIANGLES, numindices, GL_UNSIGNED_SHORT, NULL); CHECK(); } + +void App::setBlend(int enable) { + if (enable) + glEnable(GL_BLEND); + else + glDisable(GL_BLEND); +} +\ No newline at end of file diff --git a/common/glapp.h b/common/glapp.h @@ -16,11 +16,19 @@ #ifndef _GL_APP_H_ #define _GL_APP_H_ +#ifdef _WIN32 +#define NO_SDL_GLEXT 1 +#include <SDL.h> +#include <SDL_opengl.h> +#include "opengl.h" +#else #define GL_GLEXT_PROTOTYPES 1 #define NO_SDL_GLEXT 1 #include <SDL.h> #include <SDL_opengl.h> -#include <GL/glext.h> +#include "glcorearb.h" +#endif + #include <math.h> #define SL "glsl" @@ -173,7 +181,7 @@ public: void drawIndexedInstanced(unsigned numindices, unsigned numinstances); void drawInstanced(unsigned numvertices, unsigned numinstances); void drawIndexed(unsigned numindices); - + void setBlend(int enable); protected: int width; int height; diff --git a/common/opengl.h b/common/opengl.h @@ -0,0 +1,86 @@ +/* Copyright 2013 Brian Swetland <swetland@frotz.net> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _OPEN_GL_H_ +#define _OPEN_GL_H_ + +#include "glcorearb.h" + +#ifndef GLUE_DEFINE_EXTENSIONS +#define GLXTN extern +#else +#define GLXTN +#endif + +GLXTN PFNGLACTIVETEXTUREPROC glActiveTexture; +GLXTN PFNGLATTACHSHADERPROC glAttachShader; +GLXTN PFNGLBINDBUFFERPROC glBindBuffer; +GLXTN PFNGLBINDBUFFERBASEPROC glBindBufferBase; +GLXTN PFNGLBINDVERTEXARRAYPROC glBindVertexArray; +GLXTN PFNGLBUFFERDATAPROC glBufferData; +GLXTN PFNGLCOMPILESHADERPROC glCompileShader; +GLXTN PFNGLCREATEPROGRAMPROC glCreateProgram; +GLXTN PFNGLCREATESHADERPROC glCreateShader; +GLXTN PFNGLDELETEBUFFERSPROC glDeleteBuffers; +GLXTN PFNGLDELETEPROGRAMPROC glDeleteProgram; +GLXTN PFNGLDELETESHADERPROC glDeleteShader; +GLXTN PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays; +GLXTN PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray; +GLXTN PFNGLDRAWARRAYSINSTANCEDPROC glDrawArraysInstanced; +GLXTN PFNGLDRAWELEMENTSINSTANCEDPROC glDrawElementsInstanced; +GLXTN PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray; +GLXTN PFNGLGENBUFFERSPROC glGenBuffers; +GLXTN PFNGLGENERATEMIPMAPPROC glGenerateMipmap; +GLXTN PFNGLGENVERTEXARRAYSPROC glGenVertexArrays; +GLXTN PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation; +GLXTN PFNGLGETPROGRAMIVPROC glGetProgramiv; +GLXTN PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog; +GLXTN PFNGLGETSHADERIVPROC glGetShaderiv; +GLXTN PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog; +GLXTN PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation; +GLXTN PFNGLLINKPROGRAMPROC glLinkProgram; +GLXTN PFNGLSHADERSOURCEPROC glShaderSource; +GLXTN PFNGLUNIFORM1IPROC glUniform1i; +GLXTN PFNGLUNIFORM4FVPROC glUniform4fv; +GLXTN PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv; +GLXTN PFNGLUNIFORMBLOCKBINDINGPROC glUniformBlockBinding; +GLXTN PFNGLUSEPROGRAMPROC glUseProgram; +GLXTN PFNGLVERTEXATTRIBDIVISORPROC glVertexAttribDivisor; +GLXTN PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer; +GLXTN PFNGLVERTEXATTRIBIPOINTERPROC glVertexAttribIPointer; + +#ifdef GLUE_DEFINE_EXTENSIONS +#define EFUNC(n) { (void**) &n, #n } +struct { + void **func; + const char *name; +} fntb[] = { + EFUNC(glActiveTexture), EFUNC(glAttachShader), EFUNC(glBindBuffer), + EFUNC(glBindBufferBase), EFUNC(glBindVertexArray), EFUNC(glBufferData), + EFUNC(glCompileShader), EFUNC(glCreateProgram), EFUNC(glCreateShader), + EFUNC(glDeleteBuffers), EFUNC(glDeleteProgram), EFUNC(glDeleteShader), + EFUNC(glDeleteVertexArrays), EFUNC(glDisableVertexAttribArray), + EFUNC(glDrawArraysInstanced), EFUNC(glDrawElementsInstanced), + EFUNC(glEnableVertexAttribArray), EFUNC(glGenBuffers), EFUNC(glGenerateMipmap), + EFUNC(glGenVertexArrays), EFUNC(glGetAttribLocation), EFUNC(glGetProgramiv), + EFUNC(glGetProgramInfoLog), EFUNC(glGetShaderiv), EFUNC(glGetShaderInfoLog), + EFUNC(glGetUniformLocation), EFUNC(glLinkProgram), EFUNC(glShaderSource), + EFUNC(glUniform1i), EFUNC(glUniform4fv), EFUNC(glUniformMatrix4fv), + EFUNC(glUniformBlockBinding), EFUNC(glUseProgram), EFUNC(glVertexAttribDivisor), + EFUNC(glVertexAttribPointer), EFUNC(glVertexAttribIPointer), +}; +#endif +#endif +