goat3dgfx

view src/gfxutil.cc @ 34:3eb6c8f89fe1

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