multilights, two suns,

This commit is contained in:
xkamikoo 2021-01-17 01:26:29 +01:00
parent 0ac976fa5c
commit 8b245730aa
15 changed files with 166 additions and 17 deletions

0
Skybox.h Normal file
View File

View File

@ -17,6 +17,8 @@
<None Include="shaders\shader_4_tex.vert" />
<None Include="shaders\shader_color.frag" />
<None Include="shaders\shader_color.vert" />
<None Include="shaders\shader_skybox.frag" />
<None Include="shaders\shader_skybox.vert" />
<None Include="shaders\shader_tex.frag" />
<None Include="shaders\shader_tex.vert" />
</ItemGroup>
@ -30,6 +32,7 @@
<ClCompile Include="src\Texture.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Skybox.h" />
<ClInclude Include="src\Camera.h" />
<ClInclude Include="src\mesh.h" />
<ClInclude Include="src\model.h" />

View File

@ -42,6 +42,12 @@
<None Include="shaders\shader_4_sun.vert">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_skybox.frag">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_skybox.vert">
<Filter>Shader Files</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Box.cpp">
@ -94,5 +100,8 @@
<ClInclude Include="src\model.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="Skybox.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -5,7 +5,7 @@ uniform vec3 objectColor;
uniform vec3 lightPos;
uniform vec3 cameraPos;
uniform sampler2D colorTexture;
uniform vec3 colorTex;
in vec3 interpNormal;
in vec3 fragPos;
in vec2 vTexCoord;
@ -16,6 +16,6 @@ void main()
vec3 V = normalize(cameraPos-fragPos);
float coef = pow(max(0,dot(normal,V)),3);
vec4 textureColor = texture2D(colorTexture, -vTexCoord);
vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z);
vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z) * colorTex;
gl_FragColor = vec4(texture + texture * coef, 1.0);
}

View File

@ -1,26 +1,42 @@
#version 430 core
struct PointLight {
vec3 position;
vec3 color;
};
#define NR_POINT_LIGHTS 2
uniform vec3 objectColor;
//uniform vec3 lightDir;
uniform vec3 lightPos;
uniform vec3 cameraPos;
uniform sampler2D colorTexture;
uniform PointLight pointLights[NR_POINT_LIGHTS];
in vec3 interpNormal;
in vec3 fragPos;
in vec2 vTexCoord;
void main()
{
vec3 lightDir = normalize(lightPos-fragPos);
vec3 V = normalize(cameraPos-fragPos);
vec3 normal = normalize(interpNormal);
vec3 R = reflect(-normalize(lightDir),normal);
float specular = pow(max(0,dot(R,V)),10);
float diffuse = max(0,dot(normal,normalize(lightDir)));
vec3 fragColor = vec3(0,0,0);
vec4 textureColor = texture2D(colorTexture, -vTexCoord);
vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z);
vec4 ambient = vec4(0.1, 0.1, 0.1, 1.0) * textureColor;
gl_FragColor = vec4(mix(texture,texture*diffuse+vec3(1)*specular,0.9), 1.0) + ambient;
vec3 normal = normalize(interpNormal);
for(int i = 0; i < NR_POINT_LIGHTS; i++)
{
vec3 lightDir = normalize(pointLights[i].position - fragPos);
vec3 V = normalize(cameraPos-fragPos);
vec3 R = reflect(-normalize(lightDir),normal);
float specular = pow(max(0,dot(R,V)),10);
float diffuse = max(0,dot(normal,normalize(lightDir)));
vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z) * pointLights[i].color;
fragColor += mix(texture,texture*diffuse+vec3(1)*specular,0.9);
}
gl_FragColor = vec4(fragColor, 1.0) + ambient;
}

View File

@ -0,0 +1,11 @@
#version 330 core
out vec4 FragColor;
in vec3 TexCoords;
uniform samplerCube skybox;
void main()
{
FragColor = texture(skybox, TexCoords);
}

View File

@ -0,0 +1,12 @@
#version 330 core
layout(location = 0) in vec3 vertexPosition;
out vec3 TexCoords;
uniform mat4 transformation;
void main()
{
TexCoords = vertexPosition;
gl_Position = transformation * vec4(vertexPosition, 1.0);
}

BIN
skybox/back.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 303 KiB

BIN
skybox/bottom.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 352 KiB

BIN
skybox/cubemap.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 MiB

BIN
skybox/front.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 368 KiB

BIN
skybox/left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 336 KiB

BIN
skybox/right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 358 KiB

BIN
skybox/top.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 KiB

View File

@ -24,6 +24,7 @@
GLuint program;
GLuint programTex;
GLuint programSun;
GLuint programSkybox;
Core::Shader_Loader shaderLoader;
Core::RenderContext armContext;
@ -33,8 +34,11 @@ int ballIndex;
GLuint sunTexture;
GLuint earthTexture;
GLuint moonTexture;
GLuint skyboxTexture;
obj::Model sphereModel;
obj::Model cubeModel;
Core::RenderContext sphereContext;
Core::RenderContext cubeContext;
float cameraAngle = 0;
@ -43,11 +47,23 @@ glm::vec3 cameraDir;
glm::mat4 cameraMatrix, perspectiveMatrix;
struct Light {
glm::vec3 pos;
glm::vec3 position;
glm::vec3 color;
};
std::vector<std::string> faces
{
"skybox/right.jpg",
"skybox/left.jpg",
"skybox/top.jpg",
"skybox/bottom.jpg",
"skybox/front.jpg",
"skybox/back.jpg"
};
std::vector<Light> lights;
void keyboard(unsigned char key, int x, int y)
@ -76,6 +92,8 @@ glm::mat4 createCameraMatrix()
return Core::createViewMatrix(cameraPos, cameraDir, up);
}
void drawObject(GLuint program, Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 color)
{
glUseProgram(program);
@ -92,11 +110,61 @@ void drawObject(GLuint program, Core::RenderContext context, glm::mat4 modelMatr
glUseProgram(0);
}
//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);
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);
@ -134,21 +202,36 @@ void renderScene()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
drawSkybox(programSkybox, cubeContext, skyboxTexture);
glUseProgram(programSun);
glUniform3f(glGetUniformLocation(programSun, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glm::vec3 sunPos = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 sunPos2 = glm::vec3(5.0f, -1.0f, 10.0f);
glm::mat4 sunModelMatrix = glm::mat4(1.0f);
sunModelMatrix = glm::translate(sunModelMatrix, sunPos);
sunModelMatrix = glm::rotate(sunModelMatrix, time/10.0f, glm::vec3(0.0f, 1.0f, 1.0f));
drawObjectTexture(programSun, sphereContext, sunModelMatrix, glm::vec3(0.8f, 0.5f, 0.1f), sunTexture);
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);
glm::vec3 lightPos = sunPos;
glUniform3f(glGetUniformLocation(programTex, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
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));
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));
@ -167,12 +250,27 @@ void init()
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");
programSun = shaderLoader.CreateProgram("shaders/shader_4_sun.vert", "shaders/shader_4_sun.frag");
programSkybox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
sphereModel = obj::loadModelFromFile("models/sphere.obj");
cubeModel = obj::loadModelFromFile("models/cube.obj");
sphereContext.initFromOBJ(sphereModel);
cubeContext.initFromOBJ(cubeModel);
sunTexture = Core::LoadTexture("textures/sun.png");
earthTexture = Core::LoadTexture("textures/earth2.png");
moonTexture = Core::LoadTexture("textures/moon.png");
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);
}
void shutdown()