nucLeaRsc2

Untitled

Jan 19th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.58 KB | None | 0 0
  1. //#include "lab_glut.hpp"
  2. #include "lab_geometry.hpp"
  3.  
  4. #include <vector>
  5. using namespace std;
  6.  
  7. #define PI 3.14159265359
  8. #define SIN(x) (sin((x) * PI / 180))
  9. #define COS(x) (cos((x) * PI / 180))
  10. #define TAN(x) (SIN(x) / COS(x))
  11.  
  12. struct Vertex {
  13. glm::vec3 position;
  14. glm::vec3 color;
  15.  
  16. Vertex(glm::vec3 position, glm::vec3 color)
  17. {
  18. this->position = position;
  19. this->color = color;
  20. }
  21. };
  22.  
  23. class Shape{
  24. public:
  25. Shape(){}
  26. Shape(glm::vec3 startPosition, int length){
  27. this->startPosition = startPosition;
  28. this->length = length;
  29. this->model_matrix = glm::translate(glm::mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), startPosition);
  30. }
  31.  
  32. void addVertex(Vertex v){
  33. vertices.push_back(Vertex(v.position, v.color));
  34. }
  35.  
  36. void addVertex(double x, double y, double z){
  37. this->addVertex(glm::vec3(x, y, z));
  38. }
  39.  
  40. void addVertex(int x, int y, int z){
  41. this->addVertex(glm::vec3(x, y, z));
  42. }
  43.  
  44. void addVertex(glm::vec3 position){
  45. this->addVertex(Vertex(position, colors));
  46. }
  47.  
  48. void addVertex(glm::vec3 position, glm::vec3 color){
  49. this->addVertex(Vertex(position, color));
  50. }
  51.  
  52. void addIndex(glm::uvec3 positions){
  53. indices.push_back(positions);
  54. }
  55.  
  56. void addIndex(int x, int y, int z){
  57. this->addIndex(glm::uvec3(x, y, z));
  58. }
  59.  
  60. void addIndex(float x, float y, float z){
  61. this->addIndex(glm::uvec3(x, y, z));
  62. }
  63.  
  64. vector<Vertex> vertices;
  65. vector<glm::uvec3> indices;
  66. glm::mat4 model_matrix;
  67. glm::vec3 colors;
  68.  
  69. unsigned int vao, nIndices;
  70. glm::vec3 startPosition;
  71. int length;
  72. virtual void createShape() = 0;
  73.  
  74. ~Shape(){
  75. glDeleteVertexArrays(1, &vao);
  76. }
  77.  
  78. };
  79.  
  80. class Cube : public Shape{
  81. public:
  82.  
  83. Cube() : Cube(glm::vec3(0, 0, 0), 15){}
  84. Cube(glm::vec3 startPosition, int length) : Shape(startPosition, length) {
  85. this->createShape();
  86. }
  87.  
  88. virtual void createShape(){
  89.  
  90. this->addVertex(Vertex(glm::vec3(0, 0, length), glm::vec3(1, 0, 0)));
  91. this->addVertex(Vertex(glm::vec3(length, 0, length), glm::vec3(0, 0, 0)));
  92. this->addVertex(Vertex(glm::vec3(0, length, length), glm::vec3(0, 0, 0)));
  93. this->addVertex(Vertex(glm::vec3(length, length, length), glm::vec3(0, 1, 0)));
  94.  
  95. this->addVertex(Vertex(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0)));
  96. this->addVertex(Vertex(glm::vec3(length, 0, 0), glm::vec3(0, 0, 0)));
  97. this->addVertex(Vertex(glm::vec3(0, length, 0), glm::vec3(0, 0, 1)));
  98. this->addVertex(Vertex(glm::vec3(length, length, 0), glm::vec3(0, 0, 0)));
  99.  
  100. indices.push_back(glm::uvec3(0, 1, 2));
  101. indices.push_back(glm::uvec3(1, 3, 2));
  102. indices.push_back(glm::uvec3(2, 3, 7));
  103. indices.push_back(glm::uvec3(2, 7, 6));
  104. indices.push_back(glm::uvec3(1, 7, 3));
  105. indices.push_back(glm::uvec3(1, 5, 7));
  106. indices.push_back(glm::uvec3(6, 7, 4));
  107. indices.push_back(glm::uvec3(7, 5, 4));
  108. indices.push_back(glm::uvec3(0, 4, 1));
  109. indices.push_back(glm::uvec3(1, 4, 5));
  110. indices.push_back(glm::uvec3(2, 6, 4));
  111. indices.push_back(glm::uvec3(0, 2, 4));
  112.  
  113. }
  114.  
  115. };
  116.  
  117. class Pyramid : public Shape{
  118. public:
  119. Pyramid() :Pyramid(glm::vec3(0, 0, 0), 15, 15){}
  120.  
  121. Pyramid(glm::vec3 startPosition, int length, int height):Shape(startPosition, length){
  122. this->height = height;
  123. this->createShape();
  124. }
  125.  
  126. int height;
  127.  
  128.  
  129. void createShape(){
  130.  
  131. this->addVertex(Vertex(glm::vec3(0, 0, this->length), glm::vec3(1, 0, 0)));
  132. this->addVertex(Vertex(glm::vec3(this->length, 0, this->length), glm::vec3(0, 0.5, 0)));
  133. this->addVertex(Vertex(glm::vec3(this->length / 2, this->height, this->length / 2), glm::vec3(0.1, 0.3, 0)));
  134. this->addVertex(Vertex(glm::vec3(this->length, 0, 0), glm::vec3(0, 0, 0.9)));
  135. this->addVertex(Vertex(glm::vec3(0, 0, 0), glm::vec3(0.3, 0.4, 0.77)));
  136.  
  137. indices.push_back(glm::uvec3(0, 2, 1));
  138. indices.push_back(glm::uvec3(1, 2, 3));
  139. indices.push_back(glm::uvec3(3, 2, 4));
  140. indices.push_back(glm::uvec3(4, 2, 0));
  141. indices.push_back(glm::uvec3(0, 1, 3));
  142. indices.push_back(glm::uvec3(4, 3, 0));
  143.  
  144. }
  145. };
  146.  
  147. class Background : public Shape{
  148. private:
  149. Background(glm::vec3 startPosition, int length) : Shape(startPosition, length) { }
  150.  
  151. public:
  152. Background() : Background(glm::vec3(0, 0, 0), 90, 15, glm::vec3(1,0,0)){}
  153.  
  154. Background(glm::vec3 startPosition, int length, int backgroundQuadSize, glm::vec3 backgroundColor) :Background(startPosition, length){
  155. this->startPosition = startPosition;
  156. this->length = length;
  157. this->backgroundQuadSize = backgroundQuadSize;
  158. this->backgroundColor = backgroundColor;
  159. this->createShape();
  160.  
  161. }
  162.  
  163. int backgroundQuadSize;
  164. glm::vec3 backgroundColor;
  165.  
  166. void createShape(){
  167.  
  168. /* Merg din quadSize in quadSize, pun vertexii si formez quadul nou. La i = 0 nu am ce quad sa formez. */
  169. int n = this->length / this->backgroundQuadSize;
  170. for (int i = 0; i < n; i ++){
  171.  
  172. glm::vec3 color;
  173. /* Retard mode quad creation */
  174. for (int j = 0; j < n; j++){
  175. if ( j < 3) // bulina rosie din colt
  176. color = glm::vec3(1, 0, 0);
  177. else
  178. color = backgroundColor;
  179. //cout << i << " " << j << "\n";
  180. this->createSimpleQuad(i*this->backgroundQuadSize, j*this->backgroundQuadSize, color);
  181. }
  182. }
  183. }
  184.  
  185. void createSimpleQuad(int x, int z, glm::vec3 color){
  186. /* 0 2
  187. 1 3
  188. */
  189.  
  190. int size = vertices.size();
  191.  
  192. vertices.push_back(Vertex(glm::vec3(x, 0, z), color));
  193. vertices.push_back(Vertex(glm::vec3(x, 0, z + this->backgroundQuadSize), color));
  194. vertices.push_back(Vertex(glm::vec3(x + this->backgroundQuadSize, 0, z), color));
  195. vertices.push_back(Vertex(glm::vec3(x + this->backgroundQuadSize, 0, z + this->backgroundQuadSize), color));
  196.  
  197. indices.push_back(glm::uvec3(size, size + 1, size + 2));
  198. indices.push_back(glm::uvec3(size + 1, size + 2, size + 3));
  199.  
  200. }
  201.  
  202. };
  203.  
  204. class Ramp: public Shape{
  205. public:
  206. double height, length, angle;
  207. glm::vec3 endPosition;
  208.  
  209. Ramp(glm::vec3 startPosition, int height) :Ramp(startPosition, height, 100){ }
  210. Ramp(glm::vec3 startPosition, int height, int length) :Ramp(startPosition, height, length, 45){ }
  211. Ramp(glm::vec3 startPosition, int height, int length, int angle):Shape(startPosition, length){
  212. this->startPosition = startPosition;
  213. this->length = length;
  214. this->height = height;
  215. this->angle = angle;
  216. this->createShape();
  217. }
  218.  
  219.  
  220. void createShape(){
  221.  
  222. double height = this->height + this->length;
  223. // piciorul 1
  224. this->colors = glm::vec3(0.1, 0.1, 0.9);
  225. this->addVertex(0.0, 0.0, 0.0);
  226. this->colors = glm::vec3(0.6, 0.1, 0.2);
  227. this->addVertex(0.0, 0.0, length);
  228. this->colors = glm::vec3(0.1, 0.1, 0.9);
  229. this->addVertex(0.0f, this->height + 2 * length / TAN(angle), (double)length);
  230. this->colors = glm::vec3(0.6, 0.1, 0.2);
  231. this->addVertex(0.0, this->height + length / TAN(angle), 0.0);
  232.  
  233. // piciorul 2
  234. this->colors = glm::vec3(0.1, 0.1, 0.9);
  235. this->addVertex(length, 0.0, 0.0);
  236. this->colors = glm::vec3(0.6, 0.1, 0.2);
  237. this->addVertex(length, 0.0, length);
  238. this->colors = glm::vec3(0.1, 0.1, 0.9);
  239. this->addVertex((double)length, this->height + 2 * length / TAN(angle), (double)length);
  240. this->colors = glm::vec3(0.6, 0.1, 0.2);
  241. this->addVertex(length, this->height + length / TAN(angle), 0.0);
  242.  
  243. this->addIndex(0, 1, 2);
  244. this->addIndex(0, 2, 3);
  245. this->addIndex(4, 5, 6);
  246. this->addIndex(4, 6, 7);
  247.  
  248. this->colors = glm::vec3(1, 0.3, 0.1);
  249. double startHeight, newHeight;
  250. double startZ = this->height - height, newZ;
  251.  
  252. startHeight = this->height;
  253. int size;
  254. while (startZ < 100){
  255. size = this->vertices.size();
  256.  
  257. this->addVertex(0.0f, startHeight, startZ);
  258. this->addVertex((float)length, startHeight, startZ);
  259.  
  260. newZ = startZ + 1;
  261. newHeight = startHeight + abs(newZ-startZ) * COS(angle) / SIN(angle);
  262. this->addVertex((float)length, newHeight, newZ);
  263. this->addVertex(0.0f, newHeight, newZ);
  264.  
  265. startHeight = newHeight;
  266. startZ = newZ;
  267.  
  268. this->addIndex(size, size+1, size+2);
  269. this->addIndex(size, size+2, size+3);
  270.  
  271. }
  272.  
  273. size = this->vertices.size();
  274.  
  275. //piciorul 3
  276. this->colors = glm::vec3(0.1, 0.1, 0.9);
  277. this->addVertex(0.0, 0.0, startZ-length);
  278. this->colors = glm::vec3(0.6, 0.1, 0.2);
  279. this->addVertex(0.0, 0.0, startZ);
  280. this->colors = glm::vec3(0.1, 0.1, 0.9);
  281. this->addVertex(0.0f, startHeight, (double)startZ);
  282. this->colors = glm::vec3(0.6, 0.1, 0.2);
  283. this->addVertex(0.0, startHeight - length * COS(angle) / SIN(angle), startZ - length);
  284.  
  285. // piciorul 4
  286. this->colors = glm::vec3(0.1, 0.1, 0.9);
  287. this->addVertex(length, 0.0, startZ-length);
  288. this->colors = glm::vec3(0.6, 0.1, 0.2);
  289. this->addVertex(length, 0.0, startZ);
  290. this->colors = glm::vec3(0.1, 0.1, 0.9);
  291. this->addVertex((double)length, startHeight, (double)startZ);
  292. this->colors = glm::vec3(0.6, 0.1, 0.2);
  293. this->addVertex(length, startHeight - length * COS(angle) / SIN(angle), startZ - length);
  294.  
  295. this->addIndex(size, size+1, size+2);
  296. this->addIndex(size, size+2, size+3);
  297. this->addIndex(size+4, size+5, size+6);
  298. this->addIndex(size+4, size+6, size+7);
  299.  
  300.  
  301. this->endPosition = glm::vec3( startPosition.x, startHeight + startPosition.y, startZ + startPosition.z);
  302. }
  303. };
  304.  
  305. si
  306.  
  307.  
  308. void sendAndBindData(Shape* shape){
  309. this->sendAndBindData(shape->vertices, shape->indices, &shape->vao, &shape->nIndices);
  310. }
  311.  
  312. void sendAndBindData(vector<Vertex> vertices, vector<glm::uvec3> indices, unsigned int* vao, unsigned int* nIndices){
  313. //creeaza obiectele OpenGL necesare desenarii
  314.  
  315. //unsigned int gl_vertex_array_object, gl_vertex_buffer_object, gl_index_buffer_object;
  316.  
  317. unsigned int local_VAO, local_VBO, local_IBO;
  318.  
  319. // VAO -> un obiect ce reprezinta un container pentru starea de desenare
  320. // Generez un VAO cu dimensiunea de UN VBO
  321. glGenVertexArrays(1, &local_VAO);
  322. // Bindez acest VAO -- Voi repeta acest pas la desenare pentru a-l trimite la draw pe acesta (ca si activ)
  323. glBindVertexArray(local_VAO);
  324.  
  325. // VBO -> un obiect in care tinem vertecsii
  326. // Generez UN VBO
  327. glGenBuffers(1, &local_VBO);
  328. // Bindez acest VBO global
  329. glBindBuffer(GL_ARRAY_BUFFER, local_VBO);
  330. // Ii setez datele ca fiind punctele definite in vertices in acest moment
  331. glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
  332.  
  333. // WIKI: These functions say that the attribute index index​ will get its attribute data from whatever buffer object is currently bound to GL_ARRAY_BUFFER​.
  334. // Deci, vor fi trimise catre SHADER datele din bufferul bindat in acest moment de tip GL_ARRAY_BUFFER (deci pot sa pun aceste apeluri si dupa bindarea IBO-ului, ca el e de alt tip (GL_ELEMENT_ARRAY_BUFFER)
  335. unsigned int in_position_pipe = glGetAttribLocation(gl_program_shader, "in_position");
  336. glEnableVertexAttribArray(in_position_pipe);
  337. glVertexAttribPointer(in_position_pipe, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
  338.  
  339. unsigned int in_color_pipe = glGetAttribLocation(gl_program_shader, "in_color");
  340. glEnableVertexAttribArray(in_color_pipe);
  341. glVertexAttribPointer(in_color_pipe, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(float)));
  342.  
  343.  
  344. //IBO -> un obiect in care tinem indecsii
  345. // Generez UN IBO
  346. glGenBuffers(1, &local_IBO);
  347. // Il bindez global ca fiind cel curent folosit
  348. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, local_IBO);
  349. // Ii setez datele ca fiind datele din indeces
  350. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(glm::uvec3), &indices[0], GL_STATIC_DRAW);
  351.  
  352. *vao = local_VAO;
  353. *nIndices = indices.size() * 3;
  354. }
  355.  
  356.  
  357.  
  358. void Tema::sendAndBindData(Shape* shape){
  359. this->sendAndBindData(shape->vertices, shape->indices, &shape->vao, &shape->nIndices);
  360. }
  361.  
  362. void Tema::createInitialObjects(){
  363.  
  364. Cube cube = new Cube();
  365. this->sendAndBindData(cube);
  366. }
Advertisement
Add Comment
Please, Sign In to add comment