i dont see anywhere what “Texture::Bind(0)” actually does.
//Draws the mesh.
void Mesh:Draw()
{
glBindVertexArray(_VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _EBO);
_Texture.Bind(0);
...
just to reiterate:
to draw a cube in a transformed position/orientation
glUseProgram(myprogram);
glBindVertexArray(myvao);
mat4 mymatrix = mat4(1);//or some other transform * view * perspective
glUniformMatrix4fv(location, 1, GL_FALSE, value_ptr(mymatrix));
glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_BYTE/*or shhort/int*/, nullptr);
//glBindVertexArray(0);
//glUseProgram(0);
thats all, to do texturing, your fragmentshader needs a “uniform sampler2D mytexture;”. in addition, you c/c++ code needs to tell the “myprogram” from what texture unit (lets say 3) it is sourced:
GLint location = glGetUniformLocation(myprogram, "mytexture");
if (location == -1)
/* handle error*/;
else
glUniform1i(location, 3);
and right before drawing (that is the “glDraw*whatever” command), you bind the necessary texture to that unit:
glBindTextureUnit(3, sometexture);
then your cube will be textured according to the texcoords that you’ve sent to the fragmentshader. note that you dont need to bind (again) the element buffer or the vertex buffer since that info is stored in the VAO. take a look at this example, or read any of the tutorials found on the internet.
thats part 1
part 2: texture data
if stbi_load(…) doen NOT return 0, then it has succeded, so you have to generate a texture, allocate memory for it and fill it with that data, as usual by calling:
GLuint texture = 0;
glGenTextures(1, &tetxure);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, ... etc ...);
for testing purposes, you can skip the loading part and just set a 2x2 texture with 4 different texel colors:
struct Texel {
unsigned char r, g, b, a;
};
Texel texturedata[] = {
// blue yellow
{ 0x00, 0x00, 0xFF, 0xFF }, { 0xFF, 0xFF, 0x00, 0xFF },
// red green
{ 0xFF, 0x00, 0x00, 0xFF }, { 0x00, 0xFF, 0x00, 0xFF },
};