From 9d808aa55b497a2cc602fdb68153bcc41af7a50f Mon Sep 17 00:00:00 2001 From: Ilshat Safiullin Date: Fri, 15 Jun 2018 18:48:45 +0500 Subject: [PATCH] Unify shaders --- .gitignore | 4 ++ main.cpp | 119 ++++++++++++++------------------------ resources/shader.fragment | 28 +++++---- resources/shader.vertex | 36 +++++++----- resources/thread.fragment | 17 ------ resources/thread.vertex | 21 ------- utilities.cpp | 1 - 7 files changed, 86 insertions(+), 140 deletions(-) create mode 100644 .gitignore delete mode 100644 resources/thread.fragment delete mode 100644 resources/thread.vertex diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b95380d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea +cmake-build-debug +includes +libraries diff --git a/main.cpp b/main.cpp index 4e487b8..36dbf22 100644 --- a/main.cpp +++ b/main.cpp @@ -97,45 +97,43 @@ void bufferVertices( } }; -int main() { +int main() noexcept(false) { setUp(); auto shaderProgram = linkShaderProgram({ {GL_VERTEX_SHADER, "../resources/shader.vertex"}, {GL_FRAGMENT_SHADER, "../resources/shader.fragment"} }); - auto texture = loadTexture("../resources/wooden-container.jpg"); - GLfloat vertices[] = { - // position, texture coordinates - -2048.0f, -2048.0f, 0.0f, 0.0f, 0.0f, - 2048.0f, -2048.0f, 0.0f, 1.0f, 0.0f, - 2048.0f, 2048.0f, 0.0f, 1.0f, 1.0f, - 2048.0f, 2048.0f, 0.0f, 1.0f, 1.0f, - -2048.0f, 2048.0f, 0.0f, 0.0f, 1.0f, - -2048.0f, -2048.0f, 0.0f, 0.0f, 0.0f + auto planeTexture = loadTexture("../resources/wooden-container.jpg"); + + GLfloat planeVertices[] = { + // position, normal, texture coordinates, color + -2048.0f, -2048.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, + 2048.0f, -2048.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, + 2048.0f, 2048.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + 2048.0f, 2048.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + -2048.0f, 2048.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, + -2048.0f, -2048.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f }; - GLuint vertexArrayObject; - glGenVertexArrays(1, &vertexArrayObject); + GLuint planeVAO; glGenVertexArrays(1, &planeVAO); + GLuint planeVBO; glGenBuffers(1, &planeVBO); - GLuint vertexBufferObject; - glGenBuffers(1, &vertexBufferObject); - - glBindVertexArray(vertexArrayObject); - glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(0 * sizeof(GLfloat))); - glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); - glEnableVertexAttribArray(1); + glBindVertexArray(planeVAO); + glBindBuffer(GL_ARRAY_BUFFER, planeVBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), planeVertices, GL_STATIC_DRAW); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(0 * sizeof(GLfloat))); + glEnableVertexAttribArray(0); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); + glEnableVertexAttribArray(1); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat))); + glEnableVertexAttribArray(2); + glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(8 * sizeof(GLfloat))); + glEnableVertexAttribArray(3); glBindVertexArray(0); // threads - auto threadShaderProgram = linkShaderProgram({ - {GL_VERTEX_SHADER, "../resources/thread.vertex"}, - {GL_FRAGMENT_SHADER, "../resources/thread.fragment"} - }); auto threadTexture = loadTexture("../resources/fabric.jpg"); setWindowTitle("parsing lines.json"); @@ -212,65 +210,36 @@ int main() { renderCycle([ shaderProgram, - texture, - vertexArrayObject, - threadShaderProgram, - threadTexture, - threadModelTransform, - threadVAO, - verticesBuffer - ] () { + planeTexture, + planeVAO, + threadTexture, + threadModelTransform, + threadVAO, + verticesBuffer + ] () { glClearColor(0.5f, 0.5f, 1.0f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glUseProgram(shaderProgram); + + glUniformMatrix4fv(0, 1, GL_FALSE, glm::value_ptr(glm::mat4(1.0f))); + glUniformMatrix4fv(1, 1, GL_FALSE, glm::value_ptr(getViewTransform())); + glUniformMatrix4fv(2, 1, GL_FALSE, glm::value_ptr(getProjectionTransform())); + glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture); - glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0); - glUniformMatrix4fv( - glGetUniformLocation(shaderProgram, "modelTransform"), - 1, - GL_FALSE, - glm::value_ptr(glm::mat4(1.0f)) - ); - glUniformMatrix4fv( - glGetUniformLocation(shaderProgram, "viewTransform"), - 1, - GL_FALSE, - glm::value_ptr(getViewTransform()) - ); - glUniformMatrix4fv( - glGetUniformLocation(shaderProgram, "projectionTransform"), - 1, - GL_FALSE, - glm::value_ptr(getProjectionTransform()) - ); - glBindVertexArray(vertexArrayObject); + glBindTexture(GL_TEXTURE_2D, planeTexture); + glUniform1i(3, 0); + + glBindVertexArray(planeVAO); glDrawArrays(GL_TRIANGLES, 0, 6); glBindVertexArray(0); - glUseProgram(threadShaderProgram); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, threadTexture); - glUniform1i(glGetUniformLocation(threadShaderProgram, "sampler"), 0); - glUniformMatrix4fv( - glGetUniformLocation(threadShaderProgram, "modelTransform"), - 1, - GL_FALSE, - glm::value_ptr(threadModelTransform) - ); - glUniformMatrix4fv( - glGetUniformLocation(threadShaderProgram, "viewTransform"), - 1, - GL_FALSE, - glm::value_ptr(getViewTransform()) - ); - glUniformMatrix4fv( - glGetUniformLocation(threadShaderProgram, "projectionTransform"), - 1, - GL_FALSE, - glm::value_ptr(getProjectionTransform()) - ); + glUniform1i(3, 0); + + glUniformMatrix4fv(0, 1, GL_FALSE, glm::value_ptr(threadModelTransform)); + glBindVertexArray(threadVAO); glDrawArrays(GL_TRIANGLES, 0, verticesBuffer.size()); glBindVertexArray(0); diff --git a/resources/shader.fragment b/resources/shader.fragment index dcaed05..d9d6021 100644 --- a/resources/shader.fragment +++ b/resources/shader.fragment @@ -1,11 +1,17 @@ -#version 460 core - -in vec2 fragmentTextureCoordinates; - -uniform sampler2D sampler; - -out vec4 color; - -void main() { - color = texture(sampler, fragmentTextureCoordinates); -} +#version 460 core + +in vec3 fNormal; +in vec2 fTextureCoordinates; +in vec3 fColor; + +layout(location = 3) uniform sampler2D sampler; + +out vec4 color; + +const vec3 lightDirection = normalize(vec3(0.0f, 0.0f, -1.0f)); + +void main() { + vec3 sampleColor = texture(sampler, fTextureCoordinates).rgb; + float intensity = clamp(dot(lightDirection, -normalize(fNormal)), 0.0f, 1.0f); + color = vec4(intensity * sampleColor * fColor + vec3(0.15f, 0.15f, 0.15f), 1.0f); +} diff --git a/resources/shader.vertex b/resources/shader.vertex index b3db785..a70a744 100644 --- a/resources/shader.vertex +++ b/resources/shader.vertex @@ -1,15 +1,21 @@ -#version 460 core - -layout(location = 0) in vec3 position; -layout(location = 1) in vec2 textureCoordinates; - -uniform mat4 modelTransform; -uniform mat4 viewTransform; -uniform mat4 projectionTransform; - -out vec2 fragmentTextureCoordinates; - -void main() { - gl_Position = projectionTransform * viewTransform * modelTransform * vec4(position, 1.0f); - fragmentTextureCoordinates = vec2(textureCoordinates.x, 1.0f - textureCoordinates.y); -} +#version 460 core + +layout(location = 0) in vec3 vPosition; +layout(location = 1) in vec3 vNormal; +layout(location = 2) in vec2 vTextureCoordinates; +layout(location = 3) in vec3 vColor; + +layout(location = 0) uniform mat4 modelTransform; +layout(location = 1) uniform mat4 viewTransform; +layout(location = 2) uniform mat4 projectionTransform; + +out vec3 fNormal; +out vec2 fTextureCoordinates; +out vec3 fColor; + +void main() { + gl_Position = projectionTransform * viewTransform * modelTransform * vec4(vPosition, 1.0f); + fNormal = vNormal; + fTextureCoordinates = vTextureCoordinates; + fColor = vColor; +} diff --git a/resources/thread.fragment b/resources/thread.fragment deleted file mode 100644 index c61b93d..0000000 --- a/resources/thread.fragment +++ /dev/null @@ -1,17 +0,0 @@ -#version 460 core - -in vec3 fNormal; -in vec2 fTextureCoordinates; -in vec3 fColor; - -uniform sampler2D sampler; - -out vec4 color; - -const vec3 lightDirection = normalize(vec3(0.0f, 0.0f, -1.0f)); - -void main() { - vec3 sampleColor = texture(sampler, fTextureCoordinates).rgb; - float intensity = clamp(dot(lightDirection, -normalize(fNormal)), 0.0f, 1.0f); - color = vec4(intensity * sampleColor * fColor + vec3(0.15f, 0.15f, 0.15f), 1.0f); -} diff --git a/resources/thread.vertex b/resources/thread.vertex deleted file mode 100644 index 8852e65..0000000 --- a/resources/thread.vertex +++ /dev/null @@ -1,21 +0,0 @@ -#version 460 core - -layout(location = 0) in vec3 vPosition; -layout(location = 1) in vec3 vNormal; -layout(location = 2) in vec2 vTextureCoordinates; -layout(location = 3) in vec3 vColor; - -uniform mat4 modelTransform; -uniform mat4 viewTransform; -uniform mat4 projectionTransform; - -out vec3 fNormal; -out vec2 fTextureCoordinates; -out vec3 fColor; - -void main() { - gl_Position = projectionTransform * viewTransform * modelTransform * vec4(vPosition, 1.0f); - fNormal = vNormal; - fTextureCoordinates = vTextureCoordinates; - fColor = vColor; -} diff --git a/utilities.cpp b/utilities.cpp index 28f826b..d534edc 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -278,7 +278,6 @@ GLuint loadTexture(const string& textureFilePath) noexcept { glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); SOIL_free_image_data(imageData); - return texture; }