|
|
|
@ -34,12 +34,15 @@ unsigned int image_to_texture(unsigned char *data, int width, int height, int nr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textureID); |
|
|
|
glBindTexture(GL_TEXTURE_2D, textureID); |
|
|
|
|
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); |
|
|
|
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_S, GL_REPEAT); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 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_MIN_FILTER, GL_LINEAR); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0); |
|
|
|
|
|
|
|
|
|
|
|
return textureID; |
|
|
|
return textureID; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -55,16 +58,24 @@ void Image::load(const std::string& filename) { |
|
|
|
|
|
|
|
|
|
|
|
void Image::render() { |
|
|
|
void Image::render() { |
|
|
|
shader.use(); |
|
|
|
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); |
|
|
|
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); |
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// unbind
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0); |
|
|
|
glBindTexture(GL_TEXTURE_2D, 0); |
|
|
|
glBindVertexArray(0); |
|
|
|
glBindVertexArray(0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Image::Image() { |
|
|
|
Image::Image() { |
|
|
|
|
|
|
|
shader.use(); |
|
|
|
|
|
|
|
shader.setTextureUnit("texture1", texture_unit); |
|
|
|
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &image_vao); |
|
|
|
glGenVertexArrays(1, &image_vao); |
|
|
|
glGenBuffers(1, &image_vbo); |
|
|
|
glGenBuffers(1, &image_vbo); |
|
|
|
|
|
|
|
|
|
|
|
@ -72,12 +83,14 @@ Image::Image() { |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, image_vbo); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, image_vbo); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), &VERTICES, GL_STATIC_DRAW); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), &VERTICES, GL_STATIC_DRAW); |
|
|
|
|
|
|
|
|
|
|
|
// position attrib
|
|
|
|
// position attrib location=0
|
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); |
|
|
|
|
|
|
|
glEnableVertexAttribArray(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))); |
|
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float))); |
|
|
|
glEnableVertexAttribArray(1); |
|
|
|
glEnableVertexAttribArray(1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// unbind
|
|
|
|
glBindVertexArray(0); |
|
|
|
glBindVertexArray(0); |
|
|
|
} |
|
|
|
} |
|
|
|
|