-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leaves.cpp
79 lines (69 loc) · 1.96 KB
/
Leaves.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Leaves.h"
Leaves::Leaves(float lu, float lv) : m_id(255) {
m_tex.setWrapS(GL_REPEAT);
m_tex.setWrapT(GL_REPEAT);
m_tex.setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
m_tex.loadDDS("textures/leaves.dds");
float vboData[8*8];
int idx = 0;
// front side
for (int i = 0; i < 4; ++i) {
// pos
vboData[idx++] = i % 2;
vboData[idx++] = i / 2;
vboData[idx++] = 0;
// normal
vboData[idx++] = 0;
vboData[idx++] = 0;
vboData[idx++] = 1;
// u,v
vboData[idx++] = (i % 2) * lu;
vboData[idx++] = (i / 2) * lv;
}
// back side
for (int i = 0; i < 4; ++i) {
// pos
vboData[idx++] = 1 - i % 2;
vboData[idx++] = i / 2;
vboData[idx++] = 0;
// normal
vboData[idx++] = 0;
vboData[idx++] = 0;
vboData[idx++] = -1;
// u,v
vboData[idx++] = (i % 2) * lu;
vboData[idx++] = (i / 2) * lv;
}
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vboData), (void*)vboData, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
Leaves::~Leaves() {
glDeleteVertexArrays(1, &m_vao);
glDeleteBuffers(1, &m_vbo);
}
void Leaves::draw(const ShaderProgram &p) {
glActiveTexture(GL_TEXTURE0);
m_tex.bind();
glUniform1i(p["sampler"], 0);
glUniform3f(m_KsUniformLoc, 0, 0, 0);
glUniform1f(m_NsUniformLoc, 1.0f);
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
glBindVertexArray(0);
}
void Leaves::setUniformLocations(GLint KsUniformLoc, GLint NsUniformLoc) {
m_KsUniformLoc = KsUniformLoc;
m_NsUniformLoc = NsUniformLoc;
}