diff --git a/exercise2/glsl/terrain.frag b/exercise2/glsl/terrain.frag index 4be09b5..b019857 100644 --- a/exercise2/glsl/terrain.frag +++ b/exercise2/glsl/terrain.frag @@ -4,11 +4,13 @@ // Copyright (C) CGV TU Dresden - All Rights Reserved in vec3 n; +in vec2 uv; out vec4 color; uniform vec3 cameraPos; +uniform sampler2D grass; uniform sampler2D background; uniform vec2 screenSize; @@ -37,7 +39,8 @@ void main() vec3 dirToViewer = vec3(0, 1, 0); //material properties - color = vec4(0.6, 0.6, 0.6, 1); + //color = vec4(0.6, 0.6, 0.6, 1); + color = texture(grass, uv); float specular = 0; diff --git a/exercise2/glsl/terrain.vert b/exercise2/glsl/terrain.vert index 997a28d..35691a8 100644 --- a/exercise2/glsl/terrain.vert +++ b/exercise2/glsl/terrain.vert @@ -6,6 +6,7 @@ in vec4 position; out vec3 n; +out vec2 uv; uniform mat4 mvp; @@ -28,6 +29,9 @@ void main() { vec4 terrain_position = vec4(position.x, getTerrainHeight(vec2(position.x, position.z)), position.z, position.w); n = calculate_normal(position); + //n = vec3(0.0, 1.0, 0.0); + + uv = vec2(terrain_position.x / 255.0, terrain_position.z / 255.0); gl_Position = mvp * terrain_position; } diff --git a/exercise2/include/Viewer.h b/exercise2/include/Viewer.h index 0a17718..9f3d60a 100644 --- a/exercise2/include/Viewer.h +++ b/exercise2/include/Viewer.h @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #pragma once @@ -11,17 +11,16 @@ class Viewer : public nse::gui::AbstractViewer { -public: + public: Viewer(); void LoadShaders(); void CreateGeometry(); - void drawContents(); - bool resizeEvent(const Eigen::Vector2i&); - -private: + void drawContents(); + bool resizeEvent(const Eigen::Vector2i &); + private: void RenderSky(); Eigen::Matrix4f view, proj; @@ -36,6 +35,8 @@ private: GLuint grassTexture, rockTexture, roadColorTexture, roadNormalMap, roadSpecularMap, alphaMap; + GLint grass_texture_location; + nse::gui::GLBuffer offsetBuffer; GLuint backgroundFBO, backgroundTexture; diff --git a/exercise2/src/Viewer.cpp b/exercise2/src/Viewer.cpp index 271c828..ed83647 100644 --- a/exercise2/src/Viewer.cpp +++ b/exercise2/src/Viewer.cpp @@ -29,6 +29,8 @@ Viewer::Viewer() LoadShaders(); CreateGeometry(); + grass_texture_location = terrainShader.uniform("grass"); + //Create a texture and framebuffer for the background glGenFramebuffers(1, &backgroundFBO); glGenTextures(1, &backgroundTexture); @@ -92,7 +94,46 @@ GLuint CreateTexture(const unsigned char *fileData, size_t fileLength, bool repe GLuint textureName; int textureWidth, textureHeight, textureChannels; auto pixelData = stbi_load_from_memory(fileData, fileLength, &textureWidth, &textureHeight, &textureChannels, 3); - textureName = 0; + + GLenum provided_format; + + if (textureChannels == 1) + { + provided_format = GL_R; + } + else if (textureChannels == 2) + { + provided_format = GL_RG; + } + else if (textureChannels == 3) + { + provided_format = GL_RGB; + } + else if (textureChannels == 4) + { + provided_format = GL_RGBA; + } + + glGenTextures(1, &textureName); + glBindTexture(GL_TEXTURE_2D, textureName); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, provided_format, GL_UNSIGNED_BYTE, pixelData); + + glGenerateMipmap(GL_TEXTURE_2D); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + + if (repeat) + { + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + } + else + { + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } + stbi_image_free(pixelData); return textureName; } @@ -227,6 +268,10 @@ void Viewer::drawContents() terrainShader.setUniform("mvp", mvp); terrainShader.setUniform("cameraPos", cameraPosition, false); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, grassTexture); + glUniform1i(grass_texture_location, 0); + /* Task: Render the terrain */ glPrimitiveRestartIndex(512); glDrawElements(GL_TRIANGLE_STRIP, (PATCH_SIZE - 1) * (PATCH_SIZE - 1) * 4, GL_UNSIGNED_INT, 0);