#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
int main() {
// |>----------<>----------<>----------<>----------<>----------<|
// |> DEFINE VERTICES <|
// |>----------<>----------<>----------<>----------<>----------<|
// We add texture cordinates to our vertex data
float vertices[]{
-100.0f, -100.0f, 0.0f, 0.0f,// 0 - bottom left
100.0f, -100.0f, 1.0f, 0.0f,// 1 - bottom right
100.0f, 100.0f, 1.0f, 1.0f,// 2 - top right
-100.0f, 100.0f, 0.0f, 1.0f,// 3 - top left
};
//for (float& i : vertices) i *= 200; // we scale our shape
unsigned int indices[] = {
0, 1, 2, // first Triangle
0, 2, 3 // second Triangle
};
// we change the layout
unsigned int layout[]{ 2, GL_FLOAT, 2, GL_FLOAT };
VertexArray va; va.Bind();
VertexBuffer vb(&vertices[0], sizeof(vertices));
IndexBuffer ib(indices, sizeof(indices));
vb.setLayout(layout,sizeof(layout));
va.Unbind();
// |>----------<>----------<>----------<>----------<>----------<|
// |> TEXTURE <|
// |>----------<>----------<>----------<>----------<>----------<|
// load and create a texture
unsigned int texture1;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
// 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);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
// The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
unsigned char* data = stbi_load("res/horse-face.png", &width, &height, &nrChannels, 0);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
shader.SetUniform1i("texture1", 0);
// enable blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
#shader vertex
#version 330 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
uniform float time;
uniform mat4 u_MVP;
void main() {
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
gl_Position = u_MVP * vec4(aPos.x, aPos.y, 0.0, 1.0);
}
#shader fragment
#version 330 core
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D texture1;
void main() {
FragColor = texture(texture1, TexCoord);
}
Leave a Reply