Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #include "Window.h"
  2.  
  3. #include "Shader.h"
  4. #include "ShaderProgram.h"
  5.  
  6. #include "Mesh.h"
  7.  
  8. #include "Camera.h"
  9.  
  10. #include "Time.h"
  11.  
  12. #include <iostream>
  13.  
  14. #include "DrawableObject.h"
  15.  
  16. int main()
  17. {
  18.     Window window(1680, 1050, "VoxelCraft", GL_FALSE);
  19.     window.setClearColor(0.59765625f, 0.796875f, 0.99609375f, 1.0f);
  20.  
  21.     Shader vertexShader("vertexShader", GL_VERTEX_SHADER);
  22.     Shader fragmentShader("fragmentShader", GL_FRAGMENT_SHADER);
  23.  
  24.     ShaderProgram shaderProgram(vertexShader, fragmentShader);
  25.     shaderProgram.bind();
  26.  
  27.     Camera camera(glm::vec3(0.0f, 0.0f, 0.0f), 70.0f, (GLfloat)window.getWidth() / (GLfloat)window.getHeight(), 0.01f, 1000.0f, shaderProgram, 10.5f, 0.05f);
  28.  
  29.     /* Chunk */
  30.     enum BlockTypes
  31.     {
  32.         AIR = 0,
  33.         GRASS = 1
  34.     };
  35.  
  36.     static const GLuint size = 16;
  37.  
  38.     GLuint blockIDs[size + 1][size + 1][size + 1];
  39.  
  40.     Mesh mesh;
  41.  
  42.     Texture texture(shaderProgram, "grass", "outTexture", 0);
  43.  
  44.     for (GLuint x = 0; x < size; x++)
  45.     {
  46.         for (GLuint y = 0; y < size; y++)
  47.         {
  48.             for (GLuint z = 0; z < size; z++)
  49.             {
  50.                 blockIDs[x][y][z] = GRASS;
  51.             }
  52.         }
  53.     }
  54.  
  55.     std::vector<glm::vec3> positions;
  56.  
  57.     for (GLuint x = 0; x < size; x++)
  58.     {
  59.         for (GLuint y = 0; y < size; y++)
  60.         {
  61.             for (GLuint z = 0; z < size; z++)
  62.             {
  63.                 if (blockIDs[x + 1][y][z] != GRASS)
  64.                 {
  65.                     mesh.addData(glm::vec3(1.0f, 0.0f, 1.0f), glm::vec2(0.0f, 1.0f));
  66.                     mesh.addData(glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f));
  67.                     mesh.addData(glm::vec3(1.0f, 1.0f, 0.0f), glm::vec2(1.0f, 0.0f));
  68.                     mesh.addData(glm::vec3(1.0f, 1.0f, 1.0f), glm::vec2(1.0f, 1.0f));
  69.                
  70.                     positions.push_back(glm::vec3(x, y, z));
  71.                 }
  72.                 if (blockIDs[x - 1][y][z] != GRASS)
  73.                 {
  74.                     mesh.addData(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec2(0.0f, 1.0f));
  75.                     mesh.addData(glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(0.0f, 0.0f));
  76.                     mesh.addData(glm::vec3(0.0f, 1.0f, 1.0f), glm::vec2(1.0f, 0.0f));
  77.                     mesh.addData(glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(1.0f, 1.0f));
  78.                
  79.                     positions.push_back(glm::vec3(x, y, z));
  80.                 }
  81.  
  82.                 if (blockIDs[x][y + 1][z] != GRASS)
  83.                 {
  84.                     mesh.addData(glm::vec3(0.0f, 1.0f, 1.0f), glm::vec2(0.0f, 1.0f));
  85.                     mesh.addData(glm::vec3(1.0f, 1.0f, 1.0f), glm::vec2(0.0f, 0.0f));
  86.                     mesh.addData(glm::vec3(1.0f, 1.0f, 0.0f), glm::vec2(1.0f, 0.0f));
  87.                     mesh.addData(glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(1.0f, 1.0f));
  88.                
  89.                     positions.push_back(glm::vec3(x, y, z));
  90.                 }
  91.                 if (blockIDs[x][y - 1][z] != GRASS)
  92.                 {
  93.                     mesh.addData(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec2(0.0f, 1.0f));
  94.                     mesh.addData(glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f));
  95.                     mesh.addData(glm::vec3(1.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f));
  96.                     mesh.addData(glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 1.0f));
  97.            
  98.                     positions.push_back(glm::vec3(x, y, z));
  99.                 }
  100.  
  101.                 if (blockIDs[x][y][z + 1] != GRASS)
  102.                 {
  103.                     mesh.addData(glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(0.0f, 1.0f));
  104.                     mesh.addData(glm::vec3(1.0f, 0.0f, 1.0f), glm::vec2(0.0f, 0.0f));
  105.                     mesh.addData(glm::vec3(1.0f, 1.0f, 1.0f), glm::vec2(1.0f, 0.0f));
  106.                     mesh.addData(glm::vec3(0.0f, 1.0f, 1.0f), glm::vec2(1.0f, 1.0f));
  107.                
  108.                     positions.push_back(glm::vec3(x, y, z));
  109.                 }
  110.                 if (blockIDs[x][y][z - 1] != GRASS)
  111.                 {              
  112.                     mesh.addData(glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 1.0f));
  113.                     mesh.addData(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f));
  114.                     mesh.addData(glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(1.0f, 0.0f));
  115.                     mesh.addData(glm::vec3(1.0f, 1.0f, 0.0f), glm::vec2(1.0f, 1.0f));
  116.            
  117.                     positions.push_back(glm::vec3(x, y, z));
  118.                 }
  119.             }
  120.         }
  121.     }
  122.  
  123.     mesh.setIndices({
  124.         0, 1, 2,
  125.         2, 3, 0,
  126.  
  127.         4, 5, 6,
  128.         6, 7, 4,
  129.  
  130.         8, 9, 10,
  131.         10, 11, 8,
  132.  
  133.         12, 13, 14,
  134.         14, 15, 12,
  135.  
  136.         16, 17, 18,
  137.         18, 19, 16,
  138.  
  139.         20, 21, 22,
  140.         22, 23, 20
  141.     });
  142.  
  143.     mesh.upload();
  144.  
  145.     DrawableObject block(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), texture, &mesh);
  146.     /* End of chunk */
  147.  
  148.     Time time;
  149.  
  150.     while (window.isOpen())
  151.     {
  152.         time.startCounting();
  153.  
  154.         window.clear();
  155.  
  156.         camera.update(window, shaderProgram, time.getDeltaTime());
  157.  
  158.         for (glm::vec3 position : positions)
  159.         {
  160.             block.setPosition(position);
  161.  
  162.             block.draw(shaderProgram);
  163.         }
  164.  
  165.         window.update();
  166.     }
  167.  
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement