trying to use SFML to load images and to use with OpenGL textures, but when I draw the quad its just black.
I know the UV is right so im pretty sure its the texture its self.
The main problem is im not sure how I should be using sf::Texture in an openGL way
#include "./3dSprite.hpp"
#include "./Engine.hpp"
d3Sprite::d3Sprite(Engine *mEngine)
{
engine = mEngine;
shader = new Shader("./Data/vert.vert" ,"./Data/frag.frag");
UVList[0] = 1.0f;
UVList[1] = 1.0f;
UVList[2] = 0.0f;
UVList[3] = 1.0f;
UVList[4] = 0.0f;
UVList[5] = 0.0f;
UVList[6] = 1.0f;
UVList[7] = 1.0f;
UVList[8] = 0.0f;
UVList[9] = 0.0f;
UVList[10] = 1.0f;
UVList[11] = 0.0f;
vertexList[0] = 0.5f;
vertexList[1] = 1.0f;
vertexList[2] = 0.0f;
vertexList[3] = -0.5f;
vertexList[4] = 1.0f;
vertexList[5] = 0.0f;
vertexList[6] = -0.5f;
vertexList[7] = 0.0f;
vertexList[8] = 0.0f;
vertexList[9] = 0.5f;
vertexList[10] = 1.0f;
vertexList[11] = 0.0f;
vertexList[12] = -0.5f;
vertexList[13] = 0.0f;
vertexList[14] = 0.0f;
vertexList[15] = 0.5f;
vertexList[16] = 0.0f;
vertexList[17] = 0.0f;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexList), vertexList, GL_STATIC_DRAW);
MatrixID = glGetUniformLocation(shader->shaderID, "MVP");
positionID = glGetAttribLocation(shader->shaderID, "Position");
TextureID = glGetUniformLocation(shader->shaderID, "myTexture");
UVID = glGetAttribLocation(shader->shaderID, "UV");
//std::cout << MatrixID << ":" << positionID << ":" << TextureID << ":" << UVID << std::endl;
//TextureID prints out 65526 for some reason
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(UVList), UVList, GL_STATIC_DRAW);
glGenTextures(1, &texturebuffer);
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, texturebuffer);
// Give the image to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, TextureResourceManager::textureResourceManager.getFile("./Data/Pedro.png").GetWidth(), TextureResourceManager::textureResourceManager.getFile("./Data/Pedro.png").GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,TextureResourceManager::textureResourceManager.getFile("./Data/Pedro.png").CopyToImage().GetPixelsPtr() );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
d3Sprite::~d3Sprite()
{
// Cleanup VBO and shader
glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &uvbuffer);
glDeleteTextures(1, &texturebuffer);
delete shader;
}
void d3Sprite::Initiate(glm::mat4 *persMat, glm::mat4 *camMat)
{
*camMat = glm::lookAt(
glm::vec3( 0, 0, 5 ), // Camera is here glm::vec3( 0, 0, 5 );
glm::vec3( 0, 0, 0 ), // and looks here : at the same position, plus "direction"
glm::vec3( 0, 1, 0 ) // Head is up (set to 0,-1,0 to look upside-down)
);
Drawable::Initiate(persMat, camMat);
}
void d3Sprite::Update(float deltaTime)
{
Drawable::Update();
}
void d3Sprite::Draw()
{
glUseProgram(shader->shaderID);
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texturebuffer);
glUniform1i(TextureID, 0);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(positionID);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
positionID, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(UVID);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(
UVID, // attribute. No particular reason for 1, but must match the layout in the shader.
2, // size : U+V => 2
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
// For each model you render, since the MVP will be different (at least the M part)
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 6); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(UVID);
glDisableVertexAttribArray(positionID);
}