Space-Project/src/main.cpp

306 lines
9.6 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 "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>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
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 sunTexture;
GLuint earthTexture;
2021-01-16 21:20:03 +01:00
GLuint moonTexture;
2021-01-17 01:26:29 +01:00
GLuint skyboxTexture;
obj::Model sphereModel;
2021-01-17 01:26:29 +01:00
obj::Model cubeModel;
Core::RenderContext sphereContext;
2021-01-17 01:26:29 +01:00
Core::RenderContext cubeContext;
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-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-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;
case 'w': cameraPos += cameraDir * moveSpeed; break;
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);
}
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-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);
}
glm::mat4 drawPlanet(float time, glm::vec3 orbit, glm::vec3 translation, glm::vec3 scale)
{
glm::mat4 planetModelMatrix = glm::mat4(1.0f);
planetModelMatrix = glm::translate(planetModelMatrix, glm::vec3(2, 0, 2)); //pozycja s<>o<EFBFBD>ca
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-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();
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-16 21:20:03 +01:00
glm::vec3 sunPos = glm::vec3(0.0f, 0.0f, 0.0f);
2021-01-17 01:26:29 +01:00
glm::vec3 sunPos2 = glm::vec3(5.0f, -1.0f, 10.0f);
2021-01-16 21:20:03 +01:00
glm::mat4 sunModelMatrix = glm::mat4(1.0f);
2021-01-16 21:20:03 +01:00
sunModelMatrix = glm::translate(sunModelMatrix, sunPos);
sunModelMatrix = glm::rotate(sunModelMatrix, time/10.0f, glm::vec3(0.0f, 1.0f, 1.0f));
2021-01-17 01:26:29 +01:00
drawObjectTexture(programSun, sphereContext, sunModelMatrix, glm::vec3(0.5f, 0.8f, 0.8f), sunTexture);
glm::mat4 sunModelMatrix2 = glm::mat4(1.0f);
sunModelMatrix2 = glm::translate(sunModelMatrix2, sunPos2);
sunModelMatrix2 = glm::rotate(sunModelMatrix2, time / 10.0f, glm::vec3(0.0f, 1.0f, 1.0f));
drawObjectTexture(programSun, sphereContext, sunModelMatrix2, glm::vec3(0.9f, 0.9f, 2.0f), sunTexture);
glUseProgram(programTex);
2021-01-17 01:26:29 +01:00
lights[0].position = sunPos;
lights[1].position = sunPos2;
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";
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);
}
glUniform3f(glGetUniformLocation(programTex, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glm::mat4 earth = drawPlanet(time / 5.0f, glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(-3.5f, 0.0f, -3.5f), glm::vec3(0.5f, 0.5f, 0.5f));
2021-01-16 21:20:03 +01:00
glm::mat4 moon = drawMoon(earth, time/2.0f, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0, 1, 1), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.2f, 0.2f, 0.2f));
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-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
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);
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;
l1.position = glm::vec3(0.0f, 0.0f, 0.0f);
l1.color = glm::vec3(0.8f, 0.8f, 0.7f);
lights.push_back(l1);
Light l2;
l2.position = glm::vec3(5.0f, -1.0f, 10.0f);
l2.color = glm::vec3(0.5f, 0.5f, 0.5f);
lights.push_back(l2);
2021-01-16 17:11:39 +01:00
}
void shutdown()
{
shaderLoader.DeleteProgram(program);
}
void idle()
{
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200, 300);
glutInitWindowSize(600, 600);
glutCreateWindow("GRK-PROJECT WIP");
2021-01-16 17:11:39 +01:00
glewInit();
init();
glutKeyboardFunc(keyboard);
glutDisplayFunc(renderScene);
glutIdleFunc(idle);
glutMainLoop();
shutdown();
return 0;
}