diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..85a7f5b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.16 FATAL_ERROR) + +project(XtreemNodes) + +include_directories("include/") + +file(GLOB_RECURSE Sources "src/**.cpp") + +add_executable(${PROJECT_NAME} ${Sources}) + +if(UNIX AND NOT APPLE) # linux +target_link_libraries(${PROJECT_NAME} -lGL) +target_link_libraries(${PROJECT_NAME} -lGLEW) +target_link_libraries(${PROJECT_NAME} -lglfw) +#target_link_libraries(${PROJECT_NAME} -lsfml-opengl) +endif() + +if (WIN32) # windows +endif(WIN32) \ No newline at end of file diff --git a/GUI.cpp b/GUI.cpp deleted file mode 100644 index c22cb47..0000000 --- a/GUI.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "GUI.h" - -// Code contributed by Kacperks -// COMMENTED OUT BECAUSE IT DOESN'T WORK! -/* -Button::Button(sf::Image* normal, sf::Image* clicked, std::string words, Position2D location) { - this->normal.SetImage(*normal); - this->clicked.SetImage(*clicked); - this->currentSpr=&this->normal; - current =false; - this->normal.SetPosition(location); - this->clicked.SetPosition(location); - String.SetText(words); - String.SetPosition(location.x+3,location.y+3); - String.SetSize(14); -} -void Button::checkClick (sf::Vector2f mousePos) -{ - if (mousePos.x>currentSpr->GetPosition().x && mousePos.x<(currentSpr->GetPosition().x + currentSpr->GetSize().x)) { - if(mousePos.y>currentSpr->GetPosition().y && mousePos.y<(currentSpr->GetPosition().y + currentSpr->GetSize().y)) { - setState(!current); - } - } -} - -void Button::setState(bool which) -{ - current = which; - if (current) { - currentSpr=&clicked; - return; - } - currentSpr=&normal; -} -void Button::setText(std::string words) -{ - String.SetText(words); -} -bool Button::getVar() -{ - return current; -} -sf::Sprite* Button::getSprite() -{ - return currentSpr; -} - -sf::String * Button::getText() -{ - return &String; -}*/ diff --git a/XtreemNodes.depend b/XtreemNodes.depend deleted file mode 100644 index 820c498..0000000 --- a/XtreemNodes.depend +++ /dev/null @@ -1,134 +0,0 @@ -# depslib dependency file v1.0 -<<<<<<< HEAD -1672107417 source:c:\development\xtreemminer\main.cpp -======= -1672097148 source:c:\development\xtreemminer\main.cpp ->>>>>>> dcac2713f6b72f4d2252632a179b30a33fd827a8 - - - "Utilities.h" - "MapBlock.h" - "Base.h" - "NodeRenderer.h" - "TextureHandler.h" - - - - - -<<<<<<< HEAD -1672201712 c:\development\xtreemminer\include\noderenderer.h -======= -1667160597 c:\development\xtreemminer\include\noderenderer.h ->>>>>>> dcac2713f6b72f4d2252632a179b30a33fd827a8 - "Base.h" - "MapBlock.h" - - -1667164077 c:\development\xtreemminer\include\mapblock.h - "Base.h" - - - -1667065840 c:\development\xtreemminer\include\base.h - -1666531672 source:c:\development\xtreemminer\base.cpp - "MapBlock.h" - "Base.h" - -<<<<<<< HEAD -1672341127 c:\development\xtreemminer\include\texturehandler.h -======= -1672105046 c:\development\xtreemminer\include\texturehandler.h ->>>>>>> dcac2713f6b72f4d2252632a179b30a33fd827a8 - "stb_image.h" - "Base.h" - -1666445333 c:\development\xtreemminer\include\stb_image.h - "stb_image.h" - - - - - - - - - - - - - - - -1667065840 c:\development\xtreemminer\include\utilities.h - -1667075984 c:\development\xtreemminer\include\fastnoiselite.h - - -1667350805 source:c:\development\xtreemminer\mapblock.cpp - "MapBlock.h" - -1667294728 source:/home/kacperks/projects/XtreemNodes/MapBlock.cpp - "MapBlock.h" - -1667294728 /home/kacperks/projects/XtreemNodes/include/MapBlock.h - "Base.h" - - - -1667294728 /home/kacperks/projects/XtreemNodes/include/Base.h - -1667294728 source:/home/kacperks/projects/XtreemNodes/main.cpp - - - "Utilities.h" - "MapBlock.h" - "Base.h" - "NodeRenderer.h" - "TextureHandler.h" - - - "FastNoiseLite.h" - - - -1667294728 /home/kacperks/projects/XtreemNodes/include/Utilities.h - -1667294728 /home/kacperks/projects/XtreemNodes/include/NodeRenderer.h - "Base.h" - "MapBlock.h" - - -1667294728 /home/kacperks/projects/XtreemNodes/include/TextureHandler.h - "stb_image.h" - "Base.h" - -1667088826 /home/kacperks/projects/XtreemNodes/include/stb_image.h - "stb_image.h" - - - - - - - - - - - - - - - -1667294728 /home/kacperks/projects/XtreemNodes/include/FastNoiseLite.h - - -1667347897 source:c:\development\xtreemminer\gui.cpp - "GUI.h" - -1667333037 c:\development\xtreemminer\include\gui.h - "Base.h" - - - diff --git a/XtreemNodes.layout b/XtreemNodes.layout deleted file mode 100644 index bd1173a..0000000 --- a/XtreemNodes.layout +++ /dev/null @@ -1,114 +0,0 @@ - - - - -<<<<<<< HEAD - - - -======= - - - ->>>>>>> dcac2713f6b72f4d2252632a179b30a33fd827a8 - - - - - - - -<<<<<<< HEAD - - - - - - - - - - - - - - - - - - - - - -======= - ->>>>>>> dcac2713f6b72f4d2252632a179b30a33fd827a8 - - - - -<<<<<<< HEAD - -======= - - - - - - - - - - - - - - - - - - - - - ->>>>>>> dcac2713f6b72f4d2252632a179b30a33fd827a8 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/XtreemNodesLinux.cbp b/XtreemNodesLinux.cbp deleted file mode 100644 index b7c8c00..0000000 --- a/XtreemNodesLinux.cbp +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - diff --git a/XtreemNodesWindows.cbp b/XtreemNodesWindows.cbp deleted file mode 100644 index 1f7feae..0000000 --- a/XtreemNodesWindows.cbp +++ /dev/null @@ -1,64 +0,0 @@ - - - - - - diff --git a/XtreemNodesWindows.depend b/XtreemNodesWindows.depend deleted file mode 100644 index 09f58a1..0000000 --- a/XtreemNodesWindows.depend +++ /dev/null @@ -1,71 +0,0 @@ -# depslib dependency file v1.0 -1667347897 source:c:\development\xtreemminer\gui.cpp - "GUI.h" - -1667333037 c:\development\xtreemminer\include\gui.h - "Base.h" - - - -1667065840 c:\development\xtreemminer\include\base.h - -1672106999 source:c:\development\xtreemminer\main.cpp - - - "Utilities.h" - "MapBlock.h" - "Base.h" - "NodeRenderer.h" - "TextureHandler.h" - "LevelGenerator.h" - - - - - -1667065840 c:\development\xtreemminer\include\utilities.h - -1667164077 c:\development\xtreemminer\include\mapblock.h - "Base.h" - - - -1667160597 c:\development\xtreemminer\include\noderenderer.h - "Base.h" - "MapBlock.h" - - -1672105046 c:\development\xtreemminer\include\texturehandler.h - "stb_image.h" - "Base.h" - -1666445333 c:\development\xtreemminer\include\stb_image.h - "stb_image.h" - - - - - - - - - - - - - - - -1667350805 source:c:\development\xtreemminer\mapblock.cpp - "MapBlock.h" - -1672106616 source:c:\development\xtreemminer\levelgenerator.cpp - "LevelGenerator.h" - -1672106654 c:\development\xtreemminer\include\levelgenerator.h - "FastNoiseLite.h" - "MapBlock.h" - -1667075984 c:\development\xtreemminer\include\fastnoiselite.h - - diff --git a/XtreemNodesWindows.layout b/XtreemNodesWindows.layout deleted file mode 100644 index 9a0d84b..0000000 --- a/XtreemNodesWindows.layout +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/data/img/apple.png b/data/img/apple.png new file mode 100644 index 0000000..750557e Binary files /dev/null and b/data/img/apple.png differ diff --git a/data/img/awesomeface.png b/data/img/awesomeface.png new file mode 100644 index 0000000..23c778d Binary files /dev/null and b/data/img/awesomeface.png differ diff --git a/data/img/cobble.png b/data/img/cobble.png new file mode 100644 index 0000000..0faee33 Binary files /dev/null and b/data/img/cobble.png differ diff --git a/data/img/gold.png b/data/img/gold.png new file mode 100644 index 0000000..ca5853b Binary files /dev/null and b/data/img/gold.png differ diff --git a/data/img/grass.png b/data/img/grass.png index ce4c33f..84f83a2 100644 Binary files a/data/img/grass.png and b/data/img/grass.png differ diff --git a/data/img/grass_side.png b/data/img/grass_side.png new file mode 100644 index 0000000..453c64d Binary files /dev/null and b/data/img/grass_side.png differ diff --git a/data/img/iron.png b/data/img/iron.png new file mode 100644 index 0000000..e3e6e84 Binary files /dev/null and b/data/img/iron.png differ diff --git a/data/img/maple.png b/data/img/maple.png new file mode 100644 index 0000000..f6c6f9d Binary files /dev/null and b/data/img/maple.png differ diff --git a/data/img/oak.png b/data/img/oak.png new file mode 100644 index 0000000..76da003 Binary files /dev/null and b/data/img/oak.png differ diff --git a/data/img/test.png b/data/img/test.png new file mode 100644 index 0000000..8ace58b Binary files /dev/null and b/data/img/test.png differ diff --git a/data/img/texturemap.png b/data/img/texturemap.png deleted file mode 100644 index b9facd7..0000000 Binary files a/data/img/texturemap.png and /dev/null differ diff --git a/data/shader/MeshF.shader b/data/shader/MeshF.shader new file mode 100644 index 0000000..796a246 --- /dev/null +++ b/data/shader/MeshF.shader @@ -0,0 +1,159 @@ +#version 330 core + +in vec3 Normal; +in vec2 TexCoords; +in vec3 FragPosition; + +out vec4 FragColor; + +#define PI 3.14159265359 +#define MAX_LIGHTS 128 + +struct Material { + float Ao; + vec3 Albedo; + float Metallic; + float Roughness; +}; + +struct PointLight { + vec3 Color; + vec3 Position; + float Intensity; +}; + +struct DirectLight { + vec3 Color; + vec3 Position; + vec3 Direction; + float Intensity; +}; + +struct SpotLight { + vec3 Color; + vec3 Position; + vec3 Direction; + float ICutOff; + float OCutOff; + float Intensity; +}; + +// uniforms +uniform sampler2D AoMap; +uniform sampler2D AlbedoMap; +uniform sampler2D NormalMap; +uniform sampler2D MetallicMap; +uniform sampler2D RoughnessMap; + +uniform vec3 CameraPos; +uniform Material material; + +uniform int spotLightCount; +uniform SpotLight spotLights[MAX_LIGHTS]; + +uniform int pointLightCount; +uniform PointLight pointLights[MAX_LIGHTS]; + +uniform int directLightCount; +uniform DirectLight directLights[MAX_LIGHTS]; + +float DistributionGGX(vec3 N, vec3 H, float roughness) { + float a = roughness * roughness; + float a2 = a * a; + float NdotH = max(dot(N, H), 0.0); + float NdotH2 = NdotH * NdotH; + + float nom = a2; + float denom = (NdotH2 * (a2 - 1.0) + 1.0); + denom = PI * denom * denom; + return nom / max(denom, 0.001); // prevent divide by zero for roughness=0.0 and NdotH=1.0 +} + +float GeometrySchlickGGX(float NdotV, float roughness) { + float r = (roughness + 1.0); + float k = (r * r) / 8.0; + + float nom = NdotV; + float denom = NdotV * (1.0 - k) + k; + + return nom / denom; +} + +float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) { + float NdotV = max(dot(N, V), 0.0); + float NdotL = max(dot(N, L), 0.0); + float ggx2 = GeometrySchlickGGX(NdotV, roughness); + float ggx1 = GeometrySchlickGGX(NdotL, roughness); + + return ggx1 * ggx2; +} + +vec3 fresnelSchlick(float cosTheta, vec3 F0) { + return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0); +} + +vec3 ProcessLight(vec3 N, vec3 V, vec3 Ld, vec3 color, vec3 F0, float I, float roughness, float metallic, vec3 albedo) { + + vec3 L = normalize(Ld); + vec3 H = normalize(V + L); + float distance = length(Ld); + float attenuation = I / (distance * distance); + vec3 radiance = color * attenuation; + + // Cook-Torrance BRDF + float NDF = DistributionGGX(N, H, roughness); + float G = GeometrySmith(N, V, L, roughness); + vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0); + + vec3 nominator = NDF * G * F; + float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0); + vec3 specular = nominator / max(denominator, 0.001); + + vec3 KS = F; + vec3 KD = vec3(1.0) - KS; + KD *= 1.0 - metallic; + float NdotL = max(dot(N, L), 0.0); + vec3 Lo = (KD * albedo / PI + specular) * radiance * NdotL; + return Lo; +} + +void main() { + vec3 N = normalize(Normal); + vec3 V = normalize(CameraPos - FragPosition); + + float ao = material.Ao + texture(AoMap, TexCoords).r; + vec3 albedo = pow(material.Albedo, vec3(2.2)) + pow(vec3(texture(AlbedoMap, TexCoords)), vec3(5.2)); + float metallic = material.Metallic + texture(MetallicMap, TexCoords).r; + float roughness = material.Roughness + texture(RoughnessMap, TexCoords).r; + + vec3 F0 = vec3(0.04); + F0 = mix(F0, albedo, metallic); + + vec3 Luminance = vec3(0.0); + + // Point lights + int count = (pointLightCount > MAX_LIGHTS) ? MAX_LIGHTS : pointLightCount; + for (int i = 0; i < count; i++) { + PointLight light = pointLights[i]; + vec3 L = (light.Position - FragPosition); + Luminance += ProcessLight(N, V, L, light.Color, F0, light.Intensity, roughness, metallic, albedo); + } + + // Directional lights + count = (directLightCount > MAX_LIGHTS) ? MAX_LIGHTS : directLightCount; + for (int i = 0; i < count; i++) { + DirectLight light = directLights[i]; + Luminance += ProcessLight(N, V, light.Direction, light.Color, F0, light.Intensity, roughness, metallic, albedo); + } + + // ambient + vec3 ambient = vec3(0.1f) * albedo * ao; + + vec3 color = ambient + Luminance; + // HDR correction + color = color / (color + vec3(1.0)); + // Gamma correction + color = pow(color, vec3(1.0 / 3.0)); + + FragColor = vec4(color, 1.0); +} \ No newline at end of file diff --git a/data/shader/MeshV.shader b/data/shader/MeshV.shader new file mode 100644 index 0000000..107706e --- /dev/null +++ b/data/shader/MeshV.shader @@ -0,0 +1,21 @@ +#version 330 core +#extension GL_ARB_explicit_attrib_location : enable + +layout(location = 0) in vec3 aPos; +layout(location = 1) in vec3 aNormal; +layout(location = 2) in vec2 aTexCoords; + +out vec3 Normal; +out vec2 TexCoords; +out vec3 FragPosition; + +uniform mat4 View; +uniform mat4 Model; +uniform mat4 Projection; + +void main() { + TexCoords = aTexCoords; + FragPosition = vec3(Model * vec4(aPos, 1.0)); + Normal = transpose(inverse(mat3(Model))) * aNormal; + gl_Position = Projection * View * vec4(FragPosition, 1.0); +} diff --git a/include/GUI.h b/include/GUI.h deleted file mode 100644 index 4d49831..0000000 --- a/include/GUI.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef GUI -#define GUI -#include "Base.h" -#include -#include - -class Button -{ - public: - Button(sf::Image* normal, sf::Image* clicked, std::string text, Position2D location); - void checkClick(Position2D); - void setState(bool state); - void setText(std::string); - bool getVar(); - sf::Sprite* getSprite(); - sf::String* getText(); - - private: - sf::Sprite normal; - sf::Sprite clicked; - sf::Sprite* currentSpr; - sf::String String; - bool current; -}; - -#endif diff --git a/include/NodeRenderer.h b/include/NodeRenderer.h deleted file mode 100644 index cf3f259..0000000 --- a/include/NodeRenderer.h +++ /dev/null @@ -1,144 +0,0 @@ -#ifndef NODERENDERER_H -#define NODERENDERER_H -#include "Base.h" -#include "MapBlock.h" -#include - -class NodeRenderer -{ - public: - NodeRenderer() - { - - } - - virtual ~NodeRenderer() - { - - } - - int renderNode(int x, int y, int z) - { - Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z); // The block the node at (x, y, z) is in - - glColor3f(1.0F, 1.0F, 1.0F); - - glBegin(GL_QUADS); - // Front - if(blockManager.isAir(x, y, z - 1)) - { - glTexCoord2f(.0F, .0F); - glVertex3f(x + .0F, y + 1.0F, z + .0F); - - glTexCoord2f(1.0F, .0F); - glVertex3f(x + 1.0F, y + 1.0F, z + .0F); - - glColor3f(.6F, .6F, .6F); // Bottom vertices - glTexCoord2f(1.0F, 1.0F); - glVertex3f(x + 1.0F, y + .0F, z + .0F); - - glTexCoord2f(.0F, 1.0F); - glVertex3f(x + .0F, y + .0F, z + .0F); - glColor3f(1.0F, 1.0F, 1.0F); - } - - // Back - if(blockManager.isAir(x, y, z + 1)) - { - - glTexCoord2f(.0F, .0F); - glVertex3f(x + .0F, y + 1.0F, z + 1.0F); - - glTexCoord2f(1.0F, .0F); - glVertex3f(x + 1.0F, y + 1.0F, z + 1.0F); - - glColor3f(.6F, .6F, .6F); - glTexCoord2f(1.0F, 1.0F); - glVertex3f(x + 1.0F, y + .0F, z + 1.0F); - - glTexCoord2f(.0F, 1.0F); - glVertex3f(x + .0F, y + .0F, z + 1.0F); - glColor3f(1.0F, 1.0F, 1.0F); - } - - // Right - if(blockManager.isAir(x + 1, y, z)) - { - glTexCoord2f(1.0F, .0F); - glVertex3f(x + 1.0F, y + 1.0F, z + .0F); - - glColor3f(.6F, .6F, .6F); - glTexCoord2f(1.0F, 1.0F); - glVertex3f(x + 1.0F, y + .0F, z + .0F); - - glTexCoord2f(.0F, 1.0F); - glVertex3f(x + 1.0F, y + .0F, z + 1.0F); - glColor3f(1.0F, 1.0F, 1.0F); - - glTexCoord2f(.0F, .0F); - glVertex3f(x + 1.0F, y + 1.0F, z + 1.0F); - } - - // Left - if(blockManager.isAir(x - 1, y, z)) - { - glTexCoord2f(1.0F, .0F); - glVertex3f(x + .0F, y + 1.0F, z + .0F); - - glColor3f(.6F, .6F, .6F); - glTexCoord2f(1.0F, 1.0F); - glVertex3f(x + .0F, y + .0F, z + .0F); - - glTexCoord2f(.0F, 1.0F); - glVertex3f(x + .0F, y + .0F, z + 1.0F); - glColor3f(1.0F, 1.0F, 1.0F); - - glTexCoord2f(.0F, .0F); - glVertex3f(x + .0F, y + 1.0F, z + 1.0F); - } - - // Bottom - //printf("\n\nx: %i, y: %i, z: %i, VALUE: %s", x, y, z, blockManager.isAir(x, y - 1, z) ? "true" : "false"); - if(blockManager.isAir(x, y - 1, z)) - { - //printf("\nWUT? x: %i, y: %i, z: %i, VALUE: %s", x, y, z, blockManager.isAir(x, y - 1, z) ? "true" : "false"); - glTexCoord2f(.0F, .0F); - glVertex3f(x + 1.0F, y + .0F, z + .0F); - - glTexCoord2f(1.0F, .0F); - glVertex3f(x + .0F, y + .0F, z + .0F); - - glTexCoord2f(1.0F, 1.0F); - glVertex3f(x + .0F, y + .0F, z + 1.0F); - - glTexCoord2f(.0F, 1.0F); - glVertex3f(x + 1.0F, y + .0F, z + 1.0F); - } - - // Top - if(blockManager.isAir(x, y + 1, z)) - { - glTexCoord2f(.0F, .0F); - glVertex3f(x + 1.0F, y + 1.0F, z + .0F); - - glTexCoord2f(1.0F, .0F); - glVertex3f(x + .0F, y + 1.0F, z + .0F); - - glTexCoord2f(1.0F, 1.0F); - glVertex3f(x + .0F, y + 1.0F, z + 1.0F); - - glTexCoord2f(.0F, 1.0F); - glVertex3f(x + 1.0F, y + 1.0F, z + 1.0F); - } - - glEnd(); - return 1; - } - - protected: - - private: - -}; - -#endif diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 37de996..0000000 --- a/main.cpp +++ /dev/null @@ -1,196 +0,0 @@ -#include -#include -#include "Utilities.h" -#include "MapBlock.h" -#include "Base.h" -#include "NodeRenderer.h" -#include "TextureHandler.h" -#include -#include -#include -#include - -NodeRenderer renderer; - -BlockManager blockManager; -TextureHandler textureHandler; - -GLfloat playerX = 0; -GLfloat playerY = -30; -GLfloat playerZ = -100; -GLfloat playerRotX = 0; -GLfloat playerRotY = 0; - - - -void display() -{ - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glLoadIdentity(); - - - glRotatef(playerRotX, 1.0F, .0F, .0F); - glRotatef(playerRotY, .0F, 1.0F, .0F); - glTranslatef(playerX, playerY, playerZ); - //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - - - glBegin(GL_QUADS); - - - for(int x = 0; x < 100; x++) - { - for(int y = 0; y < 64; y++) - { - for(int z = 0; z < 100; z++) - { - if(blockManager.getNodeAt(x, y, z) > 0) - { - textureHandler.getTextureForNode(x, y, z); - renderer.renderNode(x, y, z); - } - } - } - } - - glEnd(); - - glFlush(); - glutSwapBuffers(); -/* - sf::Vector2i lastMousePos = sf::Vector2i(0, 0); - const sf::Window& window = nullptr; - sf::Vector2i mouseDelta = sf::Mouse::getPosition(window) - lastMousePos; - lastMousePos = sf::Mouse::getPosition(window);*/ - //glutPostRedisplay(); - -} - - -void reshape(int width, int height) -{ - glMatrixMode(GL_PROJECTION); - - glLoadIdentity(); - gluPerspective(30, width / (float) height, 5, 512.0F); - glViewport(0, 0, width, height); - - glMatrixMode(GL_MODELVIEW); - glutPostRedisplay(); -} - -void updateTimer(int time) -{ - // Movement - if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) - { - playerZ += .8; - } - - else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) - { - playerZ -= .8; - } - - if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) - { - playerX += .8; - } - - else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) - { - playerX -= .8; - } - - if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) - { - playerY += .8; - } - - else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) - { - playerY -= .8; - } - - // Rotation - if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) - { - playerRotY -= 1.8; - } - - else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) - { - playerRotY += 1.8; - } - - if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) - { - playerRotX -= 1.8; - } - - else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) - { - playerRotX += 1.8; - } - - if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) - { - exit(0); - } - - glutPostRedisplay(); - glutTimerFunc(30, &updateTimer, 0); -} - -int main(int argc, char **argv) -{ - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); - glutInitWindowSize(800, 600); - glutCreateWindow("XtreemNodes Engine - By MCL Software and Cube Software"); - - glClearColor(.4, .7, .8 , 255); - glEnable(GL_DEPTH_TEST); - glEnable(GL_TEXTURE_2D); - //glEnable(GL_CULL_FACE); - //glCullFace(GL_FRONT); - //glFrontFace(GL_CCW); - - // Load textures - textureHandler.loadAllTextures(); - - - - - - - -/* - for(int y = 0; y < 16; y++) - { - for(int x = 0; x < 16; x++) - { - for(int z = 0; z < 16; z++) - { - Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z); - printf("\n|x: %i |y: %i | z: %i | id: %i", x, y, z, blockManager.getNodeAt(x, y, z)); - - } - } - } - -*/ - - - - - updateTimer(0); - glutDisplayFunc(&display); - glutReshapeFunc(&reshape); - - glutMainLoop(); - - return 0; -} diff --git a/include/Base.h b/src/Base.h similarity index 81% rename from include/Base.h rename to src/Base.h index 20921d7..719c7cd 100644 --- a/include/Base.h +++ b/src/Base.h @@ -1,6 +1,11 @@ +#pragma once #ifndef BASE #define BASE +#include + +#include + //#include "NodeRenderer.h" //#include "MapBlock.h" diff --git a/src/Camera.h b/src/Camera.h new file mode 100644 index 0000000..b8c135b --- /dev/null +++ b/src/Camera.h @@ -0,0 +1,113 @@ +#ifndef CAMERA_H +#define CAMERA_H + +#include +#include +#include + +#include + +// Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods +enum Camera_Movement { + FORWARD, + BACKWARD, + LEFT, + RIGHT +}; + +const float YAW = -90.0f; +const float PITCH = 0.0f; +const float SPEED = 2.5f; +const float SENSITIVITY = 0.1f; +const float ZOOM = 45.0f; + + +class Camera +{ +public: + glm::vec3 Position; + glm::vec3 Front; + glm::vec3 Up; + glm::vec3 Right; + glm::vec3 WorldUp; + float Yaw; + float Pitch; + float MovementSpeed; + float MouseSensitivity; + float Zoom; + + Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM) + { + Position = position; + WorldUp = up; + Yaw = yaw; + Pitch = pitch; + updateCameraVectors(); + } + Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM) + { + Position = glm::vec3(posX, posY, posZ); + WorldUp = glm::vec3(upX, upY, upZ); + Yaw = yaw; + Pitch = pitch; + updateCameraVectors(); + } + + glm::mat4 GetViewMatrix() + { + return glm::lookAt(Position, Position + Front, Up); + } + + void ProcessKeyboard(Camera_Movement direction, float deltaTime) + { + float velocity = MovementSpeed * deltaTime; + if (direction == FORWARD) + Position += Front * velocity; + if (direction == BACKWARD) + Position -= Front * velocity; + if (direction == LEFT) + Position -= Right * velocity; + if (direction == RIGHT) + Position += Right * velocity; + } + + void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true) + { + xoffset *= MouseSensitivity; + yoffset *= MouseSensitivity; + + Yaw += xoffset; + Pitch += yoffset; + + if (constrainPitch) + { + if (Pitch > 89.0f) + Pitch = 89.0f; + if (Pitch < -89.0f) + Pitch = -89.0f; + } + + updateCameraVectors(); + } + + void ProcessMouseScroll(float yoffset) + { + Zoom -= (float)yoffset; + if (Zoom < 1.0f) + Zoom = 1.0f; + if (Zoom > 45.0f) + Zoom = 45.0f; + } + +private: + void updateCameraVectors() + { + glm::vec3 front; + front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch)); + front.y = sin(glm::radians(Pitch)); + front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch)); + Front = glm::normalize(front); + Up = glm::normalize(glm::cross(Right, Front)); + } +}; +#endif \ No newline at end of file diff --git a/include/FastNoiseLite.h b/src/FastNoiseLite.h similarity index 100% rename from include/FastNoiseLite.h rename to src/FastNoiseLite.h diff --git a/src/GLMaterial.h b/src/GLMaterial.h new file mode 100644 index 0000000..18989dc --- /dev/null +++ b/src/GLMaterial.h @@ -0,0 +1,42 @@ +#ifndef MATERIAL +#define MATERIAL + +#include "GLShader.h" + +struct Material { + Material() = default; + ~Material() = default; + Material(glm::vec3 albedo, GLfloat metallic, GLfloat roughness, GLfloat ao): + Albedo(albedo), Metallic(metallic), Roughness(roughness), Ao(ao) { } + + float Ao = 0.1f; + float Opacity = 1.0f; + float Metallic = 0.4f; + float Roughness = 0.2f; + float Shininess = 1.0f; + float Reflectivity = 0.0f; + glm::vec3 Albedo = glm::vec3(0.6f); + + GLuint AoMap = 0; + GLuint NormalMap = 0; + GLuint AlbedoMap = 0; + GLuint MetallicMap = 0; + GLuint RoughnessMap = 0; + GLuint SpecularBRDFMap = 0; + + void SetUniform(Shader& shader) { + shader.bind(); + shader.setuniform("material.Ao", Ao); + shader.setuniform("material.Albedo", Albedo); + shader.setuniform("material.Metallic", Metallic); + shader.setuniform("material.Roughness", Roughness); + + shader.setuniform("AoMap", AoMap, 0); + shader.setuniform("AlbedoMap", AlbedoMap, 1); + shader.setuniform("NormalMap", NormalMap, 2); + shader.setuniform("MetallicMap", MetallicMap, 3); + shader.setuniform("RoughnessMap", RoughnessMap, 4); + } +}; + +#endif \ No newline at end of file diff --git a/src/GLMesh.h b/src/GLMesh.h new file mode 100644 index 0000000..a728290 --- /dev/null +++ b/src/GLMesh.h @@ -0,0 +1,53 @@ +#ifndef GLMESH_H +#define GLMESH_H + +#include "GLVertexArray.h" +#include "GLMaterial.h" + +class GLMesh { + +public: + GLMesh() = default; + ~GLMesh() = default; + + GLMesh(Vertex* vertices, GLsizei vSize) { + vao = VertexArray(vertices, vSize); + } + + GLMesh(Vertex* vertices, GLsizei vSize, GLuint* indices, GLsizei iSize) { + vao = VertexArray(vertices, vSize, indices, iSize); + } + + GLMesh(Vertex* vertices, GLsizei vSize, GLuint* indices, GLsizei iSize, const Material& matrial): + material(matrial) { + vao = VertexArray(vertices, vSize, indices, iSize); + } + + GLMesh(const VertexArray& vao): vao(vao) {} + + void SetMaterial(const Material& matrial) { + material = matrial; + } + + Material& GetMaterial() { + return material; + } + + void Render(Shader& shader, GLenum mode = GL_TRIANGLE_STRIP) { + material.SetUniform(shader); + shader.bind(); + vao.DrawElements(mode); + } + + void DrawArrays(Shader& shader, GLenum mode = GL_TRIANGLES) { + material.SetUniform(shader); + shader.bind(); + vao.DrawArrays(mode); + } + +private: + VertexArray vao; + Material material; +}; + +#endif \ No newline at end of file diff --git a/src/GLShader.cpp b/src/GLShader.cpp new file mode 100644 index 0000000..4cd476a --- /dev/null +++ b/src/GLShader.cpp @@ -0,0 +1,115 @@ +#include "GLShader.h" +#include "Base.h" +#include +#include +#include +Shader::Shader(const char* vertexPath, const char* fragmentPath) +{ + // 1. retrieve the vertex/fragment source code from filePath + std::string vertexCode; + std::string fragmentCode; + std::ifstream vShaderFile; + std::ifstream fShaderFile; + // ensure ifstream objects can throw exceptions: + vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); + fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); + try + { + // open files + vShaderFile.open(vertexPath); + fShaderFile.open(fragmentPath); + std::stringstream vShaderStream, fShaderStream; + // read file's buffer contents into streams + vShaderStream << vShaderFile.rdbuf(); + fShaderStream << fShaderFile.rdbuf(); + // close file handlers + vShaderFile.close(); + fShaderFile.close(); + // convert stream into string + vertexCode = vShaderStream.str(); + fragmentCode = fShaderStream.str(); + } + catch (std::ifstream::failure& e) + { + std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl; + } + const char* vShaderCode = vertexCode.c_str(); + const char * fShaderCode = fragmentCode.c_str(); + // 2. compile shaders + unsigned int vertex, fragment; + // vertex shader + vertex = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex, 1, &vShaderCode, NULL); + glCompileShader(vertex); + checkCompileErrors(vertex, "VERTEX"); + // fragment Shader + fragment = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment, 1, &fShaderCode, NULL); + glCompileShader(fragment); + checkCompileErrors(fragment, "FRAGMENT"); + // shader Program + program = glCreateProgram(); + glAttachShader(program, vertex); + glAttachShader(program, fragment); + glLinkProgram(program); + checkCompileErrors(program, "PROGRAM"); + // delete the shaders as they're linked into our program now and no longer necessery + glDeleteShader(vertex); + glDeleteShader(fragment); + +} + +void Shader::bind(){ + GLCALL(glUseProgram(program)); +} + +void Shader::unbind(){ + GLCALL(glUseProgram(0)); +} + +void Shader::setuniform(const GLchar* uName, unsigned int value){ + GLCALL(glUniform1i(glGetUniformLocation(program, uName), value)); +} + + +void Shader::setuniform(const GLchar* uName, int value){ + GLCALL(glUniform1i(glGetUniformLocation(program, uName), value)); +} + +void Shader::setuniform(const GLchar* uName, GLfloat value){ + GLCALL(glUniform1i(glGetUniformLocation(program, uName), value)); +} + +void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y){ + GLCALL(glUniform2f(glGetUniformLocation(program, uName), x, y)); +} + +void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z){ + GLCALL(glUniform3f(glGetUniformLocation(program, uName), x, y, z)); +} + +void Shader::setuniform(const GLchar* uName, glm::vec3 vector){ + GLCALL(glUniform3f(glGetUniformLocation(program, uName), vector.x, vector.y, vector.z)); +} + +void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z, GLfloat w){ + GLCALL(glUniform4f(glGetUniformLocation(program, uName), x, y, z, w)); +} + +void Shader::setuniform(const GLchar* name, const glm::mat4 &mat) +{ + glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_FALSE, &mat[0][0]); +} + +void Shader::setuniform(const GLchar* uName, glm::vec2 vector){ + GLCALL(glUniform2f(glGetUniformLocation(program, uName), vector.x, vector.y)); +} + +void Shader::setuniform(const GLchar* uName, glm::vec4 vector){ + GLCALL(glUniform4f(glGetUniformLocation(program, uName), vector.x, vector.y, vector.z, vector.w)); +} +void Shader::setuniform(const GLchar* uName, GLuint tex2d, GLint unit){ // sample 2d + GLCALL(glActiveTexture(GL_TEXTURE0 + unit)); + GLCALL(glBindTexture(GL_TEXTURE_2D, tex2d)); + GLCALL(glUniform1i(glGetUniformLocation(program, uName), unit)); +} \ No newline at end of file diff --git a/src/GLShader.h b/src/GLShader.h new file mode 100644 index 0000000..d60e4d3 --- /dev/null +++ b/src/GLShader.h @@ -0,0 +1,65 @@ +#ifndef SHADER_H +#define SHADER_H + +#include +#include + +#include + + +#define GLCALL(func) func + +class Shader { + public: + Shader() : program(0) {} + Shader(const GLuint id) : program(id) {} + Shader(const char* vertexPath, const char* fragmentPath); + ~Shader() { glDeleteProgram(program); } + + void bind(); + void unbind(); + void setuniform(const GLchar* uName, unsigned int value); + void setuniform(const GLchar* uName, int value); + void setuniform(const GLchar* uName, GLfloat value); + void setuniform(const GLchar* uName, GLfloat x, GLfloat y); + void setuniform(const GLchar* uName, glm::vec2 vector); + void setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z); + void setuniform(const GLchar* uName, glm::vec3 vector); + void setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z, GLfloat w); + void setuniform(const GLchar* uName, glm::vec4 vector); + void setuniform(const GLchar* uName, const glm::mat4& mtx); + void setuniform(const GLchar* uName, GLuint tex2d, GLint unit); // sample 2d + + GLuint getuniform(const char* name); + GLuint GetProgram() { return program; } + + private: + + void checkCompileErrors(GLuint shader, std::string type) + { + GLint success; + GLchar infoLog[1024]; + if(type != "PROGRAM") + { + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if(!success) + { + glGetShaderInfoLog(shader, 1024, NULL, infoLog); + std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl; + } + } + else + { + glGetProgramiv(shader, GL_LINK_STATUS, &success); + if(!success) + { + glGetProgramInfoLog(shader, 1024, NULL, infoLog); + std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl; + } + } + } + + GLuint program; +}; + + #endif \ No newline at end of file diff --git a/src/GLVertex.h b/src/GLVertex.h new file mode 100644 index 0000000..025341a --- /dev/null +++ b/src/GLVertex.h @@ -0,0 +1,19 @@ +#ifndef GLVERTEX_H +#define GLVERTEX_H + +struct Vertex { + Vertex() = default; + Vertex(float x, float y, float z): Position(glm::vec3(x, y, z)){} + Vertex(glm::vec3 position): Position(position), Normal(0), TexCoords(0) { } + Vertex(glm::vec3 position, glm::vec3 normal): Position(position), Normal(normal), TexCoords(0) { } + Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texcoord): Position(position), Normal(normal), TexCoords(texcoord) { } + + // position + glm::vec3 Position = glm::vec3(0.0f); + // normal + glm::vec3 Normal = glm::vec3(0.0f); + // coords + glm::vec2 TexCoords = glm::vec2(0.0f); +}; + +#endif \ No newline at end of file diff --git a/src/GLVertexArray.h b/src/GLVertexArray.h new file mode 100644 index 0000000..73adf81 --- /dev/null +++ b/src/GLVertexArray.h @@ -0,0 +1,70 @@ +#ifndef GLVERTEXARRAY_H +#define GLVERTEXARRAY_H + +#include "GLVertex.h" + +class VertexArray { +public: + ~VertexArray() = default; + + VertexArray(): VAO(0), EBO(0), VBO(0), numOfIndices(0), + numOfVertices(0) {} + + VertexArray(Vertex* vertices, GLsizei vSize, GLuint* indices=nullptr, GLsizei iSize=0): + numOfIndices(iSize), numOfVertices(vSize){ + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + glGenBuffers(1, &EBO); + + glBindVertexArray(VAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, numOfVertices * sizeof(Vertex), vertices, GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, numOfIndices * sizeof(GLuint), indices, GL_STATIC_DRAW); + + // Positions + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0); + // Normals + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Normal)); + // Texcoords + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, TexCoords)); + + glBindVertexArray(0); + numOfVertices /= sizeof(Vertex); + } + + void PushAttrib(GLuint index, GLint size, GLsizei stride, GLvoid* ptr) { + glEnableVertexAttribArray(index); + glVertexAttribPointer(index, size, GL_FLOAT, GL_FALSE, size, ptr); + } + + void Bind() { + GLCALL(glBindVertexArray(VAO)); + } + + void UnBind() { + GLCALL(glBindVertexArray(0)); + } + + void DrawElements(GLenum mode = GL_TRIANGLE_STRIP) { + GLCALL(glBindVertexArray(VAO)); + GLCALL(glDrawElements(mode, numOfIndices, GL_UNSIGNED_INT, 0)); + GLCALL(glBindVertexArray(0)); + } + + void DrawArrays(GLenum mode = GL_TRIANGLES) { + GLCALL(glBindVertexArray(VAO)); + GLCALL(glDrawArrays(mode, 0, numOfVertices)); + GLCALL(glBindVertexArray(0)); + } + +private: + GLuint VAO, VBO, EBO; + GLsizei numOfIndices, numOfVertices; +}; + +#endif \ No newline at end of file diff --git a/LevelGenerator.cpp b/src/LevelGenerator.cpp similarity index 64% rename from LevelGenerator.cpp rename to src/LevelGenerator.cpp index c8084b8..9c38092 100644 --- a/LevelGenerator.cpp +++ b/src/LevelGenerator.cpp @@ -1,23 +1,24 @@ +/*#include "MapBlock.h" +#include "FastNoiseLite.h" #include "LevelGenerator.h" -FastNoiseLite perlin, os, cellular; +//FastNoiseLite perlin, os, cellular; int seed = 138; -perlin.SetSeed(seed); -perlin.SetNoiseType(FastNoiseLite::NoiseType_Perlin); -perlin.SetFrequency(.01F); - -os.SetSeed(seed); -os.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2); -os.SetFrequency(.01F); - -cellular.SetSeed(seed); -cellular.SetNoiseType(FastNoiseLite::NoiseType_Cellular); -cellular.SetFrequency(.1F); LevelGenerator::LevelGenerator() { + perlin.SetSeed(seed); + perlin.SetNoiseType(FastNoiseLite::NoiseType_Perlin); + perlin.SetFrequency(.01F); + os.SetSeed(seed); + os.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2); + os.SetFrequency(.01F); + + cellular.SetSeed(seed); + cellular.SetNoiseType(FastNoiseLite::NoiseType_Cellular); + cellular.SetFrequency(.1F); } void LevelGenerator::generateBlock() @@ -41,3 +42,4 @@ void LevelGenerator::generateBlock() } } } +*/ \ No newline at end of file diff --git a/include/LevelGenerator.h b/src/LevelGenerator.h similarity index 90% rename from include/LevelGenerator.h rename to src/LevelGenerator.h index 8f64374..bbea41d 100644 --- a/include/LevelGenerator.h +++ b/src/LevelGenerator.h @@ -1,4 +1,4 @@ -#ifndef LEVELGENERATOR +/*#ifndef LEVELGENERATOR #define LEVELGENERATOR #include "FastNoiseLite.h" @@ -16,3 +16,4 @@ class LevelGenerator }; #endif +*/ \ No newline at end of file diff --git a/include/Logger.h b/src/Logger.h similarity index 100% rename from include/Logger.h rename to src/Logger.h diff --git a/MapBlock.cpp b/src/MapBlock.cpp similarity index 97% rename from MapBlock.cpp rename to src/MapBlock.cpp index d264165..d3594e9 100644 --- a/MapBlock.cpp +++ b/src/MapBlock.cpp @@ -1,4 +1,4 @@ -#include "MapBlock.h" +/*#include "MapBlock.h" @@ -65,3 +65,4 @@ Position2D BlockUtilities::getBlockFromNodeCoordinates(int x, int z) pos2d.z = floor(z / 16); return pos2d; } +*/ \ No newline at end of file diff --git a/include/MapBlock.h b/src/MapBlock.h similarity index 96% rename from include/MapBlock.h rename to src/MapBlock.h index 4ac6a78..a3b3851 100644 --- a/include/MapBlock.h +++ b/src/MapBlock.h @@ -1,4 +1,4 @@ -#ifndef MAPBLOCK +/*#ifndef MAPBLOCK #define MAPBLOCK #include "Base.h" #include @@ -31,3 +31,4 @@ class BlockUtilities }; #endif +*/ \ No newline at end of file diff --git a/src/NodeRenderer.h b/src/NodeRenderer.h new file mode 100644 index 0000000..7f52e24 --- /dev/null +++ b/src/NodeRenderer.h @@ -0,0 +1,34 @@ +#ifndef NODERENDERER_H +#define NODERENDERER_H +#include "Base.h" +#include "MapBlock.h" + +class NodeRenderer +{ + public: + NodeRenderer() + { + + } + + virtual ~NodeRenderer() + { + + } + + int renderNode(int x, int y, int z) + { + //Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z); // The block the node at (x, y, z) is in + + // render block + + return 1; + } + + protected: + + private: + +}; + +#endif diff --git a/include/Nodes.h b/src/Nodes.h similarity index 100% rename from include/Nodes.h rename to src/Nodes.h diff --git a/include/TextureHandler.h b/src/TextureHandler.h similarity index 89% rename from include/TextureHandler.h rename to src/TextureHandler.h index 01ba231..c108126 100644 --- a/include/TextureHandler.h +++ b/src/TextureHandler.h @@ -4,8 +4,8 @@ #include "Base.h" -#ifndef TEXTURE_HANDLER -#define TEXTURE_HANDLER +//#ifndef TEXTURE_HANDLER +//#define TEXTURE_HANDLER class TextureHandler { private: @@ -24,7 +24,7 @@ class TextureHandler } void loadAllTextures() - { + {/* int textureIndex = 0; imageData = loadTexture("data/img/texturemap.png"); @@ -39,7 +39,6 @@ class TextureHandler glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData); - imageData1 = loadTexture("data/img/oak.png"); glGenTextures(1, &textures1); glBindTexture(GL_TEXTURE_2D, textures1); @@ -48,7 +47,7 @@ class TextureHandler glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData1); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData1);*/ } void getTextureForNode(int x, int y, int z) @@ -56,16 +55,16 @@ class TextureHandler //Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z); if(blockManager.getNodeAt(x, y, z) == 1) { - glBindTexture(GL_TEXTURE_2D, textures); + //glBindTexture(GL_TEXTURE_2D, textures); } else if(blockManager.getNodeAt(x, y, z) == 2) { - glBindTexture(GL_TEXTURE_2D, textures1); + //glBindTexture(GL_TEXTURE_2D, textures1); } } }; -#endif +//#endif diff --git a/include/Utilities.h b/src/Utilities.h similarity index 100% rename from include/Utilities.h rename to src/Utilities.h diff --git a/src/XtreemNodes.h b/src/XtreemNodes.h new file mode 100644 index 0000000..e69de29 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..e2c4ad8 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,216 @@ +#include +#include +#include "stb_image.h" + +#include +#include +#include + +#include "GLShader.h" +#include "GLMesh.h" +#include "Camera.h" + +#include + +void framebuffer_size_callback(GLFWwindow* window, int width, int height); +void mouse_callback(GLFWwindow* window, double xpos, double ypos); +void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); +void processInput(GLFWwindow *window); + +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + +// camera +Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; +bool firstMouse = true; + +// timing +float deltaTime = 0.0f; // time between current frame and last frame +float lastFrame = 0.0f; + +int main() +{ + // glfw: initialize and configure + // ------------------------------ + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + +#ifdef __APPLE__ + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + + // glfw window creation + // -------------------- + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); + if (window == NULL) + { + std::cout << "Failed to create GLFW window" << std::endl; + glfwTerminate(); + return -1; + } + glfwMakeContextCurrent(window); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + glfwSetCursorPosCallback(window, mouse_callback); + glfwSetScrollCallback(window, scroll_callback); + + // tell GLFW to capture our mouse + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + // glad: load all OpenGL function pointers + // --------------------------------------- + // glew + glewExperimental = GL_TRUE; + if (glewInit() != GLEW_OK) + { + std::cout << "Failed to initialize GLEW" << std::endl; + return -1; + } + + // configure global opengl state + // ----------------------------- + glEnable(GL_DEPTH_TEST); + + Shader sh = Shader("data/shader/MeshV.shader", "data/shader/MeshF.shader"); + + static Vertex cubeVertices[] = { + // positions // normals // texture coords + Vertex(glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec2(0.0f, 0.0f)), + Vertex(glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec2(1.0f, 0.0f)), + Vertex(glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec2(1.0f, 1.0f)), + Vertex(glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec2(1.0f, 1.0f)), + Vertex(glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec2(0.0f, 1.0f)), + Vertex(glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec2(0.0f, 0.0f)), + + Vertex(glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(0.0f, 0.0f)), + Vertex(glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)), + Vertex(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 1.0f)), + Vertex(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 1.0f)), + Vertex(glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(0.0f, 1.0f)), + Vertex(glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(0.0f, 0.0f)), + + Vertex(glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec2(1.0f, 0.0f)), + Vertex(glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec2(1.0f, 1.0f)), + Vertex(glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 1.0f)), + Vertex(glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 1.0f)), + Vertex(glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)), + Vertex(glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec2(1.0f, 0.0f)), + + Vertex(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(1.0f, 0.0f)), + Vertex(glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(1.0f, 1.0f)), + Vertex(glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 1.0f)), + Vertex(glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 1.0f)), + Vertex(glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)), + Vertex(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(1.0f, 0.0f)), + + Vertex(glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.0f, -1.0f, 0.0f), glm::vec2(0.0f, 1.0f)), + Vertex(glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.0f, -1.0f, 0.0f), glm::vec2(1.0f, 1.0f)), + Vertex(glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.0f, -1.0f, 0.0f), glm::vec2(1.0f, 0.0f)), + Vertex(glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.0f, -1.0f, 0.0f), glm::vec2(1.0f, 0.0f)), + Vertex(glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(0.0f, -1.0f, 0.0f), glm::vec2(0.0f, 0.0f)), + Vertex(glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.0f, -1.0f, 0.0f), glm::vec2(0.0f, 1.0f)), + + Vertex(glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.0f, 1.0f)), + Vertex(glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(1.0f, 1.0f)), + Vertex(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(1.0f, 0.0f)), + Vertex(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(1.0f, 0.0f)), + Vertex(glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.0f, 0.0f)), + Vertex(glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.0f, 1.0f)), + }; + + GLMesh mesh = GLMesh(cubeVertices, sizeof(cubeVertices)); + + + // render loop + // ----------- + while (!glfwWindowShouldClose(window)) + { + // per-frame time logic + // -------------------- + float currentFrame = static_cast(glfwGetTime()); + deltaTime = currentFrame - lastFrame; + lastFrame = currentFrame; + + // input + // ----- + processInput(window); + + // render + // ------ + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + mesh.DrawArrays(sh); + + // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) + // ------------------------------------------------------------------------------- + glfwSwapBuffers(window); + glfwPollEvents(); + } + + // glfw: terminate, clearing all previously allocated GLFW resources. + // ------------------------------------------------------------------ + glfwTerminate(); + return 0; +} + +// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly +// --------------------------------------------------------------------------------------------------------- +void processInput(GLFWwindow *window) +{ + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) + glfwSetWindowShouldClose(window, true); + + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) + camera.ProcessKeyboard(FORWARD, deltaTime); + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) + camera.ProcessKeyboard(BACKWARD, deltaTime); + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) + camera.ProcessKeyboard(LEFT, deltaTime); + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) + camera.ProcessKeyboard(RIGHT, deltaTime); +} + +// glfw: whenever the window size changed (by OS or user resize) this callback function executes +// --------------------------------------------------------------------------------------------- +void framebuffer_size_callback(GLFWwindow* window, int width, int height) +{ + // make sure the viewport matches the new window dimensions; note that width and + // height will be significantly larger than specified on retina displays. + glViewport(0, 0, width, height); +} + + +// glfw: whenever the mouse moves, this callback is called +// ------------------------------------------------------- +void mouse_callback(GLFWwindow* window, double xposIn, double yposIn) +{ + float xpos = static_cast(xposIn); + float ypos = static_cast(yposIn); + + if (firstMouse) + { + lastX = xpos; + lastY = ypos; + firstMouse = false; + } + + float xoffset = xpos - lastX; + float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top + + lastX = xpos; + lastY = ypos; + + camera.ProcessMouseMovement(xoffset, yoffset); +} + +// glfw: whenever the mouse scroll wheel scrolls, this callback is called +// ---------------------------------------------------------------------- +void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) +{ + camera.ProcessMouseScroll(static_cast(yoffset)); +} \ No newline at end of file diff --git a/src/stb_image.cpp b/src/stb_image.cpp new file mode 100644 index 0000000..badb3ef --- /dev/null +++ b/src/stb_image.cpp @@ -0,0 +1,2 @@ +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" \ No newline at end of file diff --git a/include/stb_image.h b/src/stb_image.h similarity index 100% rename from include/stb_image.h rename to src/stb_image.h