Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 KB | None | 0 0
  1. int main(void)
  2. {
  3.     glEnable(GL_DEPTH_TEST);
  4.     glEnable(GL_TEXTURE_CUBE_MAP);
  5.  
  6.     Shader cubeShader = Shader("C:\\Users\\Szydlo\\source\\repos\\Freecraft\\Freecraft\\src\\Shaders\\cube.vs", "C:\\Users\\Szydlo\\source\\repos\\Freecraft\\Freecraft\\src\\Shaders\\cube.fs");
  7.  
  8.     //CUBE
  9.     float cubeVertices[] = {
  10.         -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  11.          0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  12.          0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  13.          0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  14.         -0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  15.         -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  16.  
  17.         -0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  18.          0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  19.          0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  20.          0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  21.         -0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  22.         -0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  23.  
  24.         -0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
  25.         -0.5f,  0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
  26.         -0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
  27.         -0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
  28.         -0.5f, -0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
  29.         -0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
  30.  
  31.          0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
  32.          0.5f,  0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
  33.          0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
  34.          0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
  35.          0.5f, -0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
  36.          0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
  37.  
  38.         -0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
  39.          0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
  40.          0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
  41.          0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
  42.         -0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
  43.         -0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
  44.  
  45.         -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
  46.          0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
  47.          0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
  48.          0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
  49.         -0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
  50.         -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f
  51.     };
  52.  
  53.     unsigned int cubeVAO, cubeVBO;
  54.     glGenVertexArrays(1, &cubeVAO);
  55.     glGenBuffers(1, &cubeVBO);
  56.     glBindVertexArray(cubeVAO);
  57.     glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
  58.     glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW);
  59.     glEnableVertexAttribArray(0);
  60.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
  61.     glEnableVertexAttribArray(1);
  62.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
  63.  
  64.     unsigned int textureID;
  65.     glGenTextures(1, &textureID);
  66.     glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
  67.  
  68.     int width, height, nrComponents;
  69.     unsigned char* data = stbi_load("C:\\Users\\Szydlo\\source\\repos\\Freecraft\\Freecraft\\src\\Res\\test.png", &width, &height, &nrComponents, 0);
  70.     glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  71.     glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  72.     glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  73.     glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  74.     glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  75.     glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  76.  
  77.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  78.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  79.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  80.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  81.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  82.  
  83.     cubeShader.use();
  84.     cubeShader.setInt("cubeTexture", 0);
  85.  
  86.     while (!glfwWindowShouldClose(window))
  87.     {
  88.  
  89.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  90.         glClearColor(0.0f, 0.7f, 1.0f, 1.0f);
  91.  
  92.         //cube draw
  93.         cubeShader.use();
  94.         glm::mat4 model = glm::mat4(1.0f);
  95.         glm::mat4 view = camera.GetViewMatrix();
  96.         glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)800 / (float)600, 0.1f, 100.0f);
  97.         cubeShader.setMat4("model", model);
  98.         cubeShader.setMat4("view", view);
  99.         cubeShader.setMat4("projection", projection);
  100.  
  101.         glBindVertexArray(cubeVAO);
  102.         glActiveTexture(GL_TEXTURE0);
  103.         glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
  104.         glDrawArrays(GL_TRIANGLES, 0, 36);
  105.         glBindVertexArray(0);
  106.  
  107.         glfwSwapBuffers(window);
  108.         glfwPollEvents();
  109.     }
  110.     glDeleteVertexArrays(1, &cubeVAO);
  111.     glDeleteBuffers(1, &cubeVBO);
  112.  
  113.     glfwTerminate();
  114.     return 0;
  115. };
  116.  
  117. --fragment shader
  118.  
  119. #version 400
  120.  
  121. in vec3 texcoords;
  122.  
  123. uniform samplerCube cubeTexture;
  124.  
  125. out vec4 FragColour;
  126.  
  127. void main () {
  128.    vec4 cubeMapColour = texture (cubeTexture, texcoords);
  129.    FragColour = cubeMapColour;
  130. }
  131.  
  132. --vertex shader
  133.  
  134. #version 330 core
  135. layout (location = 0) in vec3 aPos;
  136. layout (location = 1) in vec3 aNormal;
  137.  
  138. out vec3 Normal;
  139. out vec3 Position;
  140.  
  141. uniform mat4 model;
  142. uniform mat4 view;
  143. uniform mat4 projection;
  144.  
  145. void main()
  146. {
  147.     Normal = mat3(transpose(inverse(model))) * aNormal;
  148.     Position = vec3(model * vec4(aPos, 1.0));
  149.     gl_Position = projection * view * model * vec4(aPos, 1.0);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement