-
Notifications
You must be signed in to change notification settings - Fork 0
/
Obj3D.cpp
53 lines (43 loc) · 1.85 KB
/
Obj3D.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
#include "Obj3D.hpp"
#include "objloader.hpp"
Obj3D::Obj3D(const char* fn)
{
bool res = loadOBJ(fn, vertices, uvs, normals);
// Jedes Objekt eigenem VAO zuordnen, damit mehrere Objekte moeglich sind
// VAOs sind Container fuer mehrere Buffer, die zusammen gesetzt werden sollen.
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
glGenBuffers(1, &vertexbuffer); // Kennung erhalten
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); // Daten zur Kennung definieren
// Buffer zugreifbar f�r die Shader machen
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
// Erst nach glEnableVertexAttribArray kann DrawArrays auf die Daten zugreifen...
glEnableVertexAttribArray(0); // siehe layout im vertex shader: location = 0
glVertexAttribPointer(0, // location = 0
3, // Datenformat vec3: 3 floats fuer xyz
GL_FLOAT,
GL_FALSE, // Fixedpoint data normalisieren ?
0, // Eckpunkte direkt hintereinander gespeichert
(void*)0); // abweichender Datenanfang ?
glGenBuffers(1, &normalbuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(2); // siehe layout im vertex shader
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1); // siehe layout im vertex shader
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
}
void Obj3D::display()
{
glBindVertexArray(VertexArrayID);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
}
Obj3D::~Obj3D()
{
glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &normalbuffer);
glDeleteBuffers(1, &uvbuffer);
}