add sun and earth, basic textures and lighting

This commit is contained in:
xkamikoo 2021-01-16 20:33:47 +01:00
parent 6f33df50a3
commit a403ea75d6
12 changed files with 101 additions and 15 deletions

View File

@ -11,6 +11,10 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<None Include="shaders\shader_4_sun.frag" />
<None Include="shaders\shader_4_sun.vert" />
<None Include="shaders\shader_4_tex.frag" />
<None Include="shaders\shader_4_tex.vert" />
<None Include="shaders\shader_color.frag" />
<None Include="shaders\shader_color.vert" />
<None Include="shaders\shader_tex.frag" />
@ -40,7 +44,7 @@
<ProjectGuid>{1B448102-E76C-4347-BDC7-40D02A567DB6}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>grk-cw9</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
<ProjectName>grk-project</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
@ -55,7 +59,7 @@
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">

View File

@ -30,6 +30,18 @@
<None Include="shaders\shader_tex.vert">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_4_tex.frag">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_4_tex.vert">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_4_sun.frag">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_4_sun.vert">
<Filter>Shader Files</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Box.cpp">

View File

@ -4,14 +4,18 @@ uniform vec3 objectColor;
//uniform vec3 lightDir;
uniform vec3 lightPos;
uniform vec3 cameraPos;
uniform sampler2D colorTexture;
in vec3 interpNormal;
in vec3 fragPos;
in vec2 vTexCoord;
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);
float coef = pow(max(0,dot(normal,V)),3);
vec4 textureColor = texture2D(colorTexture, -vTexCoord);
vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z);
gl_FragColor = vec4(texture + texture * coef, 1.0);
}

View File

@ -8,10 +8,12 @@ uniform mat4 transformation;
uniform mat4 modelMatrix;
out vec3 interpNormal;
out vec3 fragPos;
out vec2 vTexCoord;
void main()
{
gl_Position = transformation * vec4(vertexPosition, 1.0);
interpNormal = (modelMatrix*vec4(vertexNormal,0)).xyz;
fragPos = (modelMatrix*vec4(vertexPosition,1)).xyz;
vTexCoord = vertexTexCoord;
}

View File

@ -4,9 +4,11 @@ uniform vec3 objectColor;
//uniform vec3 lightDir;
uniform vec3 lightPos;
uniform vec3 cameraPos;
uniform sampler2D colorTexture;
in vec3 interpNormal;
in vec3 fragPos;
in vec2 vTexCoord;
void main()
{
@ -17,5 +19,8 @@ void main()
float specular = pow(max(0,dot(R,V)),10);
float diffuse = max(0,dot(normal,normalize(lightDir)));
gl_FragColor = vec4(mix(objectColor,objectColor*diffuse+vec3(1)*specular,0.9), 1.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;
}

View File

@ -6,12 +6,15 @@ layout(location = 2) in vec3 vertexNormal;
uniform mat4 transformation;
uniform mat4 modelMatrix;
out vec3 interpNormal;
out vec3 fragPos;
out vec2 vTexCoord;
void main()
{
gl_Position = transformation * vec4(vertexPosition, 1.0);
interpNormal = (modelMatrix*vec4(vertexNormal,0)).xyz;
fragPos = (modelMatrix*vec4(vertexPosition,1)).xyz;
vTexCoord = vertexTexCoord;
}

View File

@ -1,9 +1,5 @@
#include "Texture.h"
#include <fstream>
#include <iterator>
#include <vector>
#include "picopng.h"
typedef unsigned char byte;
@ -18,6 +14,12 @@ GLuint Core::LoadTexture( const char * filepath )
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
std::ifstream input( filepath, std::ios::binary );
if (!input.is_open())
{
std::cout << "Nie ma tekstury o takiej nazwie." << std::endl;
input.open("textures/a.jpg", std::ios::binary);
}
std::vector<char> buffer((
std::istreambuf_iterator<char>(input)),
(std::istreambuf_iterator<char>()));
@ -25,7 +27,6 @@ GLuint Core::LoadTexture( const char * filepath )
unsigned long w, h;
std::vector<unsigned char> decoded;
decodePNG(decoded, w, h, (unsigned char*)&buffer[0], buffer.size(), true);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, &decoded[0]);
glGenerateMipmap(GL_TEXTURE_2D);

View File

@ -1,5 +1,10 @@
#pragma once
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include "picopng.h"
#include "glew.h"
#include "freeglut.h"

View File

@ -9,7 +9,7 @@
#include "Shader_Loader.h"
#include "Render_Utils.h"
#include "Camera.h"
#include "Texture.h"
#include "Box.cpp"
@ -22,6 +22,7 @@
#include "stb_image.h"
GLuint program;
GLuint programTex;
GLuint programSun;
Core::Shader_Loader shaderLoader;
@ -30,6 +31,11 @@ Core::RenderContext armContext;
std::vector<Core::Node> arm;
int ballIndex;
GLuint sunTexture;
GLuint earthTexture;
obj::Model sphereModel;
Core::RenderContext sphereContext;
float cameraAngle = 0;
glm::vec3 cameraPos = glm::vec3(-6, 0, 0);
@ -65,6 +71,8 @@ glm::mat4 createCameraMatrix()
void drawObject(GLuint program, Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 color)
{
glUseProgram(program);
glUniform3f(glGetUniformLocation(program, "objectColor"), color.x, color.y, color.z);
glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
@ -74,6 +82,30 @@ void drawObject(GLuint program, Core::RenderContext context, glm::mat4 modelMatr
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
Core::DrawContext(context);
glUseProgram(0);
}
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);
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ñca
planetModelMatrix = glm::rotate(planetModelMatrix, time, orbit);
planetModelMatrix = glm::translate(planetModelMatrix, translation);
planetModelMatrix = glm::scale(planetModelMatrix, scale);
return planetModelMatrix;
}
void renderScene()
@ -86,16 +118,29 @@ void renderScene()
float time = glutGet(GLUT_ELAPSED_TIME) / 1000.f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glUseProgram(program);
// Macierz statku "przyczpeia" go do kamery. Wrato przeanalizowac te linijke i zrozumiec jak to dziala.
glm::vec3 lightPos = glm::vec3(-4, 1, -4);
glm::vec3 lightPos = glm::vec3(2, 0, 2);
//glUniform3f(glGetUniformLocation(program, "light_dir"), 2, 1, 0);
glUniform3f(glGetUniformLocation(program, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glUseProgram(programSun);
glUniform3f(glGetUniformLocation(programSun, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glm::mat4 sunModelMatrix = glm::mat4(1.0f);
sunModelMatrix = glm::translate(sunModelMatrix, glm::vec3(0, 0, 0));
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);
glUseProgram(programTex);
glUniform3f(glGetUniformLocation(programTex, "lightPos"), lightPos.x, lightPos.y, lightPos.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));
drawObjectTexture(programTex, sphereContext, earth, glm::vec3(0.8f, 0.8f, 0.8f), earthTexture);
glUseProgram(0);
glutSwapBuffers();
@ -105,8 +150,13 @@ 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");
programSun = shaderLoader.CreateProgram("shaders/shader_4_sun.vert", "shaders/shader_4_sun.frag");
sphereModel = obj::loadModelFromFile("models/sphere.obj");
sphereContext.initFromOBJ(sphereModel);
sunTexture = Core::LoadTexture("textures/sun.png");
earthTexture = Core::LoadTexture("textures/earth2.png");
}
void shutdown()
@ -125,7 +175,7 @@ int main(int argc, char** argv)
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200, 300);
glutInitWindowSize(600, 600);
glutCreateWindow("OpenGL Pierwszy Program");
glutCreateWindow("GRK-PROJECT WIP");
glewInit();
init();

BIN
textures/earth2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 MiB

BIN
textures/sun.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 803 KiB

BIN
textures/sun.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 MiB