Texturing
This commit is contained in:
7
assets/shaders/fragment1.glsl
Normal file
7
assets/shaders/fragment1.glsl
Normal file
@@ -0,0 +1,7 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
in vec3 ourColor;
|
||||
|
||||
void main() {
|
||||
FragColor = vec4(ourColor , 1.0);
|
||||
}
|
||||
7
assets/shaders/fragment2.glsl
Normal file
7
assets/shaders/fragment2.glsl
Normal file
@@ -0,0 +1,7 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
uniform vec4 Color;
|
||||
|
||||
void main() {
|
||||
FragColor = Color;
|
||||
}
|
||||
12
assets/shaders/fragment3.glsl
Normal file
12
assets/shaders/fragment3.glsl
Normal file
@@ -0,0 +1,12 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec3 ourColor;
|
||||
in vec2 TextureCords;
|
||||
|
||||
uniform sampler2D Texture;
|
||||
|
||||
void main() {
|
||||
FragColor = texture(Texture, TextureCords) * vec4(ourColor, 1.0);
|
||||
}
|
||||
|
||||
10
assets/shaders/vertex1.glsl
Normal file
10
assets/shaders/vertex1.glsl
Normal file
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
|
||||
out vec3 ourColor;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
ourColor = aColor;
|
||||
}
|
||||
6
assets/shaders/vertex2.glsl
Normal file
6
assets/shaders/vertex2.glsl
Normal file
@@ -0,0 +1,6 @@
|
||||
#version 330
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
}
|
||||
14
assets/shaders/vertex3.glsl
Normal file
14
assets/shaders/vertex3.glsl
Normal file
@@ -0,0 +1,14 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
layout (location = 2) in vec2 aTextureCords;
|
||||
|
||||
out vec3 ourColor;
|
||||
out vec2 TextureCords;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
ourColor = aColor;
|
||||
TextureCords = aTextureCords;
|
||||
}
|
||||
|
||||
BIN
assets/textures/wall.jpg
Normal file
BIN
assets/textures/wall.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 251 KiB |
7897
include/stb_image.h
Normal file
7897
include/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
103
src/main.c
103
src/main.c
@@ -3,6 +3,8 @@
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "stb_image.h"
|
||||
|
||||
#include "Renderer.h"
|
||||
#include "Input.h"
|
||||
|
||||
@@ -14,47 +16,69 @@ int main() {
|
||||
glfwSetKeyCallback(window, &key_pressed);
|
||||
|
||||
float vertices[] = {
|
||||
-0.3f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.8f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.3f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
|
||||
//pos // ColorIndecies //Texture coordnites
|
||||
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.5f, 1.0f
|
||||
};
|
||||
float vertices2[] = {
|
||||
-0.8f, -0.5f, 0.0f,
|
||||
-0.3f, -0.5f, 0.0f,
|
||||
-0.5f, 0.5f, 0.0f
|
||||
};
|
||||
|
||||
char* vertex1 = "../shaders/vertex1.glsl\0";
|
||||
char* fragment1 = "../shaders/fragment1.glsl\0";
|
||||
char* vertex2 = "../shaders/vertex2.glsl\0";
|
||||
char* fragment2 = "../shaders/fragment2.glsl\0";
|
||||
|
||||
unsigned int shader1, shader2;
|
||||
printf("Compiling shader1\n");
|
||||
// Compile Shader
|
||||
char* vertex1 = "../assets/shaders/vertex3.glsl\0";
|
||||
char* fragment1 = "../assets/shaders/fragment3.glsl\0";
|
||||
unsigned int shader1;
|
||||
shader1 = loadCompileShader(vertex1, fragment1);
|
||||
printf("Compiling shader2\n");
|
||||
shader2 = loadCompileShader(vertex2, fragment2);
|
||||
|
||||
unsigned int VAOs[2], VBOs[2];
|
||||
glGenVertexArrays(2, VAOs);
|
||||
glGenBuffers(2, VBOs);
|
||||
// Initialise VBO and VAO
|
||||
unsigned int VAO, VBO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
//unsigned int EBO;
|
||||
//glGenBuffers(1, &EBO);
|
||||
|
||||
glBindVertexArray(VAOs[0]);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0 );
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0 );
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3,GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
|
||||
glVertexAttribPointer(1, 3,GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
// Set texture parametes
|
||||
|
||||
float borderColor[] = { 1.0f, 1.0f, 0.0f, 1.0f };
|
||||
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
|
||||
|
||||
// Generate Texture
|
||||
unsigned int texture;
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
//Set Texture Parametes
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
// Load Texture image
|
||||
int width, height, channels;
|
||||
unsigned char *data = stbi_load("../assets/textures/wall.jpg", &width, &height, &channels, 0);
|
||||
|
||||
if(data) {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
} else {
|
||||
printf("failed to load texture\n");
|
||||
}
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
|
||||
glBindVertexArray(VAOs[1]);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0 );
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
|
||||
@@ -62,24 +86,17 @@ int main() {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shader1);
|
||||
glBindVertexArray(VAOs[0]);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glBindVertexArray(VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
float time = glfwGetTime();
|
||||
float green = (sin(time) / 2.0f) + 0.5f;
|
||||
|
||||
int vertexColorLocation = glGetUniformLocation(shader2, "Color");
|
||||
glUseProgram(shader2);
|
||||
glUniform4f(vertexColorLocation, 0.0f, green, 0.0f, 1.0f);
|
||||
|
||||
glBindVertexArray(VAOs[1]);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
2
src/stb_image.c
Normal file
2
src/stb_image.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
Reference in New Issue
Block a user