goat3dgfx

annotate src/gfxutil.cc @ 15:7d6b667821cf

wrapped everything in the goatgfx namespace
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Nov 2013 20:52:21 +0200
parents 1873dfd13f2d
children dc5918c62a64
rev   line source
nuclear@0 1 #include <assert.h>
nuclear@0 2 #include "gfxutil.h"
nuclear@0 3 #include "mesh.h"
nuclear@0 4 #include "meshgen.h"
nuclear@0 5 #include "texture.h"
nuclear@0 6 #include "shader.h"
nuclear@0 7 #include "sdrman.h"
nuclear@0 8 #include "opengl.h"
nuclear@0 9 #include "logger.h"
nuclear@0 10
nuclear@15 11 using namespace goatgfx;
nuclear@15 12
nuclear@0 13 void draw_rect(const Vector3 &v1, const Vector3 &v2, Texture2D *tex, ShaderProg *sdr)
nuclear@0 14 {
nuclear@0 15 static ShaderProg *defsdr;
nuclear@0 16
nuclear@0 17 if(!defsdr) {
nuclear@0 18 if(!(defsdr = get_sdrprog("defpost.v.glsl", "defpost.p.glsl"))) {
nuclear@0 19 static bool didlog;
nuclear@0 20 if(!didlog) {
nuclear@0 21 error_log("draw_rect: failed to load default shader\n");
nuclear@0 22 didlog = true;
nuclear@0 23 }
nuclear@0 24 if(!sdr) {
nuclear@0 25 return;
nuclear@0 26 }
nuclear@0 27 }
nuclear@0 28 }
nuclear@0 29
nuclear@0 30
nuclear@0 31 if(tex) {
nuclear@0 32 set_texture(tex);
nuclear@0 33 }
nuclear@0 34 if(sdr) {
nuclear@0 35 sdr->bind();
nuclear@0 36 } else {
nuclear@0 37 defsdr->bind();
nuclear@0 38
nuclear@0 39 assert(defsdr->get_attrib_location("attr_vertex") == MESH_ATTR_VERTEX);
nuclear@0 40 assert(defsdr->get_attrib_location("attr_texcoord") == MESH_ATTR_TEXCOORD);
nuclear@0 41 }
nuclear@0 42
nuclear@0 43
nuclear@0 44 float varr[] = {
nuclear@0 45 v1.x, v1.y, v1.z, v2.x, v1.y, v2.z,
nuclear@0 46 v2.x, v2.y, v2.z, v1.x, v2.y, v1.z
nuclear@0 47 };
nuclear@0 48 static const float tarr[] = { 0, 0, 1, 0, 1, 1, 0, 1 };
nuclear@0 49 static const unsigned int idxarr[] = {0, 1, 2, 0, 2, 3};
nuclear@0 50
nuclear@0 51 // disable VBOs if they are enabled
nuclear@0 52 glBindBuffer(GL_ARRAY_BUFFER, 0);
nuclear@0 53 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
nuclear@0 54
nuclear@0 55 glEnableVertexAttribArray(MESH_ATTR_VERTEX);
nuclear@0 56 glVertexAttribPointer(MESH_ATTR_VERTEX, 3, GL_FLOAT, GL_FALSE, 0, varr);
nuclear@0 57 glEnableVertexAttribArray(MESH_ATTR_TEXCOORD);
nuclear@0 58 glVertexAttribPointer(MESH_ATTR_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, tarr);
nuclear@0 59
nuclear@0 60 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, idxarr);
nuclear@0 61
nuclear@0 62 glDisableVertexAttribArray(MESH_ATTR_VERTEX);
nuclear@0 63 glDisableVertexAttribArray(MESH_ATTR_TEXCOORD);
nuclear@0 64
nuclear@0 65 set_texture(0);
nuclear@0 66 }