commit 8a5dbfdb6a8f70910e78a2f57984379010ff58be
parent 5feed92ead4dbcecbe9eeeb78269c1bfda1d095d
Author: Brian Swetland <swetland@frotz.net>
Date: Mon, 17 Jun 2013 01:01:37 -0700
more helper function muckery
Diffstat:
4 files changed, 34 insertions(+), 33 deletions(-)
diff --git a/common/app.h b/common/app.h
@@ -78,12 +78,19 @@ struct Program {
~Program() { if (id) { glDeleteProgram(id); } };
void use(void) { glUseProgram(id); }
int link(VertexShader *vs, PixelShader *ps);
+ int load(const char *vsfn, const char *psfn);
};
struct Texture2D {
- unsigned tex;
- Texture2D() : tex(0) {};
- ~Texture2D() { if (tex) { glDeleteTextures(1, &tex); } };
+ unsigned id;
+ Texture2D() : id(0) {};
+ ~Texture2D() { if (id) { glDeleteTextures(1, &id); } };
+ int load(const char *fn, int genmips);
+ int load(void *data, unsigned w, unsigned h, int genmips);
+ void use(unsigned index) {
+ glActiveTexture(GL_TEXTURE0 + index);
+ glBindTexture(GL_TEXTURE_2D, id);
+ }
};
struct UniformBuffer {
@@ -136,12 +143,6 @@ public:
int start(void);
void handleEvents(void);
- int loadTextureRGBA(Texture2D *tex, const char *fn, int genmips);
- int createTextureRGBA(Texture2D *tex, void *data, unsigned w, unsigned h, int genmips);
-
- void useTexture(Texture2D *tex, int slot);
- void setBlend(int enable);
-
protected:
int width;
int height;
diff --git a/common/glapp.cc b/common/glapp.cc
@@ -317,6 +317,16 @@ int Program::link(VertexShader *vs, PixelShader *ps) {
return 0;
}
+int Program::load(const char *vsfn, const char *psfn) {
+ VertexShader vs;
+ PixelShader ps;
+ if (vs.load(vsfn))
+ return -1;
+ if (ps.load(psfn))
+ return -1;
+ return link(&vs, &ps);
+}
+
static int _load_shader(unsigned *out, const char *fn, unsigned type) {
void *data;
unsigned sz;
@@ -347,37 +357,27 @@ int VertexShader::load(const char *fn) {
return _load_shader(&id, fn, GL_VERTEX_SHADER);
}
-int App::loadTextureRGBA(Texture2D *tex, const char *fn, int genmips) {
+int Texture2D::load(const char *fn, int genmips) {
void *data;
unsigned dw, dh;
int r;
if (!(data = load_png_rgba(fn, &dw, &dh, 0)))
return error("cannot load '%s'", fn);
- r = createTextureRGBA(tex, data, dw, dh, genmips);
+ r = load(data, dw, dh, genmips);
free(data);
return r;
}
-int App::createTextureRGBA(Texture2D *tex, void *data, unsigned w, unsigned h, int genmips) {
- unsigned id;
- glGenTextures(1, &id);
+
+int Texture2D::load(void *data, unsigned w, unsigned h, int genmips) {
+ if (id == 0)
+ glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, data);
if (genmips)
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- tex->tex = id;
return 0;
}
-void App::useTexture(Texture2D *tex, int slot) {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex->tex);
-}
-
-void App::setBlend(int enable) {
- if (enable)
- glEnable(GL_BLEND);
- else
- glDisable(GL_BLEND);
-}
diff --git a/common/textgrid.cc b/common/textgrid.cc
@@ -73,7 +73,7 @@ int TextGrid::init(App *a, int w, int h) {
return -1;
clear();
- if (a->loadTextureRGBA(&texture, "font-vincent-8x8.png", 0))
+ if (texture.load("font-vincent-8x8.png", 0))
return -1;
if (ps.load("TextPS.glsl"))
@@ -105,7 +105,7 @@ void TextGrid::render(App *a) {
pgm.use();
attr.use();
ubuf.use(0);
- a->useTexture(&texture, 0);
+ texture.use(0);
glDrawArraysInstanced(GL_TRIANGLES, 0, 6, width * height);
}
diff --git a/hello/hello.cc b/hello/hello.cc
@@ -66,10 +66,10 @@ private:
VertexAttributes attr;
TextGrid text;
- float nx,ny;
mat4 proj;
- float rx, ry, zoom;
+ float zoom, rx, ry;
+ float nx,ny;
struct model *m;
};
@@ -211,9 +211,9 @@ oops:
text.printf(0, 2, "zm: %8.4f", zoom);
text.printf(0, -1, "hello.cc");
- setBlend(1);
+ glEnable(GL_BLEND);
text.render(this);
- setBlend(0);
+ glDisable(GL_BLEND);
}
App *createApp(void) {