diff --git a/shaders/frag.glsl b/shaders/frag.glsl index c5d504b..c4bc256 100644 --- a/shaders/frag.glsl +++ b/shaders/frag.glsl @@ -1,7 +1,11 @@ #version 330 core out vec4 FragColor; +in vec2 TexCoord; + +uniform sampler2D ourTexture; + void main() { - FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); + FragColor = texture(ourTexture, TexCoord); } diff --git a/shaders/vert.glsl b/shaders/vert.glsl index e2feadd..99a7a57 100644 --- a/shaders/vert.glsl +++ b/shaders/vert.glsl @@ -1,10 +1,14 @@ #version 330 core layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoord; + +out vec2 TexCoord; uniform mat4 MVP; void main() { gl_Position = MVP * vec4(aPos.x, aPos.y, aPos.z, 1.0); + TexCoord = aTexCoord; } diff --git a/src/main.c b/src/main.c index 2853dd3..57739c8 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,8 @@ #include "SDL3/SDL_events.h" #include "SDL3/SDL_keycode.h" +#include "SDL3/SDL_surface.h" +#include "SDL3_image/SDL_image.h" #include "camera.h" #include "shader.h" #include "window.h" @@ -76,15 +78,36 @@ int main(int argc, char *argv[]) { // set up vertex data (and buffer(s)) and configure vertex attributes float vertices[] = { - 0.5f, 0.5f, 0.0f, // top right - 0.5f, -0.5f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f // top left + 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left + -0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left }; unsigned int indices[] = { 0, 1, 3, // first Triangle 1, 2, 3 // second Triangle }; + + // Texture settings + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + // Texture Load + SDL_Surface *texture = IMG_Load("testxure.png"); + if (texture == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load texture"); + return EXIT_FAILURE; + } + GLuint gl_texture; + glGenTextures(1, &gl_texture); + glBindTexture(GL_TEXTURE_2D, gl_texture); + glTexImage2D(gl_texture, 0, GL_RGBA, texture->w, texture->h, 0, GL_BGRA, + GL_UNSIGNED_BYTE, texture->pixels); + glGenerateMipmap(GL_TEXTURE_2D); + SDL_DestroySurface(texture); + unsigned int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); @@ -100,7 +123,9 @@ int main(int argc, char *argv[]) { glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), + (void *)(3 * sizeof(float))); glEnableVertexAttribArray(0); // note that this is allowed, the call to glVertexAttribPointer registered VBO @@ -146,11 +171,13 @@ int main(int argc, char *argv[]) { int mvp_uniform_location = glGetUniformLocation(shader->shaderProgram, "MVP"); glUniformMatrix4fv(mvp_uniform_location, 1, GL_FALSE, &mvp[0][0]); + glUniform1i(glGetUniformLocation(shader->shaderProgram, "ourTexture"), 0); // render glClearColor(0.1f, 0.3f, 0.4f, 1.f); glClear(GL_COLOR_BUFFER_BIT); + // draw our first triangle glUseProgram(shader->shaderProgram); glBindVertexArray( diff --git a/testxure.png b/testxure.png new file mode 100644 index 0000000..d9a6e28 Binary files /dev/null and b/testxure.png differ