I checked everything multiple times, and didn’t find any of the common problems i found on stackoverflow, reddit, and stackexchange. I’m trying just to render a rectangle with a texture at the moment, but it is invisible (if i try to use a basic shader the rectangle shows up correctly).
Here’s the code:
main.cpp:
int main(int argc, char** argv)
{
...
texture grass_texture;
grass_texture.load(textures_path + "grass_tile.png", 0);
renderable r_plane;
shape s_plane;
shape_maker::rectangle(s_plane, 10, 10);
s_plane.to_renderable(r_plane);
...
while (!glfwWindowShouldClose(window) && !quit)
{
...
glUseProgram(texture_shader.program);
glBindTexture(GL_TEXTURE_2D, grass_texture.id);
glUniformMatrix4fv(texture_shader["uModel"], 1, GL_FALSE, &stack.m()[0][0]);
glUniform1i(texture_shader["uColorImage"], 0);
r_plane.bind();
glDrawElements(r_plane().mode, r_plane().count, r_plane().itype, 0);
glBindTexture(GL_TEXTURE_2D, 0);
...
}
texture.h:
#pragma once
#include <GL/glew.h>
#include <string>
#include <filesystem>
struct texture {
texture() { }
~texture() { }
int x_size, y_size;
int n_components;
GLuint id;
GLuint load(std::string name, GLuint texture_unit)
{
unsigned char * data;
data = stbi_load(name.c_str(), &x_size, &y_size, &n_components, 0);
if (data == NULL)
{
std::cout << stbi_failure_reason() << std::endl << name << std::endl;
perror("Error");
}
stbi__vertical_flip(data, x_size, y_size, n_components);
glActiveTexture(GL_TEXTURE0 + texture_unit);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
int channels;
switch (n_components)
{
case 1: channels = GL_RED; break;
case 3: channels = GL_RGB; break;
case 4: channels = GL_RGBA; break;
default: assert(0);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, x_size, y_size, 0, channels, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
return id;
}
...
texture.vert:
#version 460 core
layout (location = 0) in vec3 aPosition;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec3 aNormal;
layout (location = 3) in vec3 aTangent;
layout (location = 4) in vec2 aTexCoord;
out vec2 vTexCoord;
out vec3 vColor;
uniform mat4 uProj;
uniform mat4 uView;
uniform mat4 uModel;
void main(void)
{
vColor = aColor;
vTexCoord = aTexCoord;
gl_Position = uProj*uView*uModel*vec4(aPosition, 1.0);
}
texture.frag:
#version 460 core
out vec4 color;
in vec2 vTexCoord;
in vec3 vColor;
uniform sampler2D uColorImage;
void main(void)
{
color = texture2D(uColorImage,vTexCoord);
}
The classes and structs texture
, renderable
, shape
and shape_maker
are made by my professor, and they worked in other past contexts. The path is 100% correct. It’s not a very small project so i tried to put only the important code, if you need anything else to help me just ask.