Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include "lab_glut.hpp"
- #include "lab_geometry.hpp"
- #include <vector>
- using namespace std;
- #define PI 3.14159265359
- #define SIN(x) (sin((x) * PI / 180))
- #define COS(x) (cos((x) * PI / 180))
- #define TAN(x) (SIN(x) / COS(x))
- struct Vertex {
- glm::vec3 position;
- glm::vec3 color;
- Vertex(glm::vec3 position, glm::vec3 color)
- {
- this->position = position;
- this->color = color;
- }
- };
- class Shape{
- public:
- Shape(){}
- Shape(glm::vec3 startPosition, int length){
- this->startPosition = startPosition;
- this->length = length;
- this->model_matrix = glm::translate(glm::mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), startPosition);
- }
- void addVertex(Vertex v){
- vertices.push_back(Vertex(v.position, v.color));
- }
- void addVertex(double x, double y, double z){
- this->addVertex(glm::vec3(x, y, z));
- }
- void addVertex(int x, int y, int z){
- this->addVertex(glm::vec3(x, y, z));
- }
- void addVertex(glm::vec3 position){
- this->addVertex(Vertex(position, colors));
- }
- void addVertex(glm::vec3 position, glm::vec3 color){
- this->addVertex(Vertex(position, color));
- }
- void addIndex(glm::uvec3 positions){
- indices.push_back(positions);
- }
- void addIndex(int x, int y, int z){
- this->addIndex(glm::uvec3(x, y, z));
- }
- void addIndex(float x, float y, float z){
- this->addIndex(glm::uvec3(x, y, z));
- }
- vector<Vertex> vertices;
- vector<glm::uvec3> indices;
- glm::mat4 model_matrix;
- glm::vec3 colors;
- unsigned int vao, nIndices;
- glm::vec3 startPosition;
- int length;
- virtual void createShape() = 0;
- ~Shape(){
- glDeleteVertexArrays(1, &vao);
- }
- };
- class Cube : public Shape{
- public:
- Cube() : Cube(glm::vec3(0, 0, 0), 15){}
- Cube(glm::vec3 startPosition, int length) : Shape(startPosition, length) {
- this->createShape();
- }
- virtual void createShape(){
- this->addVertex(Vertex(glm::vec3(0, 0, length), glm::vec3(1, 0, 0)));
- this->addVertex(Vertex(glm::vec3(length, 0, length), glm::vec3(0, 0, 0)));
- this->addVertex(Vertex(glm::vec3(0, length, length), glm::vec3(0, 0, 0)));
- this->addVertex(Vertex(glm::vec3(length, length, length), glm::vec3(0, 1, 0)));
- this->addVertex(Vertex(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0)));
- this->addVertex(Vertex(glm::vec3(length, 0, 0), glm::vec3(0, 0, 0)));
- this->addVertex(Vertex(glm::vec3(0, length, 0), glm::vec3(0, 0, 1)));
- this->addVertex(Vertex(glm::vec3(length, length, 0), glm::vec3(0, 0, 0)));
- indices.push_back(glm::uvec3(0, 1, 2));
- indices.push_back(glm::uvec3(1, 3, 2));
- indices.push_back(glm::uvec3(2, 3, 7));
- indices.push_back(glm::uvec3(2, 7, 6));
- indices.push_back(glm::uvec3(1, 7, 3));
- indices.push_back(glm::uvec3(1, 5, 7));
- indices.push_back(glm::uvec3(6, 7, 4));
- indices.push_back(glm::uvec3(7, 5, 4));
- indices.push_back(glm::uvec3(0, 4, 1));
- indices.push_back(glm::uvec3(1, 4, 5));
- indices.push_back(glm::uvec3(2, 6, 4));
- indices.push_back(glm::uvec3(0, 2, 4));
- }
- };
- class Pyramid : public Shape{
- public:
- Pyramid() :Pyramid(glm::vec3(0, 0, 0), 15, 15){}
- Pyramid(glm::vec3 startPosition, int length, int height):Shape(startPosition, length){
- this->height = height;
- this->createShape();
- }
- int height;
- void createShape(){
- this->addVertex(Vertex(glm::vec3(0, 0, this->length), glm::vec3(1, 0, 0)));
- this->addVertex(Vertex(glm::vec3(this->length, 0, this->length), glm::vec3(0, 0.5, 0)));
- this->addVertex(Vertex(glm::vec3(this->length / 2, this->height, this->length / 2), glm::vec3(0.1, 0.3, 0)));
- this->addVertex(Vertex(glm::vec3(this->length, 0, 0), glm::vec3(0, 0, 0.9)));
- this->addVertex(Vertex(glm::vec3(0, 0, 0), glm::vec3(0.3, 0.4, 0.77)));
- indices.push_back(glm::uvec3(0, 2, 1));
- indices.push_back(glm::uvec3(1, 2, 3));
- indices.push_back(glm::uvec3(3, 2, 4));
- indices.push_back(glm::uvec3(4, 2, 0));
- indices.push_back(glm::uvec3(0, 1, 3));
- indices.push_back(glm::uvec3(4, 3, 0));
- }
- };
- class Background : public Shape{
- private:
- Background(glm::vec3 startPosition, int length) : Shape(startPosition, length) { }
- public:
- Background() : Background(glm::vec3(0, 0, 0), 90, 15, glm::vec3(1,0,0)){}
- Background(glm::vec3 startPosition, int length, int backgroundQuadSize, glm::vec3 backgroundColor) :Background(startPosition, length){
- this->startPosition = startPosition;
- this->length = length;
- this->backgroundQuadSize = backgroundQuadSize;
- this->backgroundColor = backgroundColor;
- this->createShape();
- }
- int backgroundQuadSize;
- glm::vec3 backgroundColor;
- void createShape(){
- /* Merg din quadSize in quadSize, pun vertexii si formez quadul nou. La i = 0 nu am ce quad sa formez. */
- int n = this->length / this->backgroundQuadSize;
- for (int i = 0; i < n; i ++){
- glm::vec3 color;
- /* Retard mode quad creation */
- for (int j = 0; j < n; j++){
- if ( j < 3) // bulina rosie din colt
- color = glm::vec3(1, 0, 0);
- else
- color = backgroundColor;
- //cout << i << " " << j << "\n";
- this->createSimpleQuad(i*this->backgroundQuadSize, j*this->backgroundQuadSize, color);
- }
- }
- }
- void createSimpleQuad(int x, int z, glm::vec3 color){
- /* 0 2
- 1 3
- */
- int size = vertices.size();
- vertices.push_back(Vertex(glm::vec3(x, 0, z), color));
- vertices.push_back(Vertex(glm::vec3(x, 0, z + this->backgroundQuadSize), color));
- vertices.push_back(Vertex(glm::vec3(x + this->backgroundQuadSize, 0, z), color));
- vertices.push_back(Vertex(glm::vec3(x + this->backgroundQuadSize, 0, z + this->backgroundQuadSize), color));
- indices.push_back(glm::uvec3(size, size + 1, size + 2));
- indices.push_back(glm::uvec3(size + 1, size + 2, size + 3));
- }
- };
- class Ramp: public Shape{
- public:
- double height, length, angle;
- glm::vec3 endPosition;
- Ramp(glm::vec3 startPosition, int height) :Ramp(startPosition, height, 100){ }
- Ramp(glm::vec3 startPosition, int height, int length) :Ramp(startPosition, height, length, 45){ }
- Ramp(glm::vec3 startPosition, int height, int length, int angle):Shape(startPosition, length){
- this->startPosition = startPosition;
- this->length = length;
- this->height = height;
- this->angle = angle;
- this->createShape();
- }
- void createShape(){
- double height = this->height + this->length;
- // piciorul 1
- this->colors = glm::vec3(0.1, 0.1, 0.9);
- this->addVertex(0.0, 0.0, 0.0);
- this->colors = glm::vec3(0.6, 0.1, 0.2);
- this->addVertex(0.0, 0.0, length);
- this->colors = glm::vec3(0.1, 0.1, 0.9);
- this->addVertex(0.0f, this->height + 2 * length / TAN(angle), (double)length);
- this->colors = glm::vec3(0.6, 0.1, 0.2);
- this->addVertex(0.0, this->height + length / TAN(angle), 0.0);
- // piciorul 2
- this->colors = glm::vec3(0.1, 0.1, 0.9);
- this->addVertex(length, 0.0, 0.0);
- this->colors = glm::vec3(0.6, 0.1, 0.2);
- this->addVertex(length, 0.0, length);
- this->colors = glm::vec3(0.1, 0.1, 0.9);
- this->addVertex((double)length, this->height + 2 * length / TAN(angle), (double)length);
- this->colors = glm::vec3(0.6, 0.1, 0.2);
- this->addVertex(length, this->height + length / TAN(angle), 0.0);
- this->addIndex(0, 1, 2);
- this->addIndex(0, 2, 3);
- this->addIndex(4, 5, 6);
- this->addIndex(4, 6, 7);
- this->colors = glm::vec3(1, 0.3, 0.1);
- double startHeight, newHeight;
- double startZ = this->height - height, newZ;
- startHeight = this->height;
- int size;
- while (startZ < 100){
- size = this->vertices.size();
- this->addVertex(0.0f, startHeight, startZ);
- this->addVertex((float)length, startHeight, startZ);
- newZ = startZ + 1;
- newHeight = startHeight + abs(newZ-startZ) * COS(angle) / SIN(angle);
- this->addVertex((float)length, newHeight, newZ);
- this->addVertex(0.0f, newHeight, newZ);
- startHeight = newHeight;
- startZ = newZ;
- this->addIndex(size, size+1, size+2);
- this->addIndex(size, size+2, size+3);
- }
- size = this->vertices.size();
- //piciorul 3
- this->colors = glm::vec3(0.1, 0.1, 0.9);
- this->addVertex(0.0, 0.0, startZ-length);
- this->colors = glm::vec3(0.6, 0.1, 0.2);
- this->addVertex(0.0, 0.0, startZ);
- this->colors = glm::vec3(0.1, 0.1, 0.9);
- this->addVertex(0.0f, startHeight, (double)startZ);
- this->colors = glm::vec3(0.6, 0.1, 0.2);
- this->addVertex(0.0, startHeight - length * COS(angle) / SIN(angle), startZ - length);
- // piciorul 4
- this->colors = glm::vec3(0.1, 0.1, 0.9);
- this->addVertex(length, 0.0, startZ-length);
- this->colors = glm::vec3(0.6, 0.1, 0.2);
- this->addVertex(length, 0.0, startZ);
- this->colors = glm::vec3(0.1, 0.1, 0.9);
- this->addVertex((double)length, startHeight, (double)startZ);
- this->colors = glm::vec3(0.6, 0.1, 0.2);
- this->addVertex(length, startHeight - length * COS(angle) / SIN(angle), startZ - length);
- this->addIndex(size, size+1, size+2);
- this->addIndex(size, size+2, size+3);
- this->addIndex(size+4, size+5, size+6);
- this->addIndex(size+4, size+6, size+7);
- this->endPosition = glm::vec3( startPosition.x, startHeight + startPosition.y, startZ + startPosition.z);
- }
- };
- si
- void sendAndBindData(Shape* shape){
- this->sendAndBindData(shape->vertices, shape->indices, &shape->vao, &shape->nIndices);
- }
- void sendAndBindData(vector<Vertex> vertices, vector<glm::uvec3> indices, unsigned int* vao, unsigned int* nIndices){
- //creeaza obiectele OpenGL necesare desenarii
- //unsigned int gl_vertex_array_object, gl_vertex_buffer_object, gl_index_buffer_object;
- unsigned int local_VAO, local_VBO, local_IBO;
- // VAO -> un obiect ce reprezinta un container pentru starea de desenare
- // Generez un VAO cu dimensiunea de UN VBO
- glGenVertexArrays(1, &local_VAO);
- // Bindez acest VAO -- Voi repeta acest pas la desenare pentru a-l trimite la draw pe acesta (ca si activ)
- glBindVertexArray(local_VAO);
- // VBO -> un obiect in care tinem vertecsii
- // Generez UN VBO
- glGenBuffers(1, &local_VBO);
- // Bindez acest VBO global
- glBindBuffer(GL_ARRAY_BUFFER, local_VBO);
- // Ii setez datele ca fiind punctele definite in vertices in acest moment
- glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
- // 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.
- // 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)
- unsigned int in_position_pipe = glGetAttribLocation(gl_program_shader, "in_position");
- glEnableVertexAttribArray(in_position_pipe);
- glVertexAttribPointer(in_position_pipe, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
- unsigned int in_color_pipe = glGetAttribLocation(gl_program_shader, "in_color");
- glEnableVertexAttribArray(in_color_pipe);
- glVertexAttribPointer(in_color_pipe, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(float)));
- //IBO -> un obiect in care tinem indecsii
- // Generez UN IBO
- glGenBuffers(1, &local_IBO);
- // Il bindez global ca fiind cel curent folosit
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, local_IBO);
- // Ii setez datele ca fiind datele din indeces
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(glm::uvec3), &indices[0], GL_STATIC_DRAW);
- *vao = local_VAO;
- *nIndices = indices.size() * 3;
- }
- void Tema::sendAndBindData(Shape* shape){
- this->sendAndBindData(shape->vertices, shape->indices, &shape->vao, &shape->nIndices);
- }
- void Tema::createInitialObjects(){
- Cube cube = new Cube();
- this->sendAndBindData(cube);
- }
Advertisement
Add Comment
Please, Sign In to add comment