PageRenderTime 43ms CodeModel.GetById 2ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

/src/imac2gl3/shapes/MonsterCube.cpp

https://bitbucket.org/Elfhir/iminecraft-1.0
C++ | 221 lines | 170 code | 31 blank | 20 comment | 0 complexity | 9c496fdcb6263bdcc3bf38f50d2d8ca0 MD5 | raw file
  1#include <cmath>
  2#include <vector>
  3#include <iostream>
  4#include "imac2gl3/shapes/common.hpp"
  5#include "imac2gl3/shapes/MonsterCube.hpp"
  6
  7namespace imac2gl3 {
  8
  9    void MonsterCube::build(int type) {
 10        std::vector<ShapeVertex> data;
 11        ShapeVertex vertex;
 12
 13        type --;
 14
 15        int nbTextureMax = 1; // nombre de type different max sur notre texture, a faire evoluer pour de la dynamique
 16        float division_1_sur_3 = 1/3.f;
 17        float portionParType = 1.f/(float)nbTextureMax;
 18
 19// FRONT BOTTOM
 20// 1,0,0
 21        vertex.texCoords.x = 2*division_1_sur_3;
 22        vertex.texCoords.y = (type+1)*portionParType;    
 23        vertex.position.x = 0.5f;
 24        vertex.position.y = 0.f;
 25        vertex.position.z = -0.5f;
 26        vertex.normal.x = 1.f;
 27        vertex.normal.y = -1.f;
 28        vertex.normal.z = 1.f;
 29        vertex.normal = glm::normalize(vertex.normal);    
 30        data.push_back(vertex);
 31
 32// 0,0,0
 33        vertex.texCoords.x = division_1_sur_3;
 34        vertex.texCoords.y = (type+1)*portionParType;    
 35        vertex.position.x = -0.5f;
 36        vertex.position.y = 0.f;
 37        vertex.position.z = -0.5f;
 38        vertex.normal.x = -1.f;
 39        vertex.normal.y = -1.f;
 40        vertex.normal.z = 1.f;
 41        vertex.normal = glm::normalize(vertex.normal);    
 42        data.push_back(vertex);
 43
 44// FRONT UP
 45// 0,1,0
 46        vertex.texCoords.x = division_1_sur_3;
 47        vertex.texCoords.y = type*portionParType;    
 48        vertex.position.x = -0.5f;
 49        vertex.position.y = 1.f;
 50        vertex.position.z = -0.5f;
 51        vertex.normal.x = -1.f;
 52        vertex.normal.y = 1.f;
 53        vertex.normal.z = 1.f;
 54        vertex.normal = glm::normalize(vertex.normal);    
 55        data.push_back(vertex);
 56// 1,1,0
 57        vertex.texCoords.x = 2*division_1_sur_3;
 58        vertex.texCoords.y = type*portionParType;    
 59        vertex.position.x = 0.5f;
 60        vertex.position.y = 1.f;
 61        vertex.position.z = -0.5f;
 62        vertex.normal.x = 1.f;
 63        vertex.normal.y = 1.f;
 64        vertex.normal.z = 1.f;
 65        vertex.normal = glm::normalize(vertex.normal);    
 66        data.push_back(vertex);
 67
 68// BACK BOTTOM
 69// 1,0,1
 70        vertex.texCoords.x = 1.f;
 71        vertex.texCoords.y = 1.f;    
 72        vertex.position.x = 0.5f;
 73        vertex.position.y = 0.f;
 74        vertex.position.z = 0.5f;
 75        vertex.normal.x = 1.f;
 76        vertex.normal.y = -1.f;
 77        vertex.normal.z = -1.f;
 78        vertex.normal = glm::normalize(vertex.normal);    
 79        data.push_back(vertex);
 80
 81// 0,0,1
 82        vertex.texCoords.x = 0.f;
 83        vertex.texCoords.y = 1.f;    
 84        vertex.position.x = -0.5f;
 85        vertex.position.y = 0.f;
 86        vertex.position.z = 0.5f;
 87        vertex.normal.x = -1.f;
 88        vertex.normal.y = -1.f;
 89        vertex.normal.z = -1.f;
 90        vertex.normal = glm::normalize(vertex.normal);    
 91        data.push_back(vertex);
 92
 93// BACK UP
 94// 0,1,1
 95        vertex.texCoords.x = 0.f;
 96        vertex.texCoords.y = 0.f;    
 97        vertex.position.x = -0.5f;
 98        vertex.position.y = 1.f;
 99        vertex.position.z = 0.5f;
100        vertex.normal.x = -1.f;
101        vertex.normal.y = 1.f;
102        vertex.normal.z = -1.f;
103        vertex.normal = glm::normalize(vertex.normal);    
104        data.push_back(vertex);
105// 1,1,1
106        vertex.texCoords.x = 1.f;
107        vertex.texCoords.y = 0.f;    
108        vertex.position.x = 0.5f;
109        vertex.position.y = 1.f;
110        vertex.position.z = 0.5f;
111        vertex.normal.x = 1.f;
112        vertex.normal.y = 1.f;
113        vertex.normal.z = -1.f;
114        vertex.normal = glm::normalize(vertex.normal);    
115        data.push_back(vertex);
116
117
118
119
120        m_nVertexCount = 6 * 6; // 6 faces * 6 points (2 triangles)
121        m_pDataPointer = new ShapeVertex[m_nVertexCount];
122        
123
124        GLsizei i = 0;
125// FRONT
126        m_pDataPointer[i] = data[5];m_pDataPointer[i].texCoords.x = 2*division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
127        i++;
128        m_pDataPointer[i] = data[4];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
129        i++;
130        m_pDataPointer[i] = data[7];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = type*portionParType;
131        i++;
132
133        m_pDataPointer[i] = data[7];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = type*portionParType;
134        i++;
135        m_pDataPointer[i] = data[6];m_pDataPointer[i].texCoords.x = 2*division_1_sur_3;m_pDataPointer[i].texCoords.y = type*portionParType;
136        i++;
137        m_pDataPointer[i] = data[5];m_pDataPointer[i].texCoords.x = 2*division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
138        i++;
139
140// BOTTOM
141        m_pDataPointer[i] = data[4];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
142        i++;
143        m_pDataPointer[i] = data[5];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
144        i++;
145        m_pDataPointer[i] = data[1];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = type*portionParType;
146        i++;
147
148        m_pDataPointer[i] = data[1];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = type*portionParType;
149        i++;
150        m_pDataPointer[i] = data[0];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = type*portionParType;
151        i++;
152        m_pDataPointer[i] = data[4];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
153        i++;
154
155// BACK
156        m_pDataPointer[i] = data[0];m_pDataPointer[i].texCoords.x = 1;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
157        i++;
158        m_pDataPointer[i] = data[1];m_pDataPointer[i].texCoords.x = 2*division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
159        i++;
160        m_pDataPointer[i] = data[2];m_pDataPointer[i].texCoords.x = 2*division_1_sur_3;m_pDataPointer[i].texCoords.y = type*portionParType;
161        i++;
162
163        m_pDataPointer[i] = data[2];m_pDataPointer[i].texCoords.x = 2*division_1_sur_3;m_pDataPointer[i].texCoords.y = type*portionParType;
164        i++;
165        m_pDataPointer[i] = data[3];m_pDataPointer[i].texCoords.x = 1;m_pDataPointer[i].texCoords.y = type*portionParType;
166        i++;
167        m_pDataPointer[i] = data[0];m_pDataPointer[i].texCoords.x = 1;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
168        i++;
169
170// TOP
171        m_pDataPointer[i] = data[3];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
172        i++;
173        m_pDataPointer[i] = data[2];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
174        i++;
175        m_pDataPointer[i] = data[6];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = type*portionParType;
176        i++;
177
178        m_pDataPointer[i] = data[6];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = type*portionParType;
179        i++;
180        m_pDataPointer[i] = data[7];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = type*portionParType;
181        i++;
182        m_pDataPointer[i] = data[3];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
183        i++;
184
185// LEFT
186        m_pDataPointer[i] = data[1];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
187        i++;
188        m_pDataPointer[i] = data[5];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
189        i++;
190        m_pDataPointer[i] = data[6];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = type*portionParType;
191        i++;
192
193        m_pDataPointer[i] = data[6];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = type*portionParType;
194        i++;
195        m_pDataPointer[i] = data[2];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = type*portionParType;
196        i++;
197        m_pDataPointer[i] = data[1];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
198        i++;
199
200// RIGHT
201        m_pDataPointer[i] = data[4];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
202        i++;
203        m_pDataPointer[i] = data[0];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
204        i++;
205        m_pDataPointer[i] = data[3];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = type*portionParType;
206        i++;
207
208        m_pDataPointer[i] = data[3];m_pDataPointer[i].texCoords.x = 0;m_pDataPointer[i].texCoords.y = type*portionParType;
209        i++;
210        m_pDataPointer[i] = data[7];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = type*portionParType;
211        i++;
212        m_pDataPointer[i] = data[4];m_pDataPointer[i].texCoords.x = division_1_sur_3;m_pDataPointer[i].texCoords.y = (type+1)*portionParType;
213        i++;
214
215         
216        // Attention ! dans cette implantation on duplique beaucoup de sommets. Une meilleur strat├ęgie est de passer
217        // par un Index Buffer Object, que nous verrons dans les prochains TDs
218    }
219}
220
221