dungeon_crawler

view prototype/src/mesh.cc @ 4:158de53b4e18

tile work
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Aug 2012 05:44:52 +0300
parents 1f61f2a02832
children 252a00508411
line source
1 #include "mesh.h"
2 #include "opengl.h"
4 Mesh::Mesh()
5 {
6 nverts = nfaces = 0;
7 for(int i=0; i<NUM_MESH_ATTR; i++) {
8 vbo[i] = 0;
9 }
10 ibo = 0;
11 tang_loc = -1;
12 }
14 Mesh::~Mesh()
15 {
16 destroy();
17 }
19 const char *Mesh::get_name() const
20 {
21 return name.c_str();
22 }
24 bool Mesh::create(const aiScene *scn, aiMesh *aim)
25 {
26 if(vbo[MESH_ATTR_VERTEX]) {
27 destroy();
28 }
30 nverts = aim->mNumVertices;
31 nfaces = aim->mNumFaces;
33 glGenBuffers(1, vbo + MESH_ATTR_VERTEX);
34 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
35 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mVertices, aim->mVertices, GL_STATIC_DRAW);
37 if(aim->mNormals) {
38 glGenBuffers(1, vbo + MESH_ATTR_NORMAL);
39 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]);
40 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mNormals, aim->mNormals, GL_STATIC_DRAW);
41 }
42 if(aim->mTangents) {
43 glGenBuffers(1, vbo + MESH_ATTR_TANGENT);
44 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TANGENT]);
45 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mTangents, aim->mTangents, GL_STATIC_DRAW);
46 }
47 if(aim->mTextureCoords[0]) {
48 glGenBuffers(1, vbo + MESH_ATTR_TEXCOORD);
49 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TEXCOORD]);
50 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mTextureCoords[0], aim->mTextureCoords[0], GL_STATIC_DRAW);
51 }
53 glGenBuffers(1, &ibo);
54 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
55 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 3 * sizeof *aim->mFaces[0].mIndices, 0, GL_STATIC_DRAW);
57 /* map the buffer and fill it up */
58 unsigned int *iptr = (unsigned int*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
60 for(int i=0; i<(int)nfaces; i++) {
61 for(int j=0; j<3; j++) {
62 *iptr++ = aim->mFaces[i].mIndices[j];
63 }
64 }
65 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
66 return true;
67 }
69 void Mesh::destroy()
70 {
71 for(int i=0; i<NUM_MESH_ATTR; i++) {
72 if(vbo[i]) {
73 glDeleteBuffers(1, vbo + i);
74 vbo[i] = 0;
75 }
76 }
77 glDeleteBuffers(1, &ibo);
78 ibo = 0;
80 nverts = nfaces = 0;
81 tang_loc = -1;
82 }
84 void Mesh::set_attrib_location(int attr, int loc)
85 {
86 if(attr == MESH_ATTR_TANGENT) {
87 tang_loc = loc;
88 }
89 }
91 int Mesh::get_attrib_location(int attr) const
92 {
93 return tang_loc;
94 }
96 void Mesh::draw() const
97 {
98 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
99 glVertexPointer(3, GL_FLOAT, 0, 0);
100 glEnableClientState(GL_VERTEX_ARRAY);
102 if(vbo[MESH_ATTR_NORMAL]) {
103 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]);
104 glNormalPointer(GL_FLOAT, 0, 0);
105 glEnableClientState(GL_NORMAL_ARRAY);
106 }
107 if(vbo[MESH_ATTR_TANGENT] && tang_loc >= 0) {
108 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TANGENT]);
109 glVertexAttribPointer(tang_loc, 3, GL_FLOAT, GL_FALSE, 0, 0);
110 glEnableVertexAttribArray(tang_loc);
111 }
112 if(vbo[MESH_ATTR_TEXCOORD]) {
113 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TEXCOORD]);
114 glTexCoordPointer(3, GL_FLOAT, 0, 0);
115 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
116 }
118 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
119 glDrawElements(GL_TRIANGLES, nfaces * 3, GL_UNSIGNED_INT, 0);
121 glDisableClientState(GL_VERTEX_ARRAY);
122 glDisableClientState(GL_NORMAL_ARRAY);
123 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
124 if(tang_loc >= 0) {
125 glDisableVertexAttribArray(tang_loc);
126 }
127 }