goat3dgfx

diff src/gfxutil.cc @ 0:1873dfd13f2d

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Nov 2013 05:27:09 +0200
parents
children 7d6b667821cf
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gfxutil.cc	Thu Nov 14 05:27:09 2013 +0200
     1.3 @@ -0,0 +1,64 @@
     1.4 +#include <assert.h>
     1.5 +#include "gfxutil.h"
     1.6 +#include "mesh.h"
     1.7 +#include "meshgen.h"
     1.8 +#include "texture.h"
     1.9 +#include "shader.h"
    1.10 +#include "sdrman.h"
    1.11 +#include "opengl.h"
    1.12 +#include "logger.h"
    1.13 +
    1.14 +void draw_rect(const Vector3 &v1, const Vector3 &v2, Texture2D *tex, ShaderProg *sdr)
    1.15 +{
    1.16 +	static ShaderProg *defsdr;
    1.17 +
    1.18 +	if(!defsdr) {
    1.19 +		if(!(defsdr = get_sdrprog("defpost.v.glsl", "defpost.p.glsl"))) {
    1.20 +			static bool didlog;
    1.21 +			if(!didlog) {
    1.22 +				error_log("draw_rect: failed to load default shader\n");
    1.23 +				didlog = true;
    1.24 +			}
    1.25 +			if(!sdr) {
    1.26 +				return;
    1.27 +			}
    1.28 +		}
    1.29 +	}
    1.30 +
    1.31 +
    1.32 +	if(tex) {
    1.33 +		set_texture(tex);
    1.34 +	}
    1.35 +	if(sdr) {
    1.36 +		sdr->bind();
    1.37 +	} else {
    1.38 +		defsdr->bind();
    1.39 +
    1.40 +		assert(defsdr->get_attrib_location("attr_vertex") == MESH_ATTR_VERTEX);
    1.41 +		assert(defsdr->get_attrib_location("attr_texcoord") == MESH_ATTR_TEXCOORD);
    1.42 +	}
    1.43 +
    1.44 +
    1.45 +	float varr[] = {
    1.46 +		v1.x, v1.y, v1.z, v2.x, v1.y, v2.z,
    1.47 +		v2.x, v2.y, v2.z, v1.x, v2.y, v1.z
    1.48 +	};
    1.49 +	static const float tarr[] = { 0, 0, 1, 0, 1, 1, 0, 1 };
    1.50 +	static const unsigned int idxarr[] = {0, 1, 2, 0, 2, 3};
    1.51 +
    1.52 +	// disable VBOs if they are enabled
    1.53 +	glBindBuffer(GL_ARRAY_BUFFER, 0);
    1.54 +	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    1.55 +
    1.56 +	glEnableVertexAttribArray(MESH_ATTR_VERTEX);
    1.57 +	glVertexAttribPointer(MESH_ATTR_VERTEX, 3, GL_FLOAT, GL_FALSE, 0, varr);
    1.58 +	glEnableVertexAttribArray(MESH_ATTR_TEXCOORD);
    1.59 +	glVertexAttribPointer(MESH_ATTR_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, tarr);
    1.60 +
    1.61 +	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, idxarr);
    1.62 +
    1.63 +	glDisableVertexAttribArray(MESH_ATTR_VERTEX);
    1.64 +	glDisableVertexAttribArray(MESH_ATTR_TEXCOORD);
    1.65 +
    1.66 +	set_texture(0);
    1.67 +}