Space-Project/src/main.cpp

399 lines
12 KiB
C++
Raw Normal View History

2021-01-16 17:11:39 +01:00
#include "glew.h"
#include "freeglut.h"
#include "glm.hpp"
#include "ext.hpp"
#include <iostream>
#include <cmath>
#include <ctime>
#include <vector>
2021-01-16 17:11:39 +01:00
#include "Shader_Loader.h"
#include "Render_Utils.h"
#include "Camera.h"
#include "Texture.h"
2021-01-16 17:11:39 +01:00
#include "Box.cpp"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
2021-01-24 02:57:03 +01:00
#include "model.h"
2021-01-16 17:11:39 +01:00
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
//int winId;
2021-01-16 17:11:39 +01:00
GLuint program;
GLuint programTex;
2021-01-16 17:11:39 +01:00
GLuint programSun;
2021-01-17 01:26:29 +01:00
GLuint programSkybox;
2021-01-16 17:11:39 +01:00
Core::Shader_Loader shaderLoader;
Core::RenderContext armContext;
std::vector<Core::Node> arm;
int ballIndex;
GLuint textureShip_normals;
GLuint sunTexture;
GLuint earthTexture;
2021-01-16 21:20:03 +01:00
GLuint moonTexture;
2021-01-17 01:26:29 +01:00
GLuint skyboxTexture;
GLuint shipTexture;
obj::Model sphereModel;
2021-01-17 01:26:29 +01:00
obj::Model cubeModel;
obj::Model shipModel;
2021-01-24 02:57:03 +01:00
Core::RenderContext sphereContext;
2021-01-17 01:26:29 +01:00
Core::RenderContext cubeContext;
Core::RenderContext shipContext;
2021-01-16 17:11:39 +01:00
2021-01-24 02:57:03 +01:00
//assimp
std::shared_ptr<Model> corvette;
std::vector<Core::RenderContext> corvetteMeshes;
2021-01-16 17:11:39 +01:00
float cameraAngle = 0;
glm::vec3 cameraPos = glm::vec3(-6, 0, 0);
glm::vec3 cameraDir;
glm::mat4 cameraMatrix, perspectiveMatrix;
2021-01-25 23:50:53 +01:00
glm::vec3 sunPos = glm::vec3(10.0f, 0.0f, -5.0f);
glm::vec3 sunPos2 = glm::vec3(25.0f, -1.0f, 10.0f);
glm::vec3 engineLight = cameraPos + cameraDir * 0.6f + glm::vec3(0, -0.25f, 0);
2021-01-17 01:26:29 +01:00
2021-01-16 21:20:03 +01:00
struct Light {
2021-01-17 01:26:29 +01:00
glm::vec3 position;
2021-01-16 21:20:03 +01:00
glm::vec3 color;
2021-01-25 23:50:53 +01:00
float intensity;
2021-01-16 21:20:03 +01:00
};
2021-01-25 23:50:53 +01:00
int engineLightTimer;
2021-01-24 19:21:05 +01:00
//wczytywanie skyboxa (musi byc jpg!)
2021-01-17 01:26:29 +01:00
std::vector<std::string> faces
{
"skybox/right.jpg",
"skybox/left.jpg",
"skybox/top.jpg",
"skybox/bottom.jpg",
"skybox/front.jpg",
"skybox/back.jpg"
};
2021-01-16 21:20:03 +01:00
std::vector<Light> lights;
2021-01-16 17:11:39 +01:00
void keyboard(unsigned char key, int x, int y)
{
float angleSpeed = 0.1f;
float moveSpeed = 0.1f;
switch (key)
{
case 'z': cameraAngle -= angleSpeed; break;
case 'x': cameraAngle += angleSpeed; break;
2021-01-25 23:50:53 +01:00
case 'w':
{
cameraPos += cameraDir * moveSpeed;
lights[2].intensity = 0.001;
engineLightTimer = 0;
break;
}
2021-01-16 17:11:39 +01:00
case 's': cameraPos -= cameraDir * moveSpeed; break;
case 'd': cameraPos += glm::cross(cameraDir, glm::vec3(0, 1, 0)) * moveSpeed; break;
case 'a': cameraPos -= glm::cross(cameraDir, glm::vec3(0, 1, 0)) * moveSpeed; break;
case 'e': cameraPos += glm::cross(cameraDir, glm::vec3(1, 0, 0)) * moveSpeed; break;
case 'q': cameraPos -= glm::cross(cameraDir, glm::vec3(1, 0, 0)) * moveSpeed; break;
}
}
glm::mat4 createCameraMatrix()
{
// Obliczanie kierunku patrzenia kamery (w plaszczyznie x-z) przy uzyciu zmiennej cameraAngle kontrolowanej przez klawisze.
cameraDir = glm::vec3(cosf(cameraAngle), 0.0f, sinf(cameraAngle));
glm::vec3 up = glm::vec3(0, 1, 0);
return Core::createViewMatrix(cameraPos, cameraDir, up);
}
float frustumScale = 1.f;
2021-01-16 17:11:39 +01:00
2021-01-17 01:26:29 +01:00
2021-01-16 17:11:39 +01:00
void drawObject(GLuint program, Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 color)
{
glUseProgram(program);
2021-01-16 17:11:39 +01:00
glUniform3f(glGetUniformLocation(program, "objectColor"), color.x, color.y, color.z);
glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
Core::DrawContext(context);
glUseProgram(0);
}
2021-01-24 19:21:05 +01:00
//funkcja rysujaca modele za pomoca assimpa
2021-01-24 02:57:03 +01:00
void drawFromAssimpModel(GLuint program, std::shared_ptr<Model> model, glm::mat4 modelMatrix, glm::vec3 color)
{
glUseProgram(program);
glUniform3f(glGetUniformLocation(program, "objectColor"), color.x, color.y, color.z);
glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
model->Draw(program);
glUseProgram(0);
}
2021-01-17 01:26:29 +01:00
//Skybox
unsigned int loadCubemap(std::vector<std::string> faces)
{
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height, nrChannels;
for (unsigned int i = 0; i < faces.size(); i++)
{
unsigned char *data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
if (data)
{
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 << "Cubemap tex failed to load at path: " << faces[i] << std::endl;
stbi_image_free(data);
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_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);
return textureID;
}
void drawSkybox(GLuint program, Core::RenderContext context, GLuint texID)
{
glUseProgram(program);
glm::mat4 transformation = perspectiveMatrix * glm::mat4(glm::mat3(cameraMatrix));
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glDepthMask(GL_FALSE);
//Core::SetActiveTexture(texID, "skybox", program, 0);
glUniform1i(glGetUniformLocation(program, "skybox"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
Core::DrawContext(context);
glDepthMask(GL_TRUE);
glUseProgram(0);
}
//Textures
void drawObjectTexture(GLuint program, Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 texture, GLuint texID)
{
glUseProgram(program);
glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
2021-01-17 01:26:29 +01:00
glUniform3f(glGetUniformLocation(program, "colorTex"), texture.x, texture.y, texture.z);
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
Core::SetActiveTexture(texID, "colorTexture", program, 0);
Core::DrawContext(context);
glUseProgram(0);
}
2021-01-24 19:21:05 +01:00
//funkcja rysujaca planety (bez obracania wokol wlasnej osi bo ksiezyce sie psuja)
2021-01-24 23:18:02 +01:00
glm::mat4 drawPlanet(float time, glm::vec3 sunPos, glm::vec3 orbit, glm::vec3 translation, glm::vec3 scale)
{
glm::mat4 planetModelMatrix = glm::mat4(1.0f);
2021-01-24 23:18:02 +01:00
planetModelMatrix = glm::translate(planetModelMatrix, sunPos);
planetModelMatrix = glm::rotate(planetModelMatrix, time, orbit);
planetModelMatrix = glm::translate(planetModelMatrix, translation);
planetModelMatrix = glm::scale(planetModelMatrix, scale);
return planetModelMatrix;
2021-01-16 17:11:39 +01:00
}
2021-01-24 19:21:05 +01:00
//funkcja rysujaca ksiezyce orbitujace wokol danej planety
2021-01-16 21:20:03 +01:00
glm::mat4 drawMoon(glm::mat4 planetModelMatrix, float time, glm::vec3 orbit, glm::vec3 translation, glm::vec3 rotation, glm::vec3 scale)
{
glm::mat4 moonModelMatrix = glm::mat4(planetModelMatrix);
moonModelMatrix = glm::rotate(moonModelMatrix, time, orbit);
moonModelMatrix = glm::translate(moonModelMatrix, translation);
moonModelMatrix = glm::rotate(moonModelMatrix, time, rotation);
moonModelMatrix = glm::scale(moonModelMatrix, scale);
return moonModelMatrix;
}
2021-01-16 17:11:39 +01:00
void renderScene()
{
cameraMatrix = createCameraMatrix();
perspectiveMatrix = Core::createPerspectiveMatrix(0.01f, 1000.0f, frustumScale);
2021-01-16 17:11:39 +01:00
float time = glutGet(GLUT_ELAPSED_TIME) / 1000.f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
2021-01-16 17:11:39 +01:00
2021-01-17 01:26:29 +01:00
drawSkybox(programSkybox, cubeContext, skyboxTexture);
glUseProgram(programSun);
glUniform3f(glGetUniformLocation(programSun, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
2021-01-24 19:21:05 +01:00
//ustalanie pozycji s<>o<EFBFBD>c (lightPos)
2021-01-25 23:50:53 +01:00
2021-01-16 21:20:03 +01:00
2021-01-24 19:21:05 +01:00
//rysowanie s<>o<EFBFBD>c
glm::mat4 sunModelMatrix = glm::mat4(1.0f);
2021-01-16 21:20:03 +01:00
sunModelMatrix = glm::translate(sunModelMatrix, sunPos);
2021-01-24 23:18:02 +01:00
sunModelMatrix = glm::scale(sunModelMatrix, glm::vec3(3.0f, 3.0f, 3.0f));
drawObjectTexture(programSun, sphereContext, sunModelMatrix, glm::vec3(1.5f, 1.8f, 1.8f), sunTexture);
2021-01-17 01:26:29 +01:00
glm::mat4 sunModelMatrix2 = glm::mat4(1.0f);
sunModelMatrix2 = glm::translate(sunModelMatrix2, sunPos2);
drawObjectTexture(programSun, sphereContext, sunModelMatrix2, glm::vec3(0.9f, 0.9f, 2.0f), sunTexture);
2021-01-25 23:50:53 +01:00
glUseProgram(programTex);
2021-01-24 02:57:03 +01:00
2021-01-17 01:26:29 +01:00
lights[0].position = sunPos;
lights[1].position = sunPos2;
2021-01-25 23:50:53 +01:00
lights[2].position = cameraPos + cameraDir * 0.6f + glm::vec3(0, -0.25f, 0);
lights[2].color = glm::vec3(1.0f, 0.0f, 0.0f);
2021-01-17 01:26:29 +01:00
for (int i = 0; i < lights.size(); i++)
{
std::string col = "pointLights[" + std::to_string(i) + "].color";
std::string pos = "pointLights[" + std::to_string(i) + "].position";
2021-01-25 23:50:53 +01:00
std::string ins = "pointLights[" + std::to_string(i) + "].intensity";
2021-01-17 01:26:29 +01:00
glUniform3f(glGetUniformLocation(programTex, col.c_str()), lights[i].color.x, lights[i].color.y, lights[i].color.z);
glUniform3f(glGetUniformLocation(programTex, pos.c_str()), lights[i].position.x, lights[i].position.y, lights[i].position.z);
2021-01-25 23:50:53 +01:00
glUniform1f(glGetUniformLocation(programTex, ins.c_str()), lights[i].intensity);
2021-01-17 01:26:29 +01:00
}
glUniform3f(glGetUniformLocation(programTex, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
2021-01-24 19:21:05 +01:00
2021-01-25 23:50:53 +01:00
2021-01-24 19:21:05 +01:00
//rysowanie statku
glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.6f + glm::vec3(0, -0.25f, 0)) * glm::rotate(-cameraAngle + glm::radians(90.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.0001f));
drawFromAssimpModel(programTex, corvette, shipModelMatrix, glm::vec3(1));
2021-01-25 23:50:53 +01:00
2021-01-24 19:21:05 +01:00
//rysowanie Ziemi z ksi<73><69>ycem
2021-01-24 23:18:02 +01:00
glm::mat4 earth = drawPlanet(time / 5.0f, sunPos*glm::vec3(1.5f,1,1), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(-10.5f, 0.0f, -10.5f), glm::vec3(0.5f, 0.5f, 0.5f));
glm::mat4 moon = drawMoon(earth, time/2.0f, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0, 1, 1), glm::vec3(1.5f, 1.0f, 1.0f), glm::vec3(0.3f, 0.3f, 0.3f));
2021-01-16 21:20:03 +01:00
earth = glm::rotate(earth, time/3.0f, glm::vec3(0.0f, 0.0f, 1.0f));
drawObjectTexture(programTex, sphereContext, earth, glm::vec3(0.8f, 0.8f, 0.8f), earthTexture);
2021-01-16 21:20:03 +01:00
drawObjectTexture(programTex, sphereContext, moon, glm::vec3(0.9f, 1.0f, 0.9f), moonTexture);
2021-01-24 19:21:05 +01:00
2021-01-25 23:50:53 +01:00
if (engineLightTimer < 50) engineLightTimer++;
else lights[2].intensity = 0.0001;
2021-01-16 17:11:39 +01:00
glUseProgram(0);
glutSwapBuffers();
}
void init()
{
glEnable(GL_DEPTH_TEST);
program = shaderLoader.CreateProgram("shaders/shader_4_1.vert", "shaders/shader_4_1.frag");
programTex = shaderLoader.CreateProgram("shaders/shader_4_tex.vert", "shaders/shader_4_tex.frag");
2021-01-16 17:11:39 +01:00
programSun = shaderLoader.CreateProgram("shaders/shader_4_sun.vert", "shaders/shader_4_sun.frag");
2021-01-17 01:26:29 +01:00
programSkybox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
2021-01-16 17:11:39 +01:00
2021-01-24 02:57:03 +01:00
corvette = std::make_shared<Model>("models/Corvette-F3.obj");
//shipModel = obj::loadModelFromFile("models/spaceship.obj");
sphereModel = obj::loadModelFromFile("models/sphere.obj");
2021-01-17 01:26:29 +01:00
cubeModel = obj::loadModelFromFile("models/cube.obj");
sphereContext.initFromOBJ(sphereModel);
2021-01-17 01:26:29 +01:00
cubeContext.initFromOBJ(cubeModel);
2021-01-24 02:57:03 +01:00
//shipContext.initFromOBJ(shipModel);
shipTexture = Core::LoadTexture("textures/spaceship.png");
sunTexture = Core::LoadTexture("textures/sun.png");
earthTexture = Core::LoadTexture("textures/earth2.png");
2021-01-16 21:20:03 +01:00
moonTexture = Core::LoadTexture("textures/moon.png");
2021-01-17 01:26:29 +01:00
skyboxTexture = loadCubemap(faces);
Light l1;
2021-01-25 23:50:53 +01:00
l1.position = sunPos;
2021-01-17 01:26:29 +01:00
l1.color = glm::vec3(0.8f, 0.8f, 0.7f);
2021-01-25 23:50:53 +01:00
l1.intensity = 3;
2021-01-17 01:26:29 +01:00
lights.push_back(l1);
Light l2;
2021-01-25 23:50:53 +01:00
l2.position = sunPos2;
2021-01-17 01:26:29 +01:00
l2.color = glm::vec3(0.5f, 0.5f, 0.5f);
2021-01-25 23:50:53 +01:00
l2.intensity = 3;
2021-01-17 01:26:29 +01:00
lights.push_back(l2);
2021-01-25 23:50:53 +01:00
Light l3;
l3.position = engineLight;
l3.color = glm::vec3(0.5f, 0.5f, 0.5f);
l3.intensity = 0.0001;
lights.push_back(l3);
2021-01-16 17:11:39 +01:00
}
void shutdown()
{
shaderLoader.DeleteProgram(program);
}
void onReshape(int width, int height)
{
// Kiedy rozmiar okna sie zmieni, obraz jest znieksztalcony.
// Dostosuj odpowiednio macierz perspektywy i viewport.
// Oblicz odpowiednio globalna zmienna "frustumScale".
// Ustaw odpowiednio viewport - zobacz:
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glViewport.xhtml
frustumScale = (float)width / (float)height;
glViewport(0, 0, width, height);
}
2021-01-16 17:11:39 +01:00
void idle()
{
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
2021-01-24 20:49:53 +01:00
glutSetOption(GLUT_MULTISAMPLE, 8);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
glEnable(GL_MULTISAMPLE);
glutInitWindowPosition(100, 200);
glutInitWindowSize(1240, 720);
glutCreateWindow("GRK-PROJECT WIP");
//winId = glutCreateWindow("OpenGL + PhysX");
//glutFullScreen();
2021-01-16 17:11:39 +01:00
glewInit();
init();
glutKeyboardFunc(keyboard);
glutDisplayFunc(renderScene);
glutIdleFunc(idle);
glutReshapeFunc(onReshape);
2021-01-16 17:11:39 +01:00
glutMainLoop();
shutdown();
return 0;
}