-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexturedQuad.cpp
47 lines (38 loc) · 1.45 KB
/
TexturedQuad.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <GL/glew.h>
#include <GL/gl.h>
#include "TexturedQuad.h"
TexturedQuad *TexturedQuad::createTexturedQuad(glm::vec2 geom[2], glm::vec2 texCoords[2], ShaderProgram &program)
{
TexturedQuad *quad = new TexturedQuad(geom, texCoords, program);
return quad;
}
TexturedQuad::TexturedQuad(glm::vec2 geom[2], glm::vec2 texCoords[2], ShaderProgram &program)
{
float vertices[24] = {geom[0].x, geom[0].y, texCoords[0].x, texCoords[0].y,
geom[1].x, geom[0].y, texCoords[1].x, texCoords[0].y,
geom[1].x, geom[1].y, texCoords[1].x, texCoords[1].y,
geom[0].x, geom[0].y, texCoords[0].x, texCoords[0].y,
geom[1].x, geom[1].y, texCoords[1].x, texCoords[1].y,
geom[0].x, geom[1].y, texCoords[0].x, texCoords[1].y};
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), vertices, GL_STATIC_DRAW);
posLocation = program.bindVertexAttribute("position", 2, 4*sizeof(float), 0);
texCoordLocation = program.bindVertexAttribute("texCoord", 2, 4*sizeof(float), (void *)(2*sizeof(float)));
}
void TexturedQuad::render(const Texture &tex) const
{
glEnable(GL_TEXTURE_2D);
tex.use();
glBindVertexArray(vao);
glEnableVertexAttribArray(posLocation);
glEnableVertexAttribArray(texCoordLocation);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisable(GL_TEXTURE_2D);
}
void TexturedQuad::free()
{
glDeleteBuffers(1, &vbo);
}