diff options
author | jjesswan <jessica_wan@brown.edu> | 2024-05-07 06:02:29 -0400 |
---|---|---|
committer | jjesswan <jessica_wan@brown.edu> | 2024-05-07 06:02:29 -0400 |
commit | 6a45579dbbf991c0e12ce59958e3b533d19fc9d4 (patch) | |
tree | 83ad11d63c0a1d1c89b9469455ecbda4531fe6ac /src | |
parent | cb868acadcae4e3f497ecdabeca507f3e73e01c7 (diff) |
rotating skyboxgit add -A
Diffstat (limited to 'src')
-rwxr-xr-x | src/glwidget.cpp | 82 | ||||
-rwxr-xr-x | src/glwidget.h | 12 | ||||
-rw-r--r-- | src/graphics/shader.h | 1 | ||||
-rw-r--r-- | src/skybox.cpp | 167 | ||||
-rw-r--r-- | src/skybox.h | 220 |
5 files changed, 478 insertions, 4 deletions
diff --git a/src/glwidget.cpp b/src/glwidget.cpp index c3c525b..f9bedc9 100755 --- a/src/glwidget.cpp +++ b/src/glwidget.cpp @@ -50,12 +50,20 @@ GLWidget::GLWidget(QWidget *parent) : // Function tick() will be called once per interva connect(&m_intervalTimer, SIGNAL(timeout()), this, SLOT(tick())); + + //m_skybox = new skybox(); } GLWidget::~GLWidget() { if (m_defaultShader != nullptr) delete m_defaultShader; if (m_pointShader != nullptr) delete m_pointShader; + if (m_foamShader != nullptr) delete m_foamShader; + + if (m_skyboxShader != nullptr) delete m_skyboxShader; + //if (m_skybox != nullptr) delete m_skybox; + + } // ================== Basic OpenGL Overrides @@ -70,8 +78,12 @@ void GLWidget::initializeGL() // Set clear color to white glClearColor(0, 0, 0, 1); +// glEnable(GL_DEPTH_TEST); +// glEnable(GL_CULL_FACE); +// glEnable(GL_BLEND); +// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - // Enable depth-testing and backface culling +// // Enable depth-testing and backface culling glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); @@ -84,6 +96,15 @@ void GLWidget::initializeGL() // m_texture_shader = new Shader(":/resources/shaders/texture.vert", ":/resources/shaders/texture.frag"); m_colorShader = new Shader(":resources/shaders/color.vert", ":resources/shaders/color.frag"); m_foamShader = new Shader(":resources/shaders/foam.vert", ":resources/shaders/foam.frag"); + m_skyboxShader = new Shader(":resources/shaders/skybox.vert", ":resources/shaders/skybox.frag"); + + // specify texture for skybox +// m_skyboxShader->bind(); +// glUniform1i(glGetUniformLocation(m_skyboxShader->id(), "cubeMap"), 9); // bind texture at slot 9 +// Eigen::Vector3f sc = Eigen::Vector3f(.77f, .85f, .99f); // skycolor for fade effect +// glUniform3f(glGetUniformLocation(m_skyboxShader->id(), "skyColor"), sc[0], sc[1], sc[2]); +// m_skyboxShader->unbind(); + m_halftone_tex = loadTextureFromFile("/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/halftone.png").textureID; m_foam_tex = loadTextureFromFile("/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/foam3.png").textureID; @@ -91,6 +112,10 @@ void GLWidget::initializeGL() initCaustics(); + + // init skybox stuff + m_skybox.initializeVAO(); + // INITIALIZE TEXTURE STUFF // Prepare filepath @@ -171,6 +196,9 @@ void GLWidget::initializeGL() m_camera.lookAt(eye, target); m_camera.setOrbitPoint(target); m_camera.setPerspective(120, width() / static_cast<float>(height()), nearPlane, farPlane); + m_camera.setPosition(Eigen::Vector3f( 0, + 0, + -70289.5)); m_deltaTimeProvider.start(); m_intervalTimer.start(1000 / 60); @@ -371,6 +399,13 @@ void GLWidget::paintGL() m_arap.drawFoam(m_foamShader, GL_TRIANGLES); m_foamShader->unbind(); + // skybox + + + + m_skybox.draw(m_skyboxShader, m_camera); + + } @@ -420,6 +455,48 @@ TextureData GLWidget::loadTextureFromFile(const char *path) return newtex; } +GLuint GLWidget::loadCubeMap(std::vector<const char*> textureFiles){ + std::cout << "hello 111" << std::endl; + + // create empty texture + GLuint textureID; + glGenTextures(1, &textureID); + + std::cout << "hello fssd" << std::endl; + + + //glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); + + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + + std::cout << "hello fssd" << std::endl; + + GLuint target = GL_TEXTURE_CUBE_MAP_POSITIVE_X; + for (int i=0; i<6; i++){ + std::string filename = std::string(textureFiles[i]);//directory + '/' + filename; + int width, height, nrChannels; + unsigned char* data = stbi_load(filename.c_str(), &width, &height, &nrChannels, 0); + + if (data){ + stbi_set_flip_vertically_on_load(false); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + stbi_image_free(data); + + } else { + std::cout << "Texture failed to load at path: " << textureFiles[i] << std::endl; + stbi_image_free(data); + } + } + + return textureID; +} + + void GLWidget::resizeGL(int w, int h) { @@ -581,6 +658,8 @@ void GLWidget::tick() { float deltaSeconds = m_deltaTimeProvider.restart() / 1000.f; m_arap.update(deltaSeconds); + // rotate skybox + m_skybox.update(deltaSeconds); // Move camera auto look = m_camera.getLook(); @@ -592,6 +671,7 @@ void GLWidget::tick() moveVec *= deltaSeconds; m_camera.move(moveVec); + // Flag this view for repainting (Qt will call paintGL() soon after) update(); } diff --git a/src/glwidget.h b/src/glwidget.h index 42cfd83..006f027 100755 --- a/src/glwidget.h +++ b/src/glwidget.h @@ -1,8 +1,8 @@ #pragma once -#ifdef __APPLE__ -#define GL_SILENCE_DEPRECATION -#endif +#include "skybox.h" +#include <GL/glew.h> + #include "arap.h" #include "graphics/camera.h" @@ -52,6 +52,8 @@ private: void paintCaustics(); TextureData loadTextureFromFile(const char *path); + GLuint loadCubeMap(std::vector<const char*> textureFiles); + private slots: // Physics Tick @@ -66,6 +68,8 @@ private: Shader *m_colorShader; Shader *m_foamShader; + Shader *m_skyboxShader; + GLuint m_fullscreen_vbo; @@ -110,4 +114,6 @@ private: bool m_rightCapture; SelectMode m_rightClickSelectMode; int m_lastSelectedVertex = -1; + + skybox m_skybox; }; diff --git a/src/graphics/shader.h b/src/graphics/shader.h index 7e9dad4..5f7d7c6 100644 --- a/src/graphics/shader.h +++ b/src/graphics/shader.h @@ -10,6 +10,7 @@ #include <util/unsupportedeigenthing/OpenGLSupport> + class Shader { public: Shader(const std::string &vertexPath, const std::string &fragmentPath); diff --git a/src/skybox.cpp b/src/skybox.cpp new file mode 100644 index 0000000..3f6abd7 --- /dev/null +++ b/src/skybox.cpp @@ -0,0 +1,167 @@ +#include "skybox.h" + +#include "stb/stb_image.h" +#include <iostream> +#include <Eigen/Core> +#include <Eigen/Dense> +#include <Eigen/Sparse> +#include <Eigen/Geometry> + + + + + +skybox::skybox() +{ + //initializeVAO(); +} + +void skybox::initializeVAO(){ + sky_shape.init(m_vertices_eigen, m_faces); + std::cout << "hehee" << std::endl; + skybox_tex = loadCubeMap(m_skyboxTextureFiles); + + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + glBindVertexArray(VAO); + + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, m_vertices.size()*sizeof(float), m_vertices.data(), GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); + + // unbind + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + + +void skybox::draw(Shader *skybox_shader, Camera m_camera){ + + + + + glDepthFunc(GL_LEQUAL); + glDisable(GL_CULL_FACE); + + skybox_shader->bind(); + Eigen::Vector3f sc = Eigen::Vector3f(.77f, .85f, .99f); // skycolor for fade effect + glUniform3f(glGetUniformLocation(skybox_shader->id(), "skyColor"), sc[0], sc[1], sc[2]); + + + // activate texture + glActiveTexture(GL_TEXTURE9); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_tex); + glUniform1i(glGetUniformLocation(skybox_shader->id(), "cubeMap"), 9); // bind texture at slot 9 + + + // manually set view and projection, for non-translating view + Eigen::Matrix4f projection = m_camera.getProjection(); + Eigen::Matrix4f view = m_camera.getView(); + view.col(3) = Eigen::Vector4f(0, 0, 0, 0); + + glUniformMatrix4fv(glGetUniformLocation(skybox_shader->id(), "view"), 1, GL_FALSE, view.data()); + glUniformMatrix4fv(glGetUniformLocation(skybox_shader->id(), "projection"), 1, GL_FALSE, projection.data()); + +// skybox_shader->setUniform("projection", projection); +// skybox_shader->setUniform("view", view); + + // apply rotation matrix + skybox_shader->setUniform("rotation", m_rotation_mat); + + //glUniformMatrix4fv(glGetUniformLocation(skybox_shader., "rotation"), 1, GL_FALSE, glm::value_ptr(m_rotation_mat->getRotation()[0])); + + //Global::graphics.setGlobalData(glm::vec3(.5f)); + glBindVertexArray(VAO); + glDrawArrays(GL_TRIANGLES, 0, m_vertices.size()); + + +// glDrawElements(GL_TRIANGLES, m_vertices.size(), GL_UNSIGNED_INT, reinterpret_cast<GLvoid *>(0)); +// glBindVertexArray(0); +//// glBindTexture(GL_TEXTURE_2D, 0); +/// +/// +/// +/// + + std::cout << m_camera.getPosition() << std::endl; + + // sky_shape.draw(skybox_shader, GL_TRIANGLES); + + glDepthFunc(GL_LESS); + + + skybox_shader->unbind(); + +} + +void skybox::update(double deltaTime){ + m_rotation += ROTATE_SPEED * deltaTime; + +// Eigen::Rotation2Df f = Eigen::Rotation2Df(m_rotation); + + +// m_rotation_mat = f.toRotationMatrix(); + + + auto sinA = std::sin(m_rotation / 2); + auto cosA = std::cos(m_rotation/ 2); + + Eigen::Quaternionf q; + q.x() = 0 * sinA; + q.y() = 1 * sinA; + q.z() = 0 * sinA; + q.w() = cosA; + + Eigen::Matrix3f mat3 = q.toRotationMatrix(); + Eigen::Matrix4f mat4 = Eigen::Matrix4f::Identity(); + mat4.block(0,0,3,3) = mat3; + + + m_rotation_mat = mat4; + + + + + + +} + +GLuint skybox::loadCubeMap(std::vector<const char*> textureFiles){ + // create empty texture + GLuint textureID; + glGenTextures(1, &textureID); + + std::cout << "hello fssd" << std::endl; + + + //glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); + + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + + std::cout << "hello fssd" << std::endl; + + GLuint target = GL_TEXTURE_CUBE_MAP_POSITIVE_X; + for (int i=0; i<6; i++){ + std::string filename = std::string(textureFiles[i]);//directory + '/' + filename; + int width, height, nrChannels; + unsigned char* data = stbi_load(filename.c_str(), &width, &height, &nrChannels, 0); + + if (data){ + stbi_set_flip_vertically_on_load(false); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + stbi_image_free(data); + + } else { + std::cout << "Texture failed to load at path: " << textureFiles[i] << std::endl; + stbi_image_free(data); + } + } + + return textureID; +} diff --git a/src/skybox.h b/src/skybox.h new file mode 100644 index 0000000..94d1354 --- /dev/null +++ b/src/skybox.h @@ -0,0 +1,220 @@ +#ifndef SKYBOX_H +#define SKYBOX_H +#include "graphics/shape.h" +#define EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT +#define EIGEN_DONT_VECTORIZE +#include "graphics/camera.h" +#include "graphics/shader.h" +#include <GL/glew.h> + +//#ifdef __APPLE__ +//#define GL_SILENCE_DEPRECATION +//#endif + +#include <QOpenGLWidget> +#include <QElapsedTimer> +#include <QTimer> +#include <memory> + + +class skybox +{ +public: + skybox(); + void draw(Shader *skybox_shader, Camera m_camera); + void update(double deltaTime); + + GLuint getVAO(){ + return VAO; + } + + GLuint getSkyboxTex(){ + return skybox_tex; + } + + std::vector<GLfloat> getVertices(){ + return m_vertices; + } + + Eigen::Matrix4f getRotMat(){ + return m_rotation_mat; + } + + std::vector<const char*> getTextureFiles(){ + return m_skyboxTextureFiles; + } + + void initializeVAO(); + + + private: + GLuint loadCubeMap(std::vector<const char*> textureFiles); + + + GLuint VAO; + GLuint VBO; + GLuint EBO; + GLuint skybox_tex; + + float SIZE = 100000000.f; //100000000.f; + + std::vector<int> m_vertex_indices = { + // Right + 1, 2, 6, + 6, 5, 1, + // Left + 0, 4, 7, + 7, 3, 0, + // Top + 4, 5, 6, + 6, 7, 4, + // Bottom + 0, 3, 2, + 2, 1, 0, + // Back + 0, 1, 5, + 5, 4, 0, + // Front + 3, 7, 6, + 6, 2, 3 + }; + + std::vector<Eigen::Vector3i> m_faces = { + // Right + Eigen::Vector3i( 1, 2, 6), + Eigen::Vector3i( 6, 5, 1), + // Left + Eigen::Vector3i( 0, 4, 7), + Eigen::Vector3i( 7, 3, 0), + // Top + Eigen::Vector3i( 4, 5, 6), + Eigen::Vector3i( 6, 7, 4), + // Bottom + Eigen::Vector3i( 0, 3, 2), + Eigen::Vector3i( 2, 1, 0), + // Back + Eigen::Vector3i( 0, 1, 5), + Eigen::Vector3i( 5, 4, 0), + // Front + Eigen::Vector3i( 3, 7, 6), + Eigen::Vector3i( 6, 2, 3) + }; + std::vector<GLfloat> m_vertices = { + -SIZE, SIZE, -SIZE, + -SIZE, -SIZE, -SIZE, + SIZE, -SIZE, -SIZE, + SIZE, -SIZE, -SIZE, + SIZE, SIZE, -SIZE, + -SIZE, SIZE, -SIZE, + + -SIZE, -SIZE, SIZE, + -SIZE, -SIZE, -SIZE, + -SIZE, SIZE, -SIZE, + -SIZE, SIZE, -SIZE, + -SIZE, SIZE, SIZE, + -SIZE, -SIZE, SIZE, + + SIZE, -SIZE, -SIZE, + SIZE, -SIZE, SIZE, + SIZE, SIZE, SIZE, + SIZE, SIZE, SIZE, + SIZE, SIZE, -SIZE, + SIZE, -SIZE, -SIZE, + + -SIZE, -SIZE, SIZE, + -SIZE, SIZE, SIZE, + SIZE, SIZE, SIZE, + SIZE, SIZE, SIZE, + SIZE, -SIZE, SIZE, + -SIZE, -SIZE, SIZE, + + -SIZE, SIZE, -SIZE, + SIZE, SIZE, -SIZE, + SIZE, SIZE, SIZE, + SIZE, SIZE, SIZE, + -SIZE, SIZE, SIZE, + -SIZE, SIZE, -SIZE, + + -SIZE, -SIZE, -SIZE, + -SIZE, -SIZE, SIZE, + SIZE, -SIZE, -SIZE, + SIZE, -SIZE, -SIZE, + -SIZE, -SIZE, SIZE, + SIZE, -SIZE, SIZE + }; + + std::vector<Eigen::Vector3f> m_vertices_eigen = { + Eigen::Vector3f( -SIZE, SIZE, -SIZE), + Eigen::Vector3f( -SIZE, -SIZE, -SIZE), + Eigen::Vector3f( SIZE, -SIZE, -SIZE), + Eigen::Vector3f( SIZE, -SIZE, -SIZE), + Eigen::Vector3f( SIZE, SIZE, -SIZE), + Eigen::Vector3f( -SIZE, SIZE, -SIZE), + + Eigen::Vector3f( -SIZE, -SIZE, SIZE), + Eigen::Vector3f( -SIZE, -SIZE, -SIZE), + Eigen::Vector3f( -SIZE, SIZE, -SIZE), + Eigen::Vector3f( -SIZE, SIZE, -SIZE), + Eigen::Vector3f( -SIZE, SIZE, SIZE), + Eigen::Vector3f( -SIZE, -SIZE, SIZE), + + Eigen::Vector3f( SIZE, -SIZE, -SIZE), + Eigen::Vector3f( SIZE, -SIZE, SIZE), + Eigen::Vector3f( SIZE, SIZE, SIZE), + Eigen::Vector3f( SIZE, SIZE, SIZE), + Eigen::Vector3f( SIZE, SIZE, -SIZE), + Eigen::Vector3f( SIZE, -SIZE, -SIZE), + + Eigen::Vector3f( -SIZE, -SIZE, SIZE), + Eigen::Vector3f( -SIZE, SIZE, SIZE), + Eigen::Vector3f( SIZE, SIZE, SIZE), + Eigen::Vector3f( SIZE, SIZE, SIZE), + Eigen::Vector3f( SIZE, -SIZE, SIZE), + Eigen::Vector3f( -SIZE, -SIZE, SIZE), + + Eigen::Vector3f( -SIZE, SIZE, -SIZE), + Eigen::Vector3f( SIZE, SIZE, -SIZE), + Eigen::Vector3f( SIZE, SIZE, SIZE), + Eigen::Vector3f( SIZE, SIZE, SIZE), + Eigen::Vector3f( -SIZE, SIZE, SIZE), + Eigen::Vector3f( -SIZE, SIZE, -SIZE), + + Eigen::Vector3f( -SIZE, -SIZE, -SIZE), + Eigen::Vector3f( -SIZE, -SIZE, SIZE), + Eigen::Vector3f( SIZE, -SIZE, -SIZE), + Eigen::Vector3f( SIZE, -SIZE, -SIZE), + Eigen::Vector3f( -SIZE, -SIZE, SIZE), + Eigen::Vector3f( SIZE, -SIZE, SIZE) + }; + + std::vector<const char*> m_skyboxTextureFiles = + +// { +// "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/foam3.png", +// "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/foam3.png", +// "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/foam3.png", +// "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/foam3.png", +// "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/foam3.png", +// "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/foam3.png" + +// }; + + + + + {"/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/cupertin-lake_rt.png", + "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/cupertin-lake_lf.png", + "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/cupertin-lake_up.png", + "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/cupertin-lake_dn.png", + "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/cupertin-lake_bk.png", + "/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/cupertin-lake_ft.png", + }; + + float ROTATE_SPEED = .01f; // 1 degree per sec + float m_rotation = 0.f; + Eigen::Matrix4f m_rotation_mat = Eigen::Matrix4f::Identity(); + + Shape sky_shape; +}; + +#endif // SKYBOX_H |