wojtek #1

Merged
s444341 merged 3 commits from wojtek into master 2021-02-01 10:03:12 +01:00
Showing only changes of commit 7c02a8eae5 - Show all commits

View File

@ -11,14 +11,28 @@
#include "Camera.h" #include "Camera.h"
#include "Texture.h" #include "Texture.h"
#include "Physics.h" #include "Physics.h"
#include "Skybox.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
Core::Shader_Loader shaderLoader; Core::Shader_Loader shaderLoader;
GLuint programColor; GLuint programColor;
GLuint programTexture; GLuint programTexture;
GLuint programSkybox;
GLuint cubemapTexture;
GLuint skyboxVAO, skyboxVBO;
obj::Model planeModel, spaceshipModel, boosterModel; obj::Model planeModel;
Core::RenderContext planeContext, spaceshipContext, boosterContext; obj::Model boxModel;
GLuint boxTexture, groundTexture; 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 cameraPos = glm::vec3(0, 5, 40);
glm::vec3 cameraDir; glm::vec3 cameraDir;
@ -27,6 +41,18 @@ float cameraAngle = 0;
glm::mat4 cameraMatrix, perspectiveMatrix; glm::mat4 cameraMatrix, perspectiveMatrix;
glm::vec3 lightDir = glm::normalize(glm::vec3(0.5, -1, -0.5)); glm::vec3 lightDir = glm::normalize(glm::vec3(0.5, -1, -0.5));
glm::vec3 lightPos = glm::vec3(-15.75, 2, 0);
// skybox
std::vector<std::string> 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) // Initalization of physical scene (PhysX)
@ -59,16 +85,27 @@ void initRenderables()
{ {
// load models // load models
planeModel = obj::loadModelFromFile("models/plane.obj"); 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"); 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); planeContext.initFromOBJ(planeModel);
spaceshipContext.initFromOBJ(spaceshipModel); boxContext.initFromOBJ(boxModel);
shipContext.initFromOBJ(shipModel);
boosterContext.initFromOBJ(boosterModel); boosterContext.initFromOBJ(boosterModel);
platformContext.initFromOBJ(platformModel);
craneContext.initFromOBJ(craneModel);
lampContext.initFromOBJ(lampModel);
// load textures // load textures
groundTexture = Core::LoadTexture("textures/sand.jpg"); groundTexture = Core::LoadTexture("textures/sand.jpg");
boxTexture = Core::LoadTexture("textures/a.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 // This time we organize all the renderables in a list
// of basic properties (model, transform, texture), // of basic properties (model, transform, texture),
@ -83,12 +120,12 @@ void initRenderables()
Renderable* booster = new Renderable(); Renderable* booster = new Renderable();
booster->context = &boosterContext; booster->context = &boosterContext;
booster->textureId = boxTexture; booster->textureId = shipTexture;
renderables.emplace_back(booster); renderables.emplace_back(booster);
Renderable* spaceship = new Renderable(); Renderable* spaceship = new Renderable();
spaceship->context = &spaceshipContext; spaceship->context = &shipContext;
spaceship->textureId = boxTexture; spaceship->textureId = shipTexture;
renderables.emplace_back(spaceship); 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, "objectColor"), color.x, color.y, color.z);
glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.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; glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "modelViewProjectionMatrix"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(program, "modelViewProjectionMatrix"), 1, GL_FALSE, (float*)&transformation);
@ -217,6 +255,7 @@ void drawObjectTexture(Core::RenderContext * context, glm::mat4 modelMatrix, GLu
glUseProgram(program); glUseProgram(program);
glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z); 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); Core::SetActiveTexture(textureId, "textureSampler", program, 0);
glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix; glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
@ -247,11 +286,13 @@ void renderScene()
// Update of camera and perspective matrices // Update of camera and perspective matrices
cameraMatrix = createCameraMatrix(); cameraMatrix = createCameraMatrix();
perspectiveMatrix = Core::createPerspectiveMatrix(); perspectiveMatrix = Core::createPerspectiveMatrix(0.1f, 1000.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.1f, 0.3f, 1.0f); glClearColor(0.0f, 0.1f, 0.3f, 1.0f);
Skybox::drawSkybox(programSkybox, cameraMatrix, perspectiveMatrix, cubemapTexture);
// update transforms from physics simulation // update transforms from physics simulation
updateTransforms(); updateTransforms();
@ -282,6 +323,9 @@ void init()
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
programColor = shaderLoader.CreateProgram("shaders/shader_color.vert", "shaders/shader_color.frag"); programColor = shaderLoader.CreateProgram("shaders/shader_color.vert", "shaders/shader_color.frag");
programTexture = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.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(); initRenderables();
initPhysicsScene(); initPhysicsScene();
@ -292,6 +336,14 @@ void shutdown()
{ {
shaderLoader.DeleteProgram(programColor); shaderLoader.DeleteProgram(programColor);
shaderLoader.DeleteProgram(programTexture); 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() void idle()
@ -305,7 +357,7 @@ int main(int argc, char ** argv)
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200, 200); glutInitWindowPosition(200, 200);
glutInitWindowSize(600, 600); glutInitWindowSize(600, 600);
glutCreateWindow("OpenGL + PhysX"); glutCreateWindow("Projekt GRK");
glewInit(); glewInit();
init(); init();