Reflection
[opengl.git] / skybox.cpp
index bd097cb473dc85f92189a354325de78f6b4c4ba5..79ce9d022029cf121fc51c02f1cf05e67026382a 100644 (file)
@@ -1,13 +1,12 @@
 #include "shapes.hpp"
 #include "skybox.hpp"
 #include "image.hpp"
-#include <GL/glew.h>
 #include <glm/gtc/type_ptr.hpp>
        
 Skybox::Skybox(const std::vector<std::string> faces): program("skyboxvert.glsl", "skyboxfrag.glsl") {
-       glUseProgram(program.progId);
        glGenTextures(1, &texId);
        glBindTexture(GL_TEXTURE_CUBE_MAP, texId);
+       glDepthFunc(GL_LEQUAL);
 
        int width, height, numChans;
        for (int i = 0; i < faces.size(); i++) {
@@ -30,6 +29,9 @@ Skybox::Skybox(const std::vector<std::string> faces): program("skyboxvert.glsl",
 
        auto vertices = cube();
 
+       //reverse so facing inside out
+       std::reverse(vertices.begin(), vertices.end());
+
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
        
@@ -37,26 +39,11 @@ Skybox::Skybox(const std::vector<std::string> faces): program("skyboxvert.glsl",
        glEnableVertexAttribArray(posLoc);
        glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
 }
-void validatePrograms(GLuint progId) {
-       glValidateProgram(progId);
-       
-       GLint success;
-       glGetProgramiv(progId, GL_VALIDATE_STATUS, &success);
-       if (!success) {
-               GLchar log[1024];
-               glGetProgramInfoLog(progId, sizeof(log), NULL, log);
-               fprintf(stderr, "error: %s\n", log);
-               exit(1);
-       }
-}
 
 void Skybox::draw(glm::mat4 proj, glm::mat4 view) const {
        glUseProgram(program.progId);
 
-       glDepthMask(GL_FALSE);
-       glDisable(GL_CULL_FACE);
        glBindVertexArray(vao);
-       validatePrograms(program.progId);
 
        GLuint projLoc = glGetUniformLocation(program.progId, "projection");
        glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
@@ -65,8 +52,11 @@ void Skybox::draw(glm::mat4 proj, glm::mat4 view) const {
        view = glm::mat4(glm::mat3(view));
        glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
 
+       glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_CUBE_MAP, texId);
        glDrawArrays(GL_TRIANGLES, 0, 36);
-       glEnable(GL_CULL_FACE);
-       glDepthMask(GL_TRUE);
+}
+
+GLuint Skybox::getTexture() const {
+       return texId;
 }