Compare commits

...

2 commits

4 changed files with 66 additions and 11 deletions

View file

@ -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);
}

View file

@ -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;
}

View file

@ -1,9 +1,12 @@
#include <GL/glew.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_surface.h>
#include <stdlib.h>
#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 +79,16 @@ 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
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
@ -100,13 +104,16 @@ 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);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void *)(3 * sizeof(float)));
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.
@ -116,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
@ -153,9 +198,11 @@ int main(int argc, char *argv[]) {
// draw our first triangle
glUseProgram(shader->shaderProgram);
glBindVertexArray(
VAO); // 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
// 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

BIN
testxure.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB