graphics

experiments with opengl3.2/ogles3.3 on linux and win7
git clone http://frotz.net/git/graphics.git
Log | Files | Refs

core.h (4141B)


      1 /* Copyright 2013 Brian Swetland <swetland@frotz.net>
      2  *
      3  * Licensed under the Apache License, Version 2.0 (the "License");
      4  * you may not use this file except in compliance with the License.
      5  * You may obtain a copy of the License at
      6  *
      7  *     http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software
     10  * distributed under the License is distributed on an "AS IS" BASIS,
     11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  * See the License for the specific language governing permissions and
     13  * limitations under the License.
     14  */
     15 
     16 #ifndef _CORE_H_
     17 #define _CORE_H_
     18 
     19 //#define NO_SDL_GLEXT 1
     20 #include <SDL.h>
     21 #include "opengl.h"
     22 
     23 #include <math.h>
     24 
     25 #include "types.h"
     26 
     27 enum {
     28 	SRC_INT8,
     29 	SRC_UINT8,
     30 	SRC_INT16,
     31 	SRC_UINT16,
     32 	SRC_INT32,
     33 	SRC_UINT32,
     34 	SRC_FLOAT,
     35 };
     36 
     37 enum {
     38 	DST_FLOAT,
     39 	DST_NORMALIZED,
     40 	DST_INTEGER,
     41 };
     42 
     43 struct VertexAttrDesc {
     44 	unsigned index;
     45 	unsigned src_type;	// data type
     46 	unsigned dst_type;	// data type
     47 	unsigned count;		// number of components (1-4)
     48 	unsigned offset;	// offset of first attr
     49 	unsigned stride;	// offset from first attr to next
     50 	unsigned divisor;	// how often to step forward
     51 	unsigned unused;
     52 };
     53 
     54 struct VertexShader {
     55 	unsigned id;
     56 	VertexShader() : id(0) {};
     57 	~VertexShader() { if (id) { glDeleteShader(id); } };
     58 	int load(const char *fn, const char *defines = "");
     59 };
     60 
     61 struct PixelShader {
     62 	unsigned id;
     63 	PixelShader() : id(0) {};
     64 	~PixelShader() { if (id) glDeleteShader(id); };
     65 	int load(const char *fn, const char *defines = "");
     66 };
     67 
     68 struct GeometryShader {
     69 	unsigned id;
     70 	GeometryShader() : id(0) {};
     71 	~GeometryShader() { if (id) glDeleteShader(id); };
     72 	int load(const char *fn, const char *defines = "");
     73 };
     74 
     75 struct Program {
     76 	unsigned id;
     77 	unsigned bound;
     78 	Program() : id(0), bound(0) {};
     79 	~Program() { if (id) { glDeleteProgram(id); } };
     80 	void use(void) { glUseProgram(id); if (!bound) bind(); }
     81 	void bind(void);
     82 	int link(VertexShader *vs, PixelShader *ps);
     83 	int link(VertexShader *vs, GeometryShader *gs, PixelShader *ps);
     84 	int load(const char *vsfn, const char *psfn);
     85 	int load(const char *vsfn, const char *gsfn, const char *psfn);
     86 };
     87 
     88 #define OPT_TEX2D_GEN_MIPMAP	0x0001000
     89 #define OPT_TEX2D_RGBA		0x0000000
     90 #define OPT_TEX2D_GRAY		0x0000002
     91 
     92 struct Texture2D {
     93 	unsigned id;
     94 	unsigned width;
     95 	unsigned height;
     96 	Texture2D() : id(0), width(0), height(0) {};
     97 	~Texture2D() { if (id) { glDeleteTextures(1, &id); } };
     98 	int load(const char *fn, int options);
     99 	int load(void *data, unsigned w, unsigned h, int options);
    100 	int createRGBA(unsigned w, unsigned h);
    101 	void use(unsigned index) {
    102 		glActiveTexture(GL_TEXTURE0 + index);
    103 		glBindTexture(GL_TEXTURE_2D, id);
    104 	}
    105 };
    106 
    107 struct UniformBuffer {
    108 	unsigned id;
    109 	unsigned sz;
    110 	UniformBuffer() : id(0) {};
    111 	~UniformBuffer() { if (id) glDeleteBuffers(1, &id); };
    112 	void use(unsigned index) { glBindBufferBase(GL_UNIFORM_BUFFER, index, id); };
    113 	void load(void *data, unsigned size);
    114 };
    115 
    116 // TODO: when do we glUniformBLockBinding() ?
    117 
    118 struct VertexBuffer {
    119 	unsigned id;
    120 	unsigned sz;
    121 	VertexBuffer() : id(0) {};
    122 	~VertexBuffer() { if (id) glDeleteBuffers(1, &id); };
    123 	void use(void) { glBindBuffer(GL_ARRAY_BUFFER, id); };
    124 	void load(void *data, unsigned size);
    125 };
    126 
    127 struct IndexBuffer {
    128 	unsigned id;
    129 	unsigned sz;
    130 	IndexBuffer() : id(0) {};
    131 	~IndexBuffer() { if (id) glDeleteBuffers(1, &id); };
    132 	void use(void) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); };
    133 	void load(void *data, unsigned size);
    134 };
    135 
    136 struct VertexAttributes {
    137 	unsigned vao;
    138 	VertexAttributes() : vao(0) {};
    139 	~VertexAttributes() { /* todo */ };
    140 	void init(VertexAttrDesc *desc, VertexBuffer **data, unsigned count);
    141 	void use(void) { glBindVertexArray(vao); };
    142 };
    143 
    144 class FrameBuffer {
    145 public:
    146 	FrameBuffer() : id(0), txid(0), depth(0) {}
    147 	void init(unsigned w, unsigned h, unsigned _depth = 1);
    148 	void useTexture(unsigned index) {
    149 		glActiveTexture(GL_TEXTURE0 + index);
    150 		glBindTexture(GL_TEXTURE_RECTANGLE, txid);
    151 	}
    152 	void use(void) {
    153 		glBindFramebuffer(GL_FRAMEBUFFER, id);
    154 		glViewport(0, 0, width, height);
    155 	}
    156 private:
    157 	u32 id;
    158 	u32 txid;
    159 	u32 depth;
    160 	u32 width;
    161 	u32 height;
    162 };
    163 #endif
    164