Hi all,
So, I've come again, hat in hand, to be pointed in the right direction
I've scoured the forums(and quite a bit of the internet-realm) but can only seem to find out of date ways of handling this. I apologize if this is a repeat question, but all of the threads I've found on this are quite old, or at least old enough they don't seem to apply.
I'm needing to draw about 100 different objects with about fifteen different images or so. Instead of loading 15 textures for each of the 15 images, I'm trying to load one texture containing many images and change the texture coordinates to get the right image when needed. Basically, I'm trying to create a sprite sheet
My first question, has the method of changing the texture coordinates changed since :
glBindTexture(GL_TEXTURE_2D, 0);
glBegin(GL_QUADS)
whole_bunch_of_coordinates_below(0.0, 0.1)etc
The above method is deprecated (or removed?) for some time now, correct?
I have a feeling I need to be cropping the coordinates in one of the shaders, but the only image information being passed into relevant to the image is the texture sampler (i think) and I'm unsure how to go about changing the coordinates at that point or what to apply them to.
I've started using an SFML texture instead of freeimage for ease of use.
as of now, my loading function looks thusly:
GLuint Object::LoadImage(const char* imageName)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GLuint TextureID;
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);
image.loadFromFile(imageName);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return TextureID;
}
the previous freeimage load function:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
FREE_IMAGE_FORMAT formato =
FreeImage_GetFileType(imageName,0);
FIBITMAP* imagen = FreeImage_Load(formato, imageName);
int w = FreeImage_GetWidth(imagen);
int h = FreeImage_GetHeight(imagen);
textura = new GLubyte[4*w*h];
char* pixels = (char*)FreeImage_GetBits(imagen);
for(int j= 0; j<w*h; j++){
textura[j*4+0]= pixels[j*4+2];
textura[j*4+1]= pixels[j*4+1];
textura[j*4+2]= pixels[j*4+0];
textura[j*4+3]= pixels[j*4+3];
}
GLuint TextureID;
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0,
GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)textura );
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
apologies for the missing headers, they're rather bogged down with irrelevant things, so to save space I've left them out. The "image" from the first example is a "sf::Image" and the "textura" from the second is "GLubyte*".
I've tried the obvious things(at least to someone entirely new at this) such as trying to change the height and width of the image at load time.
This only distorts the image(which in hindsight should have been reeeeeaaally obvious).
I've also tried creating the coordinates and passing them into the vertex shader via the "in" commands but am really failing at understanding when the texture is actually used with the object or how I could change these after the fact. I am definitely missing something in my understanding of textures and shaders here.
Here is some probably relevant code, for clarity, I've cut out quite a bit that isn't relevant to the texture or object. I'm fairly certain it was nothing relevant to this problem:
In the object creation function:
Object::Object(const char* image,const char* image2, const char* objHere, glm::vec3 place,GLuint programID, int objtypes)
{
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
MatrixID = glGetUniformLocation(programID, "MVP");
ViewMatrixID = glGetUniformLocation(programID, "V");
ModelMatrixID = glGetUniformLocation(programID, "M");
PlaneMatrixID = glGetUniformLocation(programID, "P");
Texture = LoadImage(image);
loadOBJ(objHere, vertices, uvs, normals,place);
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
glGenBuffers(1, &normalbuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
glUseProgram(programID);
LightID = glGetUniformLocation(programID, "LightPosition_worldspace");
}
Let me know if you'd like the render function or the shaders posted.
So, to clarify my question,
if the first example is deprecated, through what method does one change the texture coordinates in opengl 4.40?
Feel free to point me to a resource instead of explaining everything to me (though that is always welcome if you're the patient type) While I've found several good (and more importantly, current) resources, they all seem to fail to mention this little problem of mine. But, so much information on the internet relating to opengl seems woefully outdated that I've struggled to find a solution to this.
And, while I did search this forum pretty thoroughly, everything I found was from very old threads. If one still applies, or I failed in searching the correct thing, feel free to call me an idiot and link to the correct forum
Thanks in advance for any help,
Cheers,