From 7c02a8eae5fbc21164b28c6816fb95e58f043799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20=C5=81ukasik?= Date: Sun, 31 Jan 2021 19:33:04 +0100 Subject: [PATCH] start skybox --- src/start.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/src/start.cpp b/src/start.cpp index d05261d..c344aff 100644 --- a/src/start.cpp +++ b/src/start.cpp @@ -11,14 +11,28 @@ #include "Camera.h" #include "Texture.h" #include "Physics.h" +#include "Skybox.h" + +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" Core::Shader_Loader shaderLoader; GLuint programColor; GLuint programTexture; +GLuint programSkybox; +GLuint cubemapTexture; +GLuint skyboxVAO, skyboxVBO; -obj::Model planeModel, spaceshipModel, boosterModel; -Core::RenderContext planeContext, spaceshipContext, boosterContext; -GLuint boxTexture, groundTexture; +obj::Model planeModel; +obj::Model boxModel; +obj::Model shipModel; +obj::Model boosterModel; +obj::Model platformModel; +obj::Model craneModel; +obj::Model lampModel; +glm::mat4 boxModelMatrix; +Core::RenderContext planeContext, boxContext, shipContext, boosterContext, platformContext, craneContext, lampContext; +GLuint boxTexture, groundTexture, shipTexture, stoneTexture, redTex; glm::vec3 cameraPos = glm::vec3(0, 5, 40); glm::vec3 cameraDir; @@ -27,6 +41,18 @@ float cameraAngle = 0; glm::mat4 cameraMatrix, perspectiveMatrix; glm::vec3 lightDir = glm::normalize(glm::vec3(0.5, -1, -0.5)); +glm::vec3 lightPos = glm::vec3(-15.75, 2, 0); + +// skybox +std::vector faces +{ + "textures/skybox/stars.jpeg", + "textures/skybox/stars.jpeg", + "textures/skybox/stars.jpeg", + "textures/skybox/stars.jpeg", + "textures/skybox/stars.jpeg", + "textures/skybox/stars.jpeg", +}; // Initalization of physical scene (PhysX) @@ -59,16 +85,27 @@ void initRenderables() { // load models planeModel = obj::loadModelFromFile("models/plane.obj"); - spaceshipModel = obj::loadModelFromFile("models/ship.obj"); + boxModel = obj::loadModelFromFile("models/box.obj"); + shipModel = obj::loadModelFromFile("models/ship.obj"); boosterModel = obj::loadModelFromFile("models/booster.obj"); + platformModel = obj::loadModelFromFile("models/platform4.obj"); + craneModel = obj::loadModelFromFile("models/crane.obj"); + lampModel = obj::loadModelFromFile("models/lamp2.obj"); planeContext.initFromOBJ(planeModel); - spaceshipContext.initFromOBJ(spaceshipModel); + boxContext.initFromOBJ(boxModel); + shipContext.initFromOBJ(shipModel); boosterContext.initFromOBJ(boosterModel); + platformContext.initFromOBJ(platformModel); + craneContext.initFromOBJ(craneModel); + lampContext.initFromOBJ(lampModel); // load textures groundTexture = Core::LoadTexture("textures/sand.jpg"); boxTexture = Core::LoadTexture("textures/a.jpg"); + shipTexture = Core::LoadTexture("textures/ship.png"); + stoneTexture = Core::LoadTexture("textures/stone.png"); + redTex = Core::LoadTexture("textures/redTex.png"); // This time we organize all the renderables in a list // of basic properties (model, transform, texture), @@ -83,12 +120,12 @@ void initRenderables() Renderable* booster = new Renderable(); booster->context = &boosterContext; - booster->textureId = boxTexture; + booster->textureId = shipTexture; renderables.emplace_back(booster); Renderable* spaceship = new Renderable(); - spaceship->context = &spaceshipContext; - spaceship->textureId = boxTexture; + spaceship->context = &shipContext; + spaceship->textureId = shipTexture; renderables.emplace_back(spaceship); } @@ -200,6 +237,7 @@ void drawObjectColor(Core::RenderContext* context, glm::mat4 modelMatrix, glm::v glUniform3f(glGetUniformLocation(program, "objectColor"), color.x, color.y, color.z); glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z); + glUniform3f(glGetUniformLocation(program, "lightPos"), lightPos.x, lightPos.y, lightPos.z); glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(program, "modelViewProjectionMatrix"), 1, GL_FALSE, (float*)&transformation); @@ -217,6 +255,7 @@ void drawObjectTexture(Core::RenderContext * context, glm::mat4 modelMatrix, GLu glUseProgram(program); glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z); + glUniform3f(glGetUniformLocation(program, "lightPos"), lightPos.x, lightPos.y, lightPos.z); Core::SetActiveTexture(textureId, "textureSampler", program, 0); glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix; @@ -247,11 +286,13 @@ void renderScene() // Update of camera and perspective matrices cameraMatrix = createCameraMatrix(); - perspectiveMatrix = Core::createPerspectiveMatrix(); + perspectiveMatrix = Core::createPerspectiveMatrix(0.1f, 1000.f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.0f, 0.1f, 0.3f, 1.0f); + Skybox::drawSkybox(programSkybox, cameraMatrix, perspectiveMatrix, cubemapTexture); + // update transforms from physics simulation updateTransforms(); @@ -282,6 +323,9 @@ void init() glEnable(GL_DEPTH_TEST); programColor = shaderLoader.CreateProgram("shaders/shader_color.vert", "shaders/shader_color.frag"); programTexture = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag"); + programSkybox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); + + cubemapTexture = Skybox::loadCubemap(faces); initRenderables(); initPhysicsScene(); @@ -292,6 +336,14 @@ void shutdown() { shaderLoader.DeleteProgram(programColor); shaderLoader.DeleteProgram(programTexture); + + planeContext.initFromOBJ(planeModel); + boxContext.initFromOBJ(boxModel); + shipContext.initFromOBJ(shipModel); + boosterContext.initFromOBJ(boosterModel); + platformContext.initFromOBJ(platformModel); + craneContext.initFromOBJ(craneModel); + lampContext.initFromOBJ(lampModel); } void idle() @@ -305,7 +357,7 @@ int main(int argc, char ** argv) glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(200, 200); glutInitWindowSize(600, 600); - glutCreateWindow("OpenGL + PhysX"); + glutCreateWindow("Projekt GRK"); glewInit(); init();