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();
|
||||
|
||||
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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user