graphics

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

commit 86ffc8f811719802ee3a6894e77c4dd5d1c008a4
parent ccb7bc8e6b6a0aff396eb4e2da8d253b0419a873
Author: Brian Swetland <swetland@frotz.net>
Date:   Sun,  1 Sep 2013 23:08:16 -0700

common: move low level wrapper objects into core.h

Diffstat:
Mcommon/app.h | 137++-----------------------------------------------------------------------------
Acommon/core.h | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 156 insertions(+), 134 deletions(-)

diff --git a/common/app.h b/common/app.h @@ -13,141 +13,10 @@ * limitations under the License. */ -#ifndef _GL_APP_H_ -#define _GL_APP_H_ +#ifndef _APP_H_ +#define _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 "glcorearb.h" -#endif - -#include <math.h> - -#define RGBA(r,g,b,a) ((r&0xFF)|((g&0xFF)<<8)|((b&0xFF)<<16)|((a&0xFF)<<24)) - -enum { - SRC_INT8, - SRC_UINT8, - SRC_INT16, - SRC_UINT16, - SRC_INT32, - SRC_UINT32, - SRC_FLOAT, -}; - -enum { - DST_FLOAT, - DST_NORMALIZED, - DST_INTEGER, -}; - -struct VertexAttrDesc { - unsigned index; - unsigned src_type; // data type - unsigned dst_type; // data type - unsigned count; // number of components (1-4) - unsigned offset; // offset of first attr - unsigned stride; // offset from first attr to next - unsigned divisor; // how often to step forward - unsigned unused; -}; - -struct VertexShader { - unsigned id; - VertexShader() : id(0) {}; - ~VertexShader() { if (id) { glDeleteShader(id); } }; - int load(const char *fn); -}; - -struct PixelShader { - unsigned id; - PixelShader() : id(0) {}; - ~PixelShader() { if (id) glDeleteShader(id); }; - int load(const char *fn); -}; - -struct GeometryShader { - unsigned id; - GeometryShader() : id(0) {}; - ~GeometryShader() { if (id) glDeleteShader(id); }; - int load(const char *fn); -}; - -struct Program { - unsigned id; - unsigned bound; - Program() : id(0), bound(0) {}; - ~Program() { if (id) { glDeleteProgram(id); } }; - void use(void) { glUseProgram(id); if (!bound) bind(); } - void bind(void); - int link(VertexShader *vs, PixelShader *ps); - int link(VertexShader *vs, GeometryShader *gs, PixelShader *ps); - int load(const char *vsfn, const char *psfn); - int load(const char *vsfn, const char *gsfn, const char *psfn); -}; - -#define OPT_TEX2D_GEN_MIPMAP 0x0001000 -#define OPT_TEX2D_RGBA 0x0000000 -#define OPT_TEX2D_GRAY 0x0000002 - -struct Texture2D { - unsigned id; - unsigned width; - unsigned height; - Texture2D() : id(0), width(0), height(0) {}; - ~Texture2D() { if (id) { glDeleteTextures(1, &id); } }; - int load(const char *fn, int options); - int load(void *data, unsigned w, unsigned h, int options); - void use(unsigned index) { - glActiveTexture(GL_TEXTURE0 + index); - glBindTexture(GL_TEXTURE_2D, id); - } -}; - -struct UniformBuffer { - unsigned id; - unsigned sz; - UniformBuffer() : id(0) {}; - ~UniformBuffer() { if (id) glDeleteBuffers(1, &id); }; - void use(unsigned index) { glBindBufferBase(GL_UNIFORM_BUFFER, index, id); }; - void load(void *data, unsigned size); -}; - -// TODO: when do we glUniformBLockBinding() ? - -struct VertexBuffer { - unsigned id; - unsigned sz; - VertexBuffer() : id(0) {}; - ~VertexBuffer() { if (id) glDeleteBuffers(1, &id); }; - void use(void) { glBindBuffer(GL_ARRAY_BUFFER, id); }; - void load(void *data, unsigned size); -}; - -struct IndexBuffer { - unsigned id; - unsigned sz; - IndexBuffer() : id(0) {}; - ~IndexBuffer() { if (id) glDeleteBuffers(1, &id); }; - void use(void) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); }; - void load(void *data, unsigned size); -}; - -struct VertexAttributes { - unsigned vao; - VertexAttributes() : vao(0) {}; - ~VertexAttributes() { /* todo */ }; - void init(VertexAttrDesc *desc, VertexBuffer **data, unsigned count); - void use(void) { glBindVertexArray(vao); }; -}; +#include "core.h" class App { public: diff --git a/common/core.h b/common/core.h @@ -0,0 +1,153 @@ +/* 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 _CORE_H_ +#define _CORE_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 "glcorearb.h" +#endif + +#include <math.h> + +#define RGBA(r,g,b,a) ((r&0xFF)|((g&0xFF)<<8)|((b&0xFF)<<16)|((a&0xFF)<<24)) + +enum { + SRC_INT8, + SRC_UINT8, + SRC_INT16, + SRC_UINT16, + SRC_INT32, + SRC_UINT32, + SRC_FLOAT, +}; + +enum { + DST_FLOAT, + DST_NORMALIZED, + DST_INTEGER, +}; + +struct VertexAttrDesc { + unsigned index; + unsigned src_type; // data type + unsigned dst_type; // data type + unsigned count; // number of components (1-4) + unsigned offset; // offset of first attr + unsigned stride; // offset from first attr to next + unsigned divisor; // how often to step forward + unsigned unused; +}; + +struct VertexShader { + unsigned id; + VertexShader() : id(0) {}; + ~VertexShader() { if (id) { glDeleteShader(id); } }; + int load(const char *fn); +}; + +struct PixelShader { + unsigned id; + PixelShader() : id(0) {}; + ~PixelShader() { if (id) glDeleteShader(id); }; + int load(const char *fn); +}; + +struct GeometryShader { + unsigned id; + GeometryShader() : id(0) {}; + ~GeometryShader() { if (id) glDeleteShader(id); }; + int load(const char *fn); +}; + +struct Program { + unsigned id; + unsigned bound; + Program() : id(0), bound(0) {}; + ~Program() { if (id) { glDeleteProgram(id); } }; + void use(void) { glUseProgram(id); if (!bound) bind(); } + void bind(void); + int link(VertexShader *vs, PixelShader *ps); + int link(VertexShader *vs, GeometryShader *gs, PixelShader *ps); + int load(const char *vsfn, const char *psfn); + int load(const char *vsfn, const char *gsfn, const char *psfn); +}; + +#define OPT_TEX2D_GEN_MIPMAP 0x0001000 +#define OPT_TEX2D_RGBA 0x0000000 +#define OPT_TEX2D_GRAY 0x0000002 + +struct Texture2D { + unsigned id; + unsigned width; + unsigned height; + Texture2D() : id(0), width(0), height(0) {}; + ~Texture2D() { if (id) { glDeleteTextures(1, &id); } }; + int load(const char *fn, int options); + int load(void *data, unsigned w, unsigned h, int options); + void use(unsigned index) { + glActiveTexture(GL_TEXTURE0 + index); + glBindTexture(GL_TEXTURE_2D, id); + } +}; + +struct UniformBuffer { + unsigned id; + unsigned sz; + UniformBuffer() : id(0) {}; + ~UniformBuffer() { if (id) glDeleteBuffers(1, &id); }; + void use(unsigned index) { glBindBufferBase(GL_UNIFORM_BUFFER, index, id); }; + void load(void *data, unsigned size); +}; + +// TODO: when do we glUniformBLockBinding() ? + +struct VertexBuffer { + unsigned id; + unsigned sz; + VertexBuffer() : id(0) {}; + ~VertexBuffer() { if (id) glDeleteBuffers(1, &id); }; + void use(void) { glBindBuffer(GL_ARRAY_BUFFER, id); }; + void load(void *data, unsigned size); +}; + +struct IndexBuffer { + unsigned id; + unsigned sz; + IndexBuffer() : id(0) {}; + ~IndexBuffer() { if (id) glDeleteBuffers(1, &id); }; + void use(void) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); }; + void load(void *data, unsigned size); +}; + +struct VertexAttributes { + unsigned vao; + VertexAttributes() : vao(0) {}; + ~VertexAttributes() { /* todo */ }; + void init(VertexAttrDesc *desc, VertexBuffer **data, unsigned count); + void use(void) { glBindVertexArray(vao); }; +}; + +#endif +