Unify shaders
This commit is contained in:
parent
7190568012
commit
9d808aa55b
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.idea
|
||||||
|
cmake-build-debug
|
||||||
|
includes
|
||||||
|
libraries
|
119
main.cpp
119
main.cpp
@ -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);
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
#version 460 core
|
#version 460 core
|
||||||
|
|
||||||
in vec2 fragmentTextureCoordinates;
|
in vec3 fNormal;
|
||||||
|
in vec2 fTextureCoordinates;
|
||||||
|
in vec3 fColor;
|
||||||
|
|
||||||
uniform sampler2D sampler;
|
layout(location = 3) uniform sampler2D sampler;
|
||||||
|
|
||||||
out vec4 color;
|
out vec4 color;
|
||||||
|
|
||||||
|
const vec3 lightDirection = normalize(vec3(0.0f, 0.0f, -1.0f));
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
color = texture(sampler, fragmentTextureCoordinates);
|
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);
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
layout(location = 3) in vec3 vColor;
|
||||||
|
|
||||||
uniform mat4 modelTransform;
|
layout(location = 0) uniform mat4 modelTransform;
|
||||||
uniform mat4 viewTransform;
|
layout(location = 1) uniform mat4 viewTransform;
|
||||||
uniform mat4 projectionTransform;
|
layout(location = 2) uniform mat4 projectionTransform;
|
||||||
|
|
||||||
out vec2 fragmentTextureCoordinates;
|
out vec3 fNormal;
|
||||||
|
out vec2 fTextureCoordinates;
|
||||||
|
out vec3 fColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = projectionTransform * viewTransform * modelTransform * vec4(position, 1.0f);
|
gl_Position = projectionTransform * viewTransform * modelTransform * vec4(vPosition, 1.0f);
|
||||||
fragmentTextureCoordinates = vec2(textureCoordinates.x, 1.0f - textureCoordinates.y);
|
fNormal = vNormal;
|
||||||
|
fTextureCoordinates = vTextureCoordinates;
|
||||||
|
fColor = vColor;
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
@ -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;
|
|
||||||
}
|
|
@ -279,7 +279,6 @@ GLuint loadTexture(const string& textureFilePath) noexcept {
|
|||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
SOIL_free_image_data(imageData);
|
SOIL_free_image_data(imageData);
|
||||||
|
|
||||||
|
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user