dungeon_crawler

view prototype/src/mesh.cc @ 5:252a00508411

more stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 12 Aug 2012 07:07:57 +0300
parents 158de53b4e18
children 8fb37db44fd8
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 name = aim->mName.data;
32 nverts = aim->mNumVertices;
33 nfaces = aim->mNumFaces;
35 glGenBuffers(1, vbo + MESH_ATTR_VERTEX);
36 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
37 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mVertices, aim->mVertices, GL_STATIC_DRAW);
39 if(aim->mNormals) {
40 glGenBuffers(1, vbo + MESH_ATTR_NORMAL);
41 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]);
42 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mNormals, aim->mNormals, GL_STATIC_DRAW);
43 }
44 if(aim->mTangents) {
45 glGenBuffers(1, vbo + MESH_ATTR_TANGENT);
46 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TANGENT]);
47 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mTangents, aim->mTangents, GL_STATIC_DRAW);
48 }
49 if(aim->mTextureCoords[0]) {
50 glGenBuffers(1, vbo + MESH_ATTR_TEXCOORD);
51 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TEXCOORD]);
52 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mTextureCoords[0], aim->mTextureCoords[0], GL_STATIC_DRAW);
53 }
55 glGenBuffers(1, &ibo);
56 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
57 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 3 * sizeof *aim->mFaces[0].mIndices, 0, GL_STATIC_DRAW);
59 /* map the buffer and fill it up */
60 unsigned int *iptr = (unsigned int*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
62 for(int i=0; i<(int)nfaces; i++) {
63 for(int j=0; j<3; j++) {
64 *iptr++ = aim->mFaces[i].mIndices[j];
65 }
66 }
67 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
68 return true;
69 }
71 void Mesh::destroy()
72 {
73 for(int i=0; i<NUM_MESH_ATTR; i++) {
74 if(vbo[i]) {
75 glDeleteBuffers(1, vbo + i);
76 vbo[i] = 0;
77 }
78 }
79 glDeleteBuffers(1, &ibo);
80 ibo = 0;
82 nverts = nfaces = 0;
83 tang_loc = -1;
84 }
86 void Mesh::set_attrib_location(int attr, int loc)
87 {
88 if(attr == MESH_ATTR_TANGENT) {
89 tang_loc = loc;
90 }
91 }
93 int Mesh::get_attrib_location(int attr) const
94 {
95 return tang_loc;
96 }
98 void Mesh::draw() const
99 {
100 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
101 glVertexPointer(3, GL_FLOAT, 0, 0);
102 glEnableClientState(GL_VERTEX_ARRAY);
104 if(vbo[MESH_ATTR_NORMAL]) {
105 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]);
106 glNormalPointer(GL_FLOAT, 0, 0);
107 glEnableClientState(GL_NORMAL_ARRAY);
108 }
109 if(vbo[MESH_ATTR_TANGENT] && tang_loc >= 0) {
110 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TANGENT]);
111 glVertexAttribPointer(tang_loc, 3, GL_FLOAT, GL_FALSE, 0, 0);
112 glEnableVertexAttribArray(tang_loc);
113 }
114 if(vbo[MESH_ATTR_TEXCOORD]) {
115 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TEXCOORD]);
116 glTexCoordPointer(3, GL_FLOAT, 0, 0);
117 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
118 }
120 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
121 glDrawElements(GL_TRIANGLES, nfaces * 3, GL_UNSIGNED_INT, 0);
123 glDisableClientState(GL_VERTEX_ARRAY);
124 glDisableClientState(GL_NORMAL_ARRAY);
125 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
126 if(tang_loc >= 0) {
127 glDisableVertexAttribArray(tang_loc);
128 }
129 }