As you can see in the linked image, despite assigning a texture unit, there is no texture being drawn on the screen. In the main loop, I am receiving error 1282, and there are no further errors throughout the code, I have checked with sporadic calls to the error checking function.
I am using an M2 Mac, could that be a root cause?
#include <glad/glad.h> // GLAD helper library for OpenGL function pointer help
#include <GLFW/glfw3.h> // GLFW helper library for window management
#include <iostream>
#include "shaders/Shader.h"
#include "Textures/Texture.h"
#define STB_IMAGE_IMPLEMENTATION
#include "ext/stb_image.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
/* Default screen size values */
const unsigned int SCR_HEIGHT = 600;
const unsigned int SCR_WIDTH = 800;
int main() {
/* configure window and set glfw/opengl settings */
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // macOS specific apparently?
/* create window object */
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Learn-OpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
/* init GLAD before using any opengl functions */
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { // OS specific location of function pointers
std::cout << "Failed to init GLAD" << std::endl;
return -1;
}
/* init viewport prior to rendering -> makes it so screen coords work */
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT); // separate to GLFW window size so make them equal!
/* callback to allow for user window resizing */
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
/* Set up our shader */
Shader shader("shaders/vertexShading.glsl", "shaders/fragmentShading.glsl");
/* Set up our texture */
Texture texture("../res/container.jpg");
/* Define x,y,z positions of each vertex on our vertices.
Normalised device coords -> always between -1 and 1 */
float vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
/* VBO holds the actual vertex data.
VAO keeps track of how to use the vertex data from a VBO.
EBO specifies the order in which the vertices should be connected to form shapes. */
unsigned int VBOs[2], VAOs[2];
glGenBuffers(2, VBOs);
glGenVertexArrays(2, VAOs);
/* vertices data */
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBindVertexArray(VAOs[0]);
/* copy vertex data into the buffer's memory */
/* static draw since data is set once and used many times. Use dynamic if the data changes a lot */
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
/* how we should interpret vertex data per vertex attribute */
// position attr
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); // we set location as 0 in our shader code
// color attr
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float))); // offset of size 3 floats
glEnableVertexAttribArray(1); // we set location as 1 in our shader code
// texture attr
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6*sizeof(float)));
glEnableVertexAttribArray(2);
shader.use();
shader.setUniformInt("ourTexture", 0);
/* window loop */
while(!glfwWindowShouldClose(window)) {
// input -------------------------------------------------------------------------------------------------------
processInput(window);
// rendering ---------------------------------------------------------------------------------------------------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); // greenish
glClear(GL_COLOR_BUFFER_BIT); // clear colour buffer
/* bind texture data */
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture.getID());
shader.use();
/* draw vertices */
glBindVertexArray(VAOs[0]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// unbind buffers ----------------------------------------------------------------------------------------------
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// check and call events + swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window) {
/* ability to close window */
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
/* ability to swap between fill and wireframe modes -> hold w key */
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
} else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
}
//
// Created by Henry Fielding on 23/8/2023.
//
#include "Texture.h"
Texture::Texture(const char* fp) {
/* load in texture file and set values */
data = stbi_load(fp, &width, &height, &numChannels, 0);
/* generate texture id */
glGenTextures(1, &ID);
/* bind it so any subsequent GL_TEXTURE_2D commands apply to it */
glBindTexture(GL_TEXTURE_2D, ID);
/* configure texture wrapping and filtering options. changes behaviour view docs to see differences */
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);
/* ... actually generate the texture */
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cerr << "FAILED TO LOAD TEXTURE" << std::endl;
}
/* free image data */
stbi_image_free(data);
}
unsigned int Texture::getID() {
return ID;
}
#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;
}
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
void main()
{
FragColor = texture(ourTexture, TexCoord);
}
```
