add sun shaders and render sun

This commit is contained in:
Szymon Szczubkowski 2024-01-23 23:31:33 +01:00
parent db0ed6d7c4
commit 47c8242bfd
8 changed files with 86 additions and 20 deletions

BIN
cw_8/models/sun.glb Normal file

Binary file not shown.

View File

@ -15,7 +15,7 @@ out vec2 TexCoords;
void main()
{
TexCoords = vertexTexCoord;
TexCoords = -vertexTexCoord;
worldPos = vec3(modelMatrix * vec4(vertexPosition,1));
Normal = normalize((modelMatrix * vec4(vertexNormal,0)).xyz);

15
cw_8/shaders/sun.frag Normal file
View File

@ -0,0 +1,15 @@
#version 430 core
uniform sampler2D colorTexture;
in vec3 vecNormal;
in vec3 worldPos;
in vec2 outVertexTexCoord;
out vec4 outColor;
void main()
{
vec4 textureColor = texture2D(colorTexture, outVertexTexCoord);
outColor = vec4(textureColor.rgb,1);
}

21
cw_8/shaders/sun.vert Normal file
View File

@ -0,0 +1,21 @@
#version 430 core
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal;
layout(location = 2) in vec2 vertexTexCoord;
uniform mat4 transformation;
uniform mat4 modelMatrix;
out vec3 vecNormal;
out vec3 worldPos;
out vec2 outVertexTexCoord;
void main()
{
worldPos = (modelMatrix* vec4(vertexPosition,1)).xyz;
vecNormal = (modelMatrix* vec4(vertexNormal,0)).xyz;
gl_Position = transformation * vec4(vertexPosition, 1.0);
outVertexTexCoord = -1*vertexTexCoord;
}

View File

@ -11,10 +11,21 @@
#include "Texture.h"
GLuint program;
GLuint programSun;
Core::Shader_Loader shaderLoader;
Core::RenderContext shipContext;
Core::RenderContext sphereContext;
Core::RenderContext earthContext;
Core::RenderContext sunContext;
namespace texture {
GLuint earth_albedo;
GLuint earth_normal;
GLuint sun;
}
float aspectRatio = 1.f;
@ -27,22 +38,32 @@ void loadModelToContext(std::string path, Core::RenderContext& context);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
void shutdown(GLFWwindow* window);
glm::mat4 createPerspectiveMatrix();
glm::mat4 createCameraMatrix();
void drawObject(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color);
void drawObjectEarth(Core::RenderContext& context, glm::mat4 modelMatrix);
void drawObjectSun(Core::RenderContext& context, glm::mat4 modelMatrix);
void init(GLFWwindow* window)
{
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glEnable(GL_DEPTH_TEST);
program = shaderLoader.CreateProgram("shaders/pbr.vert", "shaders/pbr.frag");
programSun = shaderLoader.CreateProgram("shaders/sun.vert", "shaders/sun.frag");
//load models here
loadModelToContext("./models/sphere.obj", sphereContext);
loadModelToContext("./models/earth.obj", earthContext);
loadModelToContext("./models/spaceship.obj", shipContext);
loadModelToContext("./models/sun.glb", sunContext);
//load textures here
texture::earth_albedo = Core::LoadTexture("textures/earth/albedo.jpeg");
texture::earth_normal = Core::LoadTexture("textures/earth/bump.jpeg");
texture::sun = Core::LoadTexture("textures/sun/sun.png");
}
void renderScene(GLFWwindow* window){
@ -52,8 +73,8 @@ void renderScene(GLFWwindow* window){
glUseProgram(program);
//desired objects go here
drawObject(sphereContext, glm::mat4(), glm::vec3(255, 0.1, 251));
drawObjectSun(sunContext, glm::mat4());
//drawObjectEarth(earthContext, glm::mat4());
//desired objects end here
glUseProgram(0);
glfwSwapBuffers(window);
@ -64,7 +85,7 @@ void processInput(GLFWwindow* window)
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
float cameraSpeed = 0.05f;
float cameraSpeed = 0.1f;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
cameraPos += cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
@ -75,8 +96,7 @@ void processInput(GLFWwindow* window)
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}
//functions below need not be touched
void drawObject(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color) {
void drawObjectEarth(Core::RenderContext& context, glm::mat4 modelMatrix) {
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
@ -84,21 +104,31 @@ void drawObject(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 c
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniform1f(glGetUniformLocation(program, "exposition"), 1.0f);
glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z);
glUniform1f(glGetUniformLocation(program, "metallic"), 1.0f);
glUniform1f(glGetUniformLocation(program, "roughness"), 1.0f);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glUniform3f(glGetUniformLocation(program, "lightPos"), 0.0f, 0.0f, 0.0f);
glUniform3f(glGetUniformLocation(program, "lightColor"), 30.f, 0.f, 0.f);
Core::SetActiveTexture(texture::earth_albedo, "albedoMap", program, 0);
Core::SetActiveTexture(texture::earth_normal, "normalMap", program, 1);
Core::DrawContext(context);
}
void drawObjectSun(Core::RenderContext& context, glm::mat4 modelMatrix) {
glUseProgram(programSun);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(programSun, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(programSun, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
Core::SetActiveTexture(texture::sun, "colorTexture", programSun, 0);
Core::DrawContext(context);
glUseProgram(program);
}
//functions below need not be touched
void loadModelToContext(std::string path, Core::RenderContext& context)
{
Assimp::Importer import;

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 MiB

BIN
cw_8/textures/sun/sun.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 949 KiB