diff --git a/grk-project.vcxproj b/grk-project.vcxproj
index 77b2c3e..91089f1 100644
--- a/grk-project.vcxproj
+++ b/grk-project.vcxproj
@@ -11,6 +11,10 @@
+
+
+
+
@@ -40,7 +44,7 @@
{1B448102-E76C-4347-BDC7-40D02A567DB6}
Win32Proj
grk-cw9
- 10.0
+ 10.0.17134.0
grk-project
@@ -55,7 +59,7 @@
false
true
Unicode
- v142
+ v141
diff --git a/grk-project.vcxproj.filters b/grk-project.vcxproj.filters
index eddb12e..501b57a 100644
--- a/grk-project.vcxproj.filters
+++ b/grk-project.vcxproj.filters
@@ -30,6 +30,18 @@
Shader Files
+
+ Shader Files
+
+
+ Shader Files
+
+
+ Shader Files
+
+
+ Shader Files
+
diff --git a/shaders/shader_4_sun.frag b/shaders/shader_4_sun.frag
index 8df468e..8cd6be6 100644
--- a/shaders/shader_4_sun.frag
+++ b/shaders/shader_4_sun.frag
@@ -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);
}
diff --git a/shaders/shader_4_sun.vert b/shaders/shader_4_sun.vert
index 8a9db1b..d86f709 100644
--- a/shaders/shader_4_sun.vert
+++ b/shaders/shader_4_sun.vert
@@ -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;
}
diff --git a/shaders/shader_4_tex.frag b/shaders/shader_4_tex.frag
index 6837b8e..fb21d36 100644
--- a/shaders/shader_4_tex.frag
+++ b/shaders/shader_4_tex.frag
@@ -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;
}
diff --git a/shaders/shader_4_tex.vert b/shaders/shader_4_tex.vert
index 8a9db1b..efd1d17 100644
--- a/shaders/shader_4_tex.vert
+++ b/shaders/shader_4_tex.vert
@@ -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;
}
diff --git a/src/Texture.cpp b/src/Texture.cpp
index 27ce185..0a0927f 100644
--- a/src/Texture.cpp
+++ b/src/Texture.cpp
@@ -1,9 +1,5 @@
#include "Texture.h"
-#include
-#include
-#include
-#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 buffer((
std::istreambuf_iterator(input)),
(std::istreambuf_iterator()));
@@ -25,7 +27,6 @@ GLuint Core::LoadTexture( const char * filepath )
unsigned long w, h;
std::vector 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);
diff --git a/src/Texture.h b/src/Texture.h
index c5be313..107b37a 100644
--- a/src/Texture.h
+++ b/src/Texture.h
@@ -1,5 +1,10 @@
#pragma once
+#include
+#include
+#include
+#include
+#include "picopng.h"
#include "glew.h"
#include "freeglut.h"
diff --git a/src/main.cpp b/src/main.cpp
index c34bf34..e8a3c79 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -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 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();
diff --git a/textures/earth2.png b/textures/earth2.png
new file mode 100644
index 0000000..465f5d3
Binary files /dev/null and b/textures/earth2.png differ
diff --git a/textures/sun.jpg b/textures/sun.jpg
new file mode 100644
index 0000000..9d787d8
Binary files /dev/null and b/textures/sun.jpg differ
diff --git a/textures/sun.png b/textures/sun.png
new file mode 100644
index 0000000..7122608
Binary files /dev/null and b/textures/sun.png differ