Loaded texture correctly
This commit is contained in:
parent
b025639f81
commit
ed015e0af7
1 changed files with 48 additions and 28 deletions
74
src/main.c
74
src/main.c
|
|
@ -1,5 +1,6 @@
|
|||
#include <GL/glew.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_surface.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL3/SDL_events.h"
|
||||
|
|
@ -88,26 +89,6 @@ int main(int argc, char *argv[]) {
|
|||
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);
|
||||
|
|
@ -124,14 +105,15 @@ int main(int argc, char *argv[]) {
|
|||
GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
|
||||
(void *)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// note that this is allowed, the call to glVertexAttribPointer registered VBO
|
||||
// as the vertex attribute's bound vertex buffer object so afterwards we can
|
||||
// safely unbind
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
// remember: do NOT unbind the EBO while a VAO is active as the bound element
|
||||
// buffer object IS stored in the VAO; keep the EBO bound.
|
||||
|
|
@ -141,11 +123,49 @@ int main(int argc, char *argv[]) {
|
|||
// modify this VAO, but this rarely happens. Modifying other VAOs requires a
|
||||
// call to glBindVertexArray anyways so we generally don't unbind VAOs (nor
|
||||
// VBOs) when it's not directly necessary.
|
||||
glBindVertexArray(0);
|
||||
// glBindVertexArray(0);
|
||||
|
||||
// Texture Load
|
||||
SDL_Surface *image = IMG_Load("./testxure.png");
|
||||
if (image == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load texture");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
SDL_FlipSurface(image, SDL_FLIP_VERTICAL);
|
||||
|
||||
GLuint texture;
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glGenTextures(1, &texture);
|
||||
|
||||
// Handle different SDL Surface data types
|
||||
int mode = GL_RGBA;
|
||||
// if (image->format->BytesPerPixel == 4) {
|
||||
// mode = GL_RGBA;
|
||||
// }
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
// set the texture wrapping parameters
|
||||
glTexParameteri(
|
||||
GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
|
||||
GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
// set texture filtering parameters
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h, 0, mode,
|
||||
GL_UNSIGNED_BYTE, image->pixels);
|
||||
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
SDL_DestroySurface(image);
|
||||
|
||||
// Initialise camera
|
||||
Camera camera = Camera_default;
|
||||
|
||||
// set our texture uniform
|
||||
glUseProgram(shader->shaderProgram);
|
||||
glUniform1i(glGetUniformLocation(shader->shaderProgram, "ourTexture"), 0);
|
||||
|
||||
bool wants_running = true;
|
||||
while (wants_running) {
|
||||
// TODO: Create event handler files
|
||||
|
|
@ -171,18 +191,18 @@ 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(
|
||||
VAO); // seeing as we only have a single VAO there's no need to bind it
|
||||
|
||||
// seeing as we only have a single VAO there's no need to bind it
|
||||
// every time, but we'll do so to keep things a bit more organized
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
// glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
// glBindVertexArray(0); // no need to unbind it every time
|
||||
|
|
|
|||
Loading…
Reference in a new issue