diff --git a/src/container.jpg b/src/container.jpg new file mode 100644 index 0000000..d07bee4 Binary files /dev/null and b/src/container.jpg differ diff --git a/src/fragment.glsl b/src/fragment.glsl index 31e169f..f6d7b2e 100644 --- a/src/fragment.glsl +++ b/src/fragment.glsl @@ -1,8 +1,12 @@ // Set the GLSL version to 3.3 and use the OpenGL core profile #version 330 core out vec4 FragColor; + in vec3 ourColor; +in vec2 TexCoord; + +uniform sampler2D ourTexture; void main() { - FragColor = vec4(ourColor, 1.0); + FragColor = texture(ourTexture, TexCoord); } diff --git a/src/image.cc b/src/image.cc new file mode 100644 index 0000000..050d3b8 --- /dev/null +++ b/src/image.cc @@ -0,0 +1,10 @@ +#include "image.hh" +#include + +Image::Image(const char* path) { + data = stbi_load(path, &width, &height, &nrChannels, 0); +} + +Image::~Image() { + stbi_image_free(data); +} diff --git a/src/image.hh b/src/image.hh new file mode 100644 index 0000000..d0245a8 --- /dev/null +++ b/src/image.hh @@ -0,0 +1,11 @@ +#ifndef IMAGE_HH +#define IMAGE_HH +#define STB_IMAGE_IMPLEMENTATION +class Image { + public: + int width, height, nrChannels; + unsigned char* data; + Image(const char* path); + ~Image(); +}; +#endif diff --git a/src/main.cc b/src/main.cc index 1af3d70..f448494 100644 --- a/src/main.cc +++ b/src/main.cc @@ -6,6 +6,7 @@ #include "shader.hh" #include "utilities.hh" +#include "image.hh" void framebuffer_size_callback(GLFWwindow* window, int width, int height); void process_input(GLFWwindow* window); @@ -34,10 +35,10 @@ struct GlRenderObjects { GlRenderObjects* triangle() { // An array of verticies containing data for a triangle. float vertices[] = { - // positions // colors - -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.0f, 0.0f,1.0f, 0.0f, - 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f + // positions // colors // texture coords + -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.0f, 0.0f,1.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }; unsigned int vertexBufferObject, vertexArrayObject; @@ -58,14 +59,18 @@ GlRenderObjects* triangle() { // This will pass the position attribute to the location "0". We tell OpenGL the size of each position attribute is 3 floats. // We tell OpenGL that we're using floating point types. // We tell OpenGL we don't want our data to be normalized as its already normalized. - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // Pass the color attribute data to the location 1. // We tell it to offset the data per "vertex" by 3 floats to get the color data instead of the position data. - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float))); glEnableVertexAttribArray(1); + // Process the texture coord data. + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float))); + glEnableVertexAttribArray(2); + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -74,11 +79,11 @@ GlRenderObjects* triangle() { GlRenderObjects* rectangle() { float vertices[] = { - // positions // colors - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.0f, 0.0f,1.0f, 0.0f, - -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, - -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f + // positions // colors // texture coords + 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.0f, 0.0f,1.0f, 0.0f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f }; unsigned int indices[] = { @@ -107,13 +112,17 @@ GlRenderObjects* rectangle() { glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // Tell OpenGL on how to process the verticies. - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0); glEnableVertexAttribArray(0); // Process the color data. - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3*sizeof(float))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3*sizeof(float))); glEnableVertexAttribArray(1); + // Process the texture coord data. + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float))); + glEnableVertexAttribArray(2); + // Unbind the current buffers and arrays from the state. glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -158,6 +167,25 @@ int main() { GlRenderObjects* triangleRenderObjs = triangle(); GlRenderObjects* rectangleRenderObjs = rectangle(); + unsigned int texture; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + { + Image texture(std::format("{}/container.jpg", utilities::getCurrentPath()).c_str()); + if (texture.data) { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture.width, texture.height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture.data); + glGenerateMipmap(GL_TEXTURE_2D); + } else { + std::cout << "Failed to load texture." << std::endl; + } + } + // Create a render loop, which keeps the program open until glfw tells the loop that the window should close. while (!glfwWindowShouldClose(window)) { // Process input. diff --git a/src/meson.build b/src/meson.build index 69cf8c1..2b665cb 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,14 +1,15 @@ sources = [ 'main.cc', 'shader.cc', - 'utilities.cc' + 'utilities.cc', + 'image.cc' ] copy = find_program('cp') fragment = custom_target('fragment', output: 'fragment.glsl', input: 'fragment.glsl', command: [copy, '@INPUT@', '@OUTPUT@']) vertex = custom_target('vertex', output: 'vertex.glsl', input: 'vertex.glsl', command: [copy, '@INPUT@', '@OUTPUT@']) +container = custom_target('container', output: 'container.jpg', input: 'container.jpg', command: [copy, '@INPUT@', '@OUTPUT@']) - -executable('main', [sources] + [fragment, vertex], dependencies: [glfw, libepoxy]) +executable('main', [sources] + [fragment, vertex, container], dependencies: [glfw, libepoxy]) executable('fixedfunction', ['fixedfunction.cc'], dependencies: [glfw, libepoxy]) diff --git a/src/vertex.glsl b/src/vertex.glsl index b67dfce..e5f864f 100644 --- a/src/vertex.glsl +++ b/src/vertex.glsl @@ -2,9 +2,13 @@ #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; + out vec3 ourColor; +out vec2 TexCoord; void main() { gl_Position = vec4(aPos, 1.0); ourColor = aColor; + TexCoord = aTexCoord; }