grafika_komputerowa/grk/project/src/ex_9_1.hpp

227 lines
6.7 KiB
C++
Raw Normal View History

2024-01-02 20:54:56 +01:00
#include "glew.h"
#include <GLFW/glfw3.h>
#include "glm.hpp"
#include "ext.hpp"
#include <iostream>
#include <cmath>
2024-01-04 20:51:01 +01:00
#include <list>
2024-01-12 23:26:00 +01:00
#include <vector>
2024-01-02 20:54:56 +01:00
#include "Shader_Loader.h"
#include "Render_Utils.h"
2024-01-12 23:26:00 +01:00
#include "texture.h"
2024-01-02 20:54:56 +01:00
#include "Box.cpp"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <string>
2024-01-03 20:40:52 +01:00
#include "../Sun.h"
2024-01-03 20:51:29 +01:00
#include "../Spaceship.h"
2024-01-08 16:17:25 +01:00
#include "../Planet.h"
#include "../GameObject.h"
2024-01-03 22:06:27 +01:00
#include "../GameUtils.h"
2024-01-02 20:54:56 +01:00
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
int WIDTH = 500, HEIGHT = 500;
namespace models {
Core::RenderContext marbleBustContext;
Core::RenderContext spaceshipContext;
Core::RenderContext sphereContext;
2024-01-12 23:26:00 +01:00
Core::RenderContext cubeContext;
}
namespace texture {
GLuint cubemapTexture;
2024-01-02 20:54:56 +01:00
}
2024-01-20 21:01:14 +01:00
void createGalaxy(glm::vec3 galaxyPosition);
2024-01-02 20:54:56 +01:00
GLuint depthMapFBO;
GLuint depthMap;
GLuint program;
GLuint programSun;
GLuint programTest;
GLuint programTex;
2024-01-12 23:26:00 +01:00
GLuint programCubemap;
2024-01-02 20:54:56 +01:00
2024-01-08 16:17:25 +01:00
std::list<Planet*> planets;
Sun* sun;
2024-01-02 20:54:56 +01:00
GLuint VAO,VBO;
float exposition = 1.f;
glm::vec3 pointlightPos = glm::vec3(0, 2, 0);
glm::vec3 pointlightColor = glm::vec3(0.9, 0.6, 0.6);
float lastTime = -1.f;
float deltaTime = 0.f;
2024-01-10 21:20:52 +01:00
Spaceship* spaceship = Spaceship::getInstance();
2024-01-20 21:01:14 +01:00
void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef);
2024-01-02 20:54:56 +01:00
void updateDeltaTime(float time) {
if (lastTime < 0) {
lastTime = time;
return;
}
deltaTime = time - lastTime;
if (deltaTime > 0.1) deltaTime = 0.1;
lastTime = time;
}
void renderShadowapSun() {
float time = glfwGetTime();
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
//uzupelnij o renderowanie glebokosci do tekstury
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, WIDTH, HEIGHT);
}
void renderScene(GLFWwindow* window)
{
glClearColor(0.4f, 0.4f, 0.8f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float time = glfwGetTime();
updateDeltaTime(time);
renderShadowapSun();
2024-01-12 23:26:00 +01:00
drawSkybox(models::cubeContext, glm::translate(glm::mat4(), spaceship->cameraPos), texture::cubemapTexture, programCubemap);
2024-01-02 20:54:56 +01:00
//space lamp
glUseProgram(programSun);
2024-01-10 20:56:06 +01:00
std::list<Sun*>* suns = GameUtils::getInstance()->getSuns();
for (Sun* sun : *suns) {
sun->draw();
2024-01-04 20:51:01 +01:00
}
2024-01-03 20:59:48 +01:00
2024-01-02 20:54:56 +01:00
glUseProgram(program);
2024-01-08 16:17:25 +01:00
for (Planet* p : planets) {
p->draw(time, program);
}
2024-01-16 22:15:26 +01:00
spaceship->renderBullets(glfwGetTime(), program);
2024-01-08 16:17:25 +01:00
//drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program);
2024-01-02 20:54:56 +01:00
drawObjectPBR(models::sphereContext,
2024-01-02 20:54:56 +01:00
glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(time) * glm::translate(glm::vec3(1.f, 0, 0)) * glm::scale(glm::vec3(0.1f)),
2024-01-08 16:17:25 +01:00
glm::vec3(0.5, 0.5, 0.5), 0.7, 0.0, program);
2024-01-02 20:54:56 +01:00
2024-01-10 21:12:45 +01:00
drawObjectPBR(models::spaceshipContext,
2024-01-10 21:20:52 +01:00
spaceship->calculateModelMatrix(),
spaceship->color,
spaceship->roughness, spaceship->metallic, program
2024-01-02 20:54:56 +01:00
);
glUseProgram(0);
glfwSwapBuffers(window);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
2024-01-10 20:56:06 +01:00
GameUtils::getInstance()->setAspectRatio(width / float(height));
2024-01-02 20:54:56 +01:00
glViewport(0, 0, width, height);
WIDTH = width;
HEIGHT = height;
}
2024-01-04 20:51:01 +01:00
void createSuns() {
2024-01-20 21:01:14 +01:00
createGalaxy(glm::vec3(0.f));
createGalaxy(glm::vec3(0.f, 50.f, 200.f));
}
void createGalaxy(glm::vec3 galaxyPosition) {
float planetsSizes[] = { 1.f, 1.5f, 0.8f, 1.2f, 0.2f };
createSolarSystem(galaxyPosition + glm::vec3(0, 2, 0), 3, planetsSizes, 5, 15.f, 0.2f);
float planetsSizes2[] = { 0.6f, 1.1f, 0.9f };
createSolarSystem(galaxyPosition + glm::vec3(150, 5, 0), 2, planetsSizes2, 3, 15.f, 0.2f);
float planetsSizes3[] = { 0.7f, 1.5f, 1.2f, 1.f };
createSolarSystem(galaxyPosition + glm::vec3(-20, -30, 50), 4, planetsSizes3, 4, 20.f, 0.2f);
float planetSizes4[] = { 1.f, 0.5f };
createSolarSystem(galaxyPosition + glm::vec3(100, 20, -50), 5, planetsSizes3, 2, 25.f, 0.2f);
}
void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef) {
2024-01-10 20:56:06 +01:00
GameUtils* gu = GameUtils::getInstance();
2024-01-20 21:01:14 +01:00
sun = new Sun(programSun, models::sphereContext, sunPos, glm::vec3(-0.93633f, 0.351106, 0.003226f), glm::vec3(0.9f, 0.9f, 0.7f) * 5, sunScale);
2024-01-10 20:56:06 +01:00
gu->getSuns()->push_back(sun);
2024-01-20 21:01:14 +01:00
for (int i = 0; i < numberOfPlanets; i++) {
float distanceFromSum = (i + 1) * planetsDistance;
Planet* planet = new Planet(sun, distanceFromSum, i * 0.1f + planetSpeedCoef, planetSizes[i], models::sphereContext);
planets.push_back(planet);
}
2024-01-04 20:51:01 +01:00
}
2024-01-20 21:01:14 +01:00
2024-01-02 20:54:56 +01:00
void init(GLFWwindow* window)
{
2024-01-16 22:15:26 +01:00
GameUtils* gameUtils = GameUtils::getInstance();
2024-01-02 20:54:56 +01:00
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glEnable(GL_DEPTH_TEST);
2024-01-16 22:15:26 +01:00
program = gameUtils->shaderLoader->CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag");
programTest = gameUtils->shaderLoader->CreateProgram("shaders/test.vert", "shaders/test.frag");
programSun = gameUtils->shaderLoader->CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag");
programCubemap = gameUtils->shaderLoader->CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
2024-01-04 21:38:08 +01:00
2024-01-02 20:54:56 +01:00
loadModelToContext("./models/marbleBust.obj", models::marbleBustContext);
loadModelToContext("./models/spaceship.obj", models::spaceshipContext);
2024-01-02 20:54:56 +01:00
loadModelToContext("./models/sphere.obj", models::sphereContext);
2024-01-12 23:26:00 +01:00
loadModelToContext("./models/cube.obj", models::cubeContext);
2024-01-16 22:15:26 +01:00
Core::loadModelToContext("./models/sphere.obj", GameUtils::getInstance()->sphereContext);
2024-01-12 23:26:00 +01:00
std::vector<std::string> cubeFaces = {
"bkg2_right1.png",
"bkg2_left2.png",
"bkg2_top3.png",
"bkg2_bottom4.png",
"bkg2_front5.png",
2024-01-20 21:01:14 +01:00
"bkg2_back6.png"
2024-01-12 23:26:00 +01:00
};
texture::cubemapTexture = Core::LoadCubemap(cubeFaces);
2024-01-04 20:51:01 +01:00
createSuns();
2024-01-02 20:54:56 +01:00
}
void shutdown(GLFWwindow* window)
{
2024-01-16 22:15:26 +01:00
GameUtils::getInstance()->shaderLoader->DeleteProgram(program);
2024-01-02 20:54:56 +01:00
}
//obsluga wejscia
void processInput(GLFWwindow* window)
{
2024-01-16 22:15:26 +01:00
spaceship->processInput(window, deltaTime, glfwGetTime());
2024-01-03 20:51:29 +01:00
2024-01-03 22:06:27 +01:00
/*if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
2024-01-02 20:54:56 +01:00
exposition -= 0.05;
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
2024-01-03 22:06:27 +01:00
exposition += 0.05;*/
2024-01-02 20:54:56 +01:00
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) {
2024-01-10 21:20:52 +01:00
printf("GameUtils::spaceshipPos = glm::vec3(%ff, %ff, %ff);\n",spaceship->spaceshipPos.x,spaceship->spaceshipPos.y,spaceship->spaceshipPos.z);
printf("GameUtils::spaceshipDir = glm::vec3(%ff, %ff, %ff);\n",spaceship->spaceshipDir.x,spaceship->spaceshipDir.y,spaceship->spaceshipDir.z);
2024-01-02 20:54:56 +01:00
}
//cameraDir = glm::normalize(-cameraPos);
}
// funkcja jest glowna petla
void renderLoop(GLFWwindow* window) {
while (!glfwWindowShouldClose(window))
{
processInput(window);
renderScene(window);
glfwPollEvents();
}
}
//}