skybox + slonce
This commit is contained in:
parent
858b42b9e4
commit
4a4f161bb0
17
shaders/shader_4_2.frag
Normal file
17
shaders/shader_4_2.frag
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#version 430 core
|
||||||
|
|
||||||
|
uniform vec3 objectColor;
|
||||||
|
//uniform vec3 lightDir;
|
||||||
|
uniform vec3 lightPos;
|
||||||
|
uniform vec3 cameraPos;
|
||||||
|
|
||||||
|
in vec3 interpNormal;
|
||||||
|
in vec3 fragPos;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec3 normal = normalize(interpNormal);
|
||||||
|
vec3 V = normalize(cameraPos-fragPos);
|
||||||
|
float coef = max(0,dot(V,normal));
|
||||||
|
gl_FragColor = vec4(mix(objectColor,vec3(1,0.5,0.1),1-coef), 1.0);
|
||||||
|
}
|
17
shaders/shader_4_2.vert
Normal file
17
shaders/shader_4_2.vert
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#version 430 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 vertexPosition;
|
||||||
|
layout(location = 1) in vec2 vertexTexCoord;
|
||||||
|
layout(location = 2) in vec3 vertexNormal;
|
||||||
|
|
||||||
|
uniform mat4 transformation;
|
||||||
|
uniform mat4 modelMatrix;
|
||||||
|
out vec3 interpNormal;
|
||||||
|
out vec3 fragPos;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = transformation * vec4(vertexPosition, 1.0);
|
||||||
|
interpNormal = (modelMatrix*vec4(vertexNormal,0)).xyz;
|
||||||
|
fragPos = (modelMatrix*vec4(vertexPosition,1)).xyz;
|
||||||
|
}
|
@ -11,11 +11,19 @@
|
|||||||
#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"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
GLuint programColor;
|
GLuint programColor;
|
||||||
GLuint programTexture;
|
GLuint programTexture;
|
||||||
|
GLuint programSkybox;
|
||||||
|
GLuint programSun;
|
||||||
|
GLuint cubemapTexture;
|
||||||
|
GLuint skyboxVAO, skyboxVBO;
|
||||||
|
|
||||||
Core::Shader_Loader shaderLoader;
|
Core::Shader_Loader shaderLoader;
|
||||||
|
|
||||||
@ -24,16 +32,32 @@ float appLoadingTime;
|
|||||||
obj::Model planeModel;
|
obj::Model planeModel;
|
||||||
obj::Model shipModel;
|
obj::Model shipModel;
|
||||||
obj::Model sphereModel;
|
obj::Model sphereModel;
|
||||||
|
obj::Model boxModel;
|
||||||
|
|
||||||
Core::RenderContext planeContext, shipContext, sphereContext;
|
Core::RenderContext planeContext, shipContext, sphereContext, boxContext;
|
||||||
|
|
||||||
float cameraAngle = glm::radians(0.0f);
|
float cameraAngle = glm::radians(0.0f);
|
||||||
glm::vec3 cameraPos = glm::vec3(0, 0 , 250);
|
glm::vec3 cameraPos = glm::vec3(0, 0 , 250);
|
||||||
glm::vec3 cameraDir;
|
glm::vec3 cameraDir;
|
||||||
|
|
||||||
glm::mat4 cameraMatrix, perspectiveMatrix;
|
glm::mat4 cameraMatrix, perspectiveMatrix;
|
||||||
|
glm::mat4 boxModelMatrix;
|
||||||
|
|
||||||
glm::vec3 lightDir = glm::normalize(glm::vec3(1.0f, -1.0f, -1.0f));
|
glm::vec3 lightDir = glm::normalize(glm::vec3(1.0f, -1.0f, -1.0f));
|
||||||
|
glm::vec3 lightPos = glm::vec3(0, 0, -800);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 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",
|
||||||
|
};
|
||||||
|
|
||||||
GLuint textureTest;
|
GLuint textureTest;
|
||||||
GLuint textureEarth;
|
GLuint textureEarth;
|
||||||
@ -81,12 +105,23 @@ glm::mat4 createCameraMatrix()
|
|||||||
// Obliczanie kierunku patrzenia kamery (w plaszczyznie x-z) przy uzyciu zmiennej cameraAngle kontrolowanej przez klawisze.
|
// Obliczanie kierunku patrzenia kamery (w plaszczyznie x-z) przy uzyciu zmiennej cameraAngle kontrolowanej przez klawisze.
|
||||||
cameraPos = glm::rotate(cameraPos, orbitSpeed, glm::vec3(0, 1, 0));
|
cameraPos = glm::rotate(cameraPos, orbitSpeed, glm::vec3(0, 1, 0));
|
||||||
//cameraDir = glm::vec3(cosf(cameraAngle), 0.0f, sinf(cameraAngle));
|
//cameraDir = glm::vec3(cosf(cameraAngle), 0.0f, sinf(cameraAngle));
|
||||||
cameraDir = glm::normalize(glm::vec3(0, 0, 0) - cameraPos);
|
cameraDir = glm::normalize(glm::vec3(0, -1, 0) - cameraPos);
|
||||||
glm::vec3 up = glm::vec3(0, 1, 0);
|
glm::vec3 up = glm::vec3(0, 1, 0);
|
||||||
|
|
||||||
return Core::createViewMatrix(cameraPos, cameraDir, up);
|
return Core::createViewMatrix(cameraPos, cameraDir, up);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void drawObject(GLuint program, Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 color)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void drawObjectColor(Core::RenderContext* context, glm::mat4 modelMatrix, glm::vec3 color)
|
void drawObjectColor(Core::RenderContext* context, glm::mat4 modelMatrix, glm::vec3 color)
|
||||||
{
|
{
|
||||||
@ -96,6 +131,8 @@ 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);
|
||||||
@ -113,6 +150,8 @@ void drawObjectTexture(Core::RenderContext* context, glm::mat4 modelMatrix, GLui
|
|||||||
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;
|
||||||
@ -137,25 +176,36 @@ void renderScene()
|
|||||||
|
|
||||||
// Aktualizacja macierzy widoku i rzutowania.
|
// Aktualizacja macierzy widoku i rzutowania.
|
||||||
cameraMatrix = createCameraMatrix();
|
cameraMatrix = createCameraMatrix();
|
||||||
perspectiveMatrix = Core::createPerspectiveMatrix(0.1f, 1000.f);
|
perspectiveMatrix = Core::createPerspectiveMatrix(0.1f, 1500.f);
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
|
glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
|
||||||
|
|
||||||
|
// skybox
|
||||||
|
Skybox::drawSkybox(programSkybox, cameraMatrix, perspectiveMatrix, cubemapTexture);
|
||||||
|
|
||||||
//SHIP
|
//SHIP
|
||||||
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::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));
|
||||||
drawObjectTexture(&shipContext, shipModelMatrix, textureShip);
|
drawObjectTexture(&shipContext, shipModelMatrix, textureShip);
|
||||||
|
|
||||||
//SUN
|
//SUN
|
||||||
//drawObjectTexture(&sphereModel, glm::translate(glm::vec3(0, 0, 0)) * glm::scale(glm::vec3(20.f)), textureSun);
|
//drawObjectTexture(&sphereContext, glm::translate(glm::vec3(0, 0, -800)) * glm::scale(glm::vec3(10.f)), textureSun);
|
||||||
|
|
||||||
//EARTH nieruchoma - ³atwiejsze dla naszej cutscenki
|
//EARTH nieruchoma - ³atwiejsze dla naszej cutscenki
|
||||||
drawObjectTexture(&sphereContext, glm::scale(glm::vec3(45)), textureEarthHighres);
|
drawObjectTexture(&sphereContext, glm::scale(glm::vec3(45)), textureEarthHighres);
|
||||||
|
|
||||||
//MOON
|
//MOON
|
||||||
drawObjectTexture(&sphereContext, glm::rotate(time / 5.0f, glm::vec3(0, 1, 0)) * glm::translate(glm::vec3(0, 0, 300)) * glm::scale(glm::vec3(20)), textureMoon);
|
drawObjectTexture(&sphereContext, glm::rotate(time / 50.0f, glm::vec3(0, 1, 0)) * glm::translate(glm::vec3(0, 0, 300)) * glm::scale(glm::vec3(20)), textureMoon);
|
||||||
|
|
||||||
|
|
||||||
|
// sun
|
||||||
|
glUseProgram(programSun);
|
||||||
|
glUniform3f(glGetUniformLocation(programSun, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
|
||||||
|
glUniform3f(glGetUniformLocation(programSun, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
|
||||||
|
drawObject(programSun, sphereContext, glm::translate(lightPos) * glm::scale(glm::vec3(10.f)), glm::vec3(1.0f, 0.8f, 0.2f));
|
||||||
|
|
||||||
|
glUseProgram(0);
|
||||||
|
|
||||||
glutSwapBuffers();
|
glutSwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,6 +215,7 @@ void init()
|
|||||||
|
|
||||||
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");
|
||||||
|
programSun = shaderLoader.CreateProgram("shaders/shader_4_2.vert", "shaders/shader_4_2.frag");
|
||||||
|
|
||||||
shipModel = obj::loadModelFromFile("models/ship.obj");
|
shipModel = obj::loadModelFromFile("models/ship.obj");
|
||||||
shipContext.initFromOBJ(shipModel);
|
shipContext.initFromOBJ(shipModel);
|
||||||
@ -172,6 +223,12 @@ void init()
|
|||||||
sphereModel = obj::loadModelFromFile("models/sphere.obj");
|
sphereModel = obj::loadModelFromFile("models/sphere.obj");
|
||||||
sphereContext.initFromOBJ(sphereModel);
|
sphereContext.initFromOBJ(sphereModel);
|
||||||
|
|
||||||
|
boxModel = obj::loadModelFromFile("models/box.obj");
|
||||||
|
boxContext.initFromOBJ(boxModel);
|
||||||
|
programSkybox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
|
||||||
|
|
||||||
|
cubemapTexture = Skybox::loadCubemap(faces);
|
||||||
|
|
||||||
textureShip = Core::LoadTexture("textures/spaceship.png");
|
textureShip = Core::LoadTexture("textures/spaceship.png");
|
||||||
textureEarthHighres = Core::LoadTexture("textures/4k_earth_daymap.png");
|
textureEarthHighres = Core::LoadTexture("textures/4k_earth_daymap.png");
|
||||||
textureAsteroid = Core::LoadTexture("textures/asteroid.png");
|
textureAsteroid = Core::LoadTexture("textures/asteroid.png");
|
||||||
@ -205,7 +262,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 Pierwszy Program");
|
glutCreateWindow("Projekt GRK");
|
||||||
glewInit();
|
glewInit();
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
Loading…
Reference in New Issue
Block a user