Unify shaders

This commit is contained in:
Ilshat Safiullin 2018-06-15 18:48:45 +05:00
parent 7190568012
commit 9d808aa55b
7 changed files with 86 additions and 140 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.idea
cmake-build-debug
includes
libraries

119
main.cpp
View File

@ -97,45 +97,43 @@ void bufferVertices(
} }
}; };
int main() { int main() noexcept(false) {
setUp(); setUp();
auto shaderProgram = linkShaderProgram({ auto shaderProgram = linkShaderProgram({
{GL_VERTEX_SHADER, "../resources/shader.vertex"}, {GL_VERTEX_SHADER, "../resources/shader.vertex"},
{GL_FRAGMENT_SHADER, "../resources/shader.fragment"} {GL_FRAGMENT_SHADER, "../resources/shader.fragment"}
}); });
auto texture = loadTexture("../resources/wooden-container.jpg");
GLfloat vertices[] = { auto planeTexture = loadTexture("../resources/wooden-container.jpg");
// position, texture coordinates
-2048.0f, -2048.0f, 0.0f, 0.0f, 0.0f, GLfloat planeVertices[] = {
2048.0f, -2048.0f, 0.0f, 1.0f, 0.0f, // position, normal, texture coordinates, color
2048.0f, 2048.0f, 0.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,
2048.0f, 2048.0f, 0.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, 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 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; GLuint planeVAO; glGenVertexArrays(1, &planeVAO);
glGenVertexArrays(1, &vertexArrayObject); GLuint planeVBO; glGenBuffers(1, &planeVBO);
GLuint vertexBufferObject; glBindVertexArray(planeVAO);
glGenBuffers(1, &vertexBufferObject); glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), planeVertices, GL_STATIC_DRAW);
glBindVertexArray(vertexArrayObject); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(0 * sizeof(GLfloat)));
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(0 * sizeof(GLfloat))); glEnableVertexAttribArray(1);
glEnableVertexAttribArray(0); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(2);
glEnableVertexAttribArray(1); glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(8 * sizeof(GLfloat)));
glEnableVertexAttribArray(3);
glBindVertexArray(0); glBindVertexArray(0);
// threads // threads
auto threadShaderProgram = linkShaderProgram({
{GL_VERTEX_SHADER, "../resources/thread.vertex"},
{GL_FRAGMENT_SHADER, "../resources/thread.fragment"}
});
auto threadTexture = loadTexture("../resources/fabric.jpg"); auto threadTexture = loadTexture("../resources/fabric.jpg");
setWindowTitle("parsing lines.json"); setWindowTitle("parsing lines.json");
@ -212,65 +210,36 @@ int main() {
renderCycle([ renderCycle([
shaderProgram, shaderProgram,
texture, planeTexture,
vertexArrayObject, planeVAO,
threadShaderProgram, threadTexture,
threadTexture, threadModelTransform,
threadModelTransform, threadVAO,
threadVAO, verticesBuffer
verticesBuffer ] () {
] () {
glClearColor(0.5f, 0.5f, 1.0f, 1.0f); glClearColor(0.5f, 0.5f, 1.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram); 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); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, planeTexture);
glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0); glUniform1i(3, 0);
glUniformMatrix4fv(
glGetUniformLocation(shaderProgram, "modelTransform"), glBindVertexArray(planeVAO);
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);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0); glBindVertexArray(0);
glUseProgram(threadShaderProgram);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, threadTexture); glBindTexture(GL_TEXTURE_2D, threadTexture);
glUniform1i(glGetUniformLocation(threadShaderProgram, "sampler"), 0); glUniform1i(3, 0);
glUniformMatrix4fv(
glGetUniformLocation(threadShaderProgram, "modelTransform"), glUniformMatrix4fv(0, 1, GL_FALSE, glm::value_ptr(threadModelTransform));
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())
);
glBindVertexArray(threadVAO); glBindVertexArray(threadVAO);
glDrawArrays(GL_TRIANGLES, 0, verticesBuffer.size()); glDrawArrays(GL_TRIANGLES, 0, verticesBuffer.size());
glBindVertexArray(0); glBindVertexArray(0);

View File

@ -1,11 +1,17 @@
#version 460 core #version 460 core
in vec2 fragmentTextureCoordinates; in vec3 fNormal;
in vec2 fTextureCoordinates;
uniform sampler2D sampler; in vec3 fColor;
out vec4 color; layout(location = 3) uniform sampler2D sampler;
void main() { out vec4 color;
color = texture(sampler, fragmentTextureCoordinates);
} 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);
}

View File

@ -1,15 +1,21 @@
#version 460 core #version 460 core
layout(location = 0) in vec3 position; layout(location = 0) in vec3 vPosition;
layout(location = 1) in vec2 textureCoordinates; layout(location = 1) in vec3 vNormal;
layout(location = 2) in vec2 vTextureCoordinates;
uniform mat4 modelTransform; layout(location = 3) in vec3 vColor;
uniform mat4 viewTransform;
uniform mat4 projectionTransform; layout(location = 0) uniform mat4 modelTransform;
layout(location = 1) uniform mat4 viewTransform;
out vec2 fragmentTextureCoordinates; layout(location = 2) uniform mat4 projectionTransform;
void main() { out vec3 fNormal;
gl_Position = projectionTransform * viewTransform * modelTransform * vec4(position, 1.0f); out vec2 fTextureCoordinates;
fragmentTextureCoordinates = vec2(textureCoordinates.x, 1.0f - textureCoordinates.y); out vec3 fColor;
}
void main() {
gl_Position = projectionTransform * viewTransform * modelTransform * vec4(vPosition, 1.0f);
fNormal = vNormal;
fTextureCoordinates = vTextureCoordinates;
fColor = vColor;
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -278,7 +278,6 @@ GLuint loadTexture(const string& textureFilePath) noexcept {
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
SOIL_free_image_data(imageData); SOIL_free_image_data(imageData);
return texture; return texture;
} }