First attempt at getting textures to work I'm obviously missing something
This commit is contained in:
parent
84d96acdd3
commit
b025639f81
4 changed files with 41 additions and 6 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
37
src/main.c
37
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(
|
||||
|
|
|
|||
BIN
testxure.png
Normal file
BIN
testxure.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 63 KiB |
Loading…
Reference in a new issue