Wrestled down how I was misunderstanding the way textures are bound and managed.

master
Zed A. Shaw 2 weeks ago
parent a44b31b256
commit 681007e383
  1. 1
      TODO.md
  2. 2
      lua/builder_test.lua
  3. 4
      src/engine.cpp
  4. 23
      src/image.cpp
  5. 1
      src/image.hpp
  6. 8
      src/shader.cpp
  7. 1
      src/shader.hpp
  8. 27
      src/text.cpp
  9. 1
      src/text.hpp

@ -3,3 +3,4 @@
* Character mapping for the fonts
* Checks for errors in analyzer
* Resource prep: images, fonts, font glyphs
* Images if images are first they don't render correctly, just black

@ -11,7 +11,7 @@ slide{
h1 "Here's Mine",
image{
src="assets/test.jpg"
}
},
}
slide{

@ -82,11 +82,11 @@ void Engine::init() {
setup();
text = std::make_shared<Text>();
text->load_font();
screen = std::make_shared<Screen>();
framebuffer = std::make_shared<FrameBuffer>();
text->load_font();
// BUG: this will fail if it's before text.load_font
deck.analyze();
screen->init();

@ -34,12 +34,15 @@ unsigned int image_to_texture(unsigned char *data, int width, int height, int nr
}
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
return textureID;
}
@ -55,16 +58,24 @@ void Image::load(const std::string& filename) {
void Image::render() {
shader.use();
shader.setInt("texture1", texture_id);
glBindVertexArray(image_vao);
// use the correct texture unit
glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(GL_TEXTURE_2D, texture_id);
// then I don't need to set this every time I render
glBindVertexArray(image_vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
// unbind
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
}
Image::Image() {
shader.use();
shader.setTextureUnit("texture1", texture_unit);
glGenVertexArrays(1, &image_vao);
glGenBuffers(1, &image_vbo);
@ -72,12 +83,14 @@ Image::Image() {
glBindBuffer(GL_ARRAY_BUFFER, image_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), &VERTICES, GL_STATIC_DRAW);
// position attrib
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
// position attrib location=0
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
// color attribute
// color attribute location = 1
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
// unbind
glBindVertexArray(0);
}

@ -7,6 +7,7 @@ struct Image {
unsigned int image_vao = UINT_MAX;
unsigned int image_vbo = UINT_MAX;
unsigned int texture_id = UINT_MAX;
unsigned int texture_unit = 1;
int width = 0;
int height = 0;
int nrComponents = 0;

@ -109,6 +109,14 @@ void Shader::setInt(const std::string &name, int value) const {
glUniform1i(uniform, value);
}
/*
* Mostly for self-documenting purposes, it just does an int.
*/
void Shader::setTextureUnit(const std::string &name, int value) const {
auto uniform = glGetUniformLocation(ID, name.c_str());
glUniform1i(uniform, value);
}
void Shader::setFloat(const std::string &name, float value) const {
auto uniform = glGetUniformLocation(ID, name.c_str());
glUniform1f(uniform, value);

@ -26,6 +26,7 @@ public:
void use() const;
void setBool(const std::string &name, bool value) const;
void setInt(const std::string &name, int value) const;
void setTextureUnit(const std::string &name, int value) const;
void setFloat(const std::string &name, float value) const;
void setVec4(const std::string &name, float v1, float v2, float v3, float v4) const;
void setVec4(const std::string &name, const glm::vec4& value) const;

@ -52,6 +52,21 @@ void Text::render(const std::string& text, float x, float y, float scale, glm::v
glBindTexture(GL_TEXTURE_2D, 0);
}
Text::Text() {
// can you create this before load_font?
glGenVertexArrays(1, &text_vao);
glGenBuffers(1, &text_vbo);
glBindVertexArray(text_vao);
glBindBuffer(GL_ARRAY_BUFFER, text_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindVertexArray(0);
}
void Text::load_font() {
FT_Library ft;
int fail = FT_Init_FreeType(&ft);
@ -93,22 +108,12 @@ void Text::load_font() {
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
static_cast<unsigned int>(face->glyph->advance.x));
}
// unbind
glBindTexture(GL_TEXTURE_2D, 0);
}
// cleanup
FT_Done_Face(face);
FT_Done_FreeType(ft);
// create teh vao/vbo
glGenVertexArrays(1, &text_vao);
glGenBuffers(1, &text_vbo);
glBindVertexArray(text_vao);
glBindBuffer(GL_ARRAY_BUFFER, text_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindVertexArray(0);
}

@ -21,4 +21,5 @@ struct Text {
void render(const std::string& text, float x, float y, float scale, glm::vec3 color);
void load_font();
Text();
};

Loading…
Cancel
Save