Converting transparent pngs Crashes SDL/OpenGL - c++

If I try to convert a transparent .png SDL_Surface to a OpenGL texture, then the moment the program tries to access any data from the SDL_Surface, it crashes. I observed other people's code involving transparent pngs, but they are the same and don't crash, according to other askers. This is my code, the part where the program crashes. It crashes at "n0fColors = surface->format->BytesPerPixel".
GLuint CONVERT_IMAGE(SDL_Surface * surface)
{
GLuint texture;
GLenum texture_format;
GLint nOfColors;
nOfColors = surface->format->BytesPerPixel;
if (nOfColors == 4)
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
}
else if (nOfColors == 3)
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );
delete surface;
return texture;
};
EDIT: I should note that the transparent parts of the PNG is %100 transparent.
EDIT: This is my call to my images:
void image_init()
{
wall_clay_0 = IMG_Load("images/walls/clay.png");
walls_image.clay = CONVERT_IMAGE(wall_clay_0);
};

Your loading code should be like this:
void image_init()
{
wall_clay_0 = IMG_Load("images/walls/clay.png");
if (!wall_clay_0)
{
printf("Error loading mage: %s\n", IMG_GetError());
abort(); // or whatever your application should do
}
walls_image.clay = CONVERT_IMAGE(wall_clay_0);
}

I've figured it out. I needed to make all my loaded images temporary surfaces, then make it equal to SDL_DisplayFormatAlpha of itself. For example:
SDL_Surface * surface;
surface = IMG_Load(bla.png);
surface = SDL_DisplayFormatAlpha(surface);

Related

OpenGL 4.1 and lower Black Texture, Mac and Windows

I had this problem when compiling my OpenGL code on lower-end PC's that don't support OpenGL 4.5 and macs. In my regular code, I would use functions like glCreateTextures and glTextureStorage2D, but they are not supported in other versions so I went the other glGenTextures path.
Here's the image generation code:
Texture::Texture(const std::string& path)
: m_Path(path)
{
int width, height, channels;
stbi_set_flip_vertically_on_load(1);
unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, 0);
RW_CORE_ASSERT(data, "Failed to load image!");
m_Width = width;
m_Height = height;
GLenum internalFormat = 0, dataFormat = 0;
if (channels == 4)
{
internalFormat = GL_RGBA8;
dataFormat = GL_RGBA;
}
else if (channels == 3)
{
internalFormat = GL_RGB8;
dataFormat = GL_RGB;
}
m_InternalFormat = internalFormat;
m_DataFormat = dataFormat;
RW_CORE_ASSERT(internalFormat & dataFormat, "Format not supported!");
glGenTextures(1, &m_ID);
glBindTexture(GL_TEXTURE_2D, m_ID);
glTexParameteri(m_ID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(m_ID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(m_ID, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(m_ID, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(m_ID, 1, internalFormat, m_Width, m_Height, 0, dataFormat, GL_UNSIGNED_BYTE, data);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(data);
}
And I want to bind my textures to specific slots on the GPU so I have this function:
void Texture::Bind(uint32_t slot) const
{
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_2D, m_ID);
}
Here's the screenshot of what gets drawn:
To make sure it wasn't a rendering problem I decided to put it into ImGui's renderer.
And here's the picture I am supposed to get:
And image imports correctly, I get no errors and same importing code and paths work on higher-end PC and the only thing that's changed is that the higher-end PC has OpenGL 4.5 texture generation code.
It turns out I had to specify GL_TEXTURE_2D in these places instead of texture ID:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 1, internalFormat, m_Width, m_Height, 0, dataFormat, GL_UNSIGNED_BYTE, data);

which image format takes less time to load into memory

I have a requirement where i need to load a sequence of images to the memory and than play them back to back.
I load all of the files into a std::vector and after they all get loaded i play them.
this is code for loading each file.
void loadTextureFromFile(const GLchar *file)
{
// Create Texture object
Texture2D texture;
// Load image
int width, height, channels;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
unsigned char* image = SOIL_load_image(file, &width, &height, &channels, SOIL_LOAD_AUTO);
// Set The Internal Format
if (channels > 3)
{
texture.Internal_Format = GL_RGBA;
texture.Image_Format = GL_RGBA;
}
else
{
texture.Internal_Format = GL_RGB;
texture.Image_Format = GL_RGB;
}
// Now generate texture
texture.Generate(width, height, image);
imageSequence.push_back(texture);
// And finally free image data
SOIL_free_image_data(image);
}
////////////////////////////////////////////////////////
Texture2D::Texture2D()
: Width(0), Height(0), Internal_Format(GL_RGB), Image_Format(GL_RGB), Wrap_S(GL_REPEAT), Wrap_T(GL_REPEAT), Filter_Min(GL_LINEAR), Filter_Max(GL_LINEAR) , WrapMode(SumTextureDecl::TextureWrapMode::Repeat)
{
glGenTextures(1, &this->ID);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
void Texture2D::Generate(GLuint width, GLuint height, unsigned char* data)
{
float aniso = 0.0f;
this->Width = width;
this->Height = height;
glBindTexture(GL_TEXTURE_2D, this->ID);
// Set Texture wrap and filter modes
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);
// Create Texture
glTexImage2D(GL_TEXTURE_2D, 0, this->Internal_Format, width, height, 0, this->Image_Format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->Wrap_S);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->Wrap_T);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
// Unbind texture
glBindTexture(GL_TEXTURE_2D, 0);
}
it takes some time to load all the files of image sequence so what file format would load the fastest ?
I am using soil to load images by using some other library would the loading become more fast ?

Setting a texture on only one object in scene

I'm new to opengl and I have this puppet (head,torso,legs,arms) and I would like to set the texture on only the torso part of this puppet.
I was under the assumption that before I draw the torso portion of my puppet I can simply call glEnable(GL_TEXTURE_2D) and after drawing call glDisable(GL_TEXTURE_2D), but I seem to be getting a GL_INVALID_ENUM error.
// Code for loading textures called in an init function at startup
void loadTextures()
{
// load and create a texture
// -------------------------
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
unsigned char *data = stbi_load("container.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
// Code for drawing the puppet
void A3::renderNode(const SceneNode * root, mat4 view, mat4 model) {
if (root == nullptr) return;
mat4 tmp_model = model * root->get_transform();
if (root->m_nodeType == NodeType::GeometryNode) {
const GeometryNode * geometryNode = static_cast<const GeometryNode *>(root);
updateShaderUniforms(m_shader, *geometryNode, view, tmp_model , picking);
// Get the BatchInfo corresponding to the GeometryNode's unique MeshId.
BatchInfo batchInfo = m_batchInfoMap[geometryNode->meshId];
m_shader.enable();
if (root->m_name == "torso")
glEnable(GL_TEXTURE_2D);
glDrawArrays(GL_TRIANGLES, batchInfo.startIndex, batchInfo.numIndices);
glDisable(GL_TEXTURE_2D);
m_shader.disable();
}
for (const SceneNode * node : root->children) {
renderNode(node, view, tmp_model); // recursion
}
}

How to properly load an image to use as an OpenGL texture?

I am trying to load an image into an OpenGL texture using SOIL2; however, it never seems to be correct unless I use SOIL2's load to texture function. I have tried using STB image and Devil, but both get similar results.
Code:
GLuint load_image(const std::string& path) {
int iwidth, iheight, channels;
unsigned char* image = SOIL_load_image(path.c_str(), &iwidth, &iheight, &channels, SOIL_LOAD_RGBA);
// std::cout << SOIL_last_result() << std::endl;
// float* image = stbi_loadf(path.c_str(), &iwidth, &iheight, &channels, STBI_rgb_alpha);
// if(!ilLoadImage(path.c_str()))
// std::cout << "Devil Failed to load image: " << iluErrorString(ilGetError()) << std::endl;
//
// unsigned char* image = ilGetData();
//
// int iwidth = ilGetInteger(IL_IMAGE_WIDTH);
// int iheight = ilGetInteger(IL_IMAGE_HEIGHT);
// int channels = ilGetInteger(IL_IMAGE_CHANNELS);
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0 + texture);
GLint old_unpack_alignment;
glGetIntegerv(GL_UNPACK_ALIGNMENT, &old_unpack_alignment);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, texture);
glCheckError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glCheckError();
GLenum original_format = (channels == 4 ? GL_RGBA : GL_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT, old_unpack_alignment);
return texture;
}
Screenshot:
What I should get:
I would like to know how to properly load an image into a texture.
Here is an example of what my texture loading function looks like:
unsigned int loadTexture(char const * path)
{
unsigned int textureID;
glGenTextures(1, &textureID);
int width, height, nrComponents;
unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
if (data)
{
GLenum format;
if (nrComponents == 1)
format = GL_RED;
else if (nrComponents == 3)
format = GL_RGB;
else if (nrComponents == 4)
format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(data);
}
else
{
std::cout << "Texture failed to load at path: " << path << std::endl;
stbi_image_free(data);
}
return textureID
}
I will usually set up my VAO & VBO before hand, then I'll use this to load in a texture. After this I'll configure my shader(s) for use, then within the render loop is where I'll use my shader, set the matrices passing in any of the needed uniforms, then after all the "model" information is completed I'll finally bind the VertexArrays, set the approrpriate texture to Active, then Bind those Texture(s) and fish up with drawing the arrays or primitives.

OpenGL loading binds the last loaded texture to all textureID's

So my problem is that I try to load multiple textures at start up and then store all of the ID's so that I can bind them to use them. Now I know that the ID's are being stored correctly because I can debug it and see in both assigning and usage that the ID's are the same. Just for every binding it uses the last texture that I load. Here is my code:
GLuint TextureLoader::LoadTexture (const char* fileName,Material& material,int width,int height) {
GLuint textureImage;
FILE* textureFile;
textureFile = fopen(fileName, "rb");
unsigned char* imageData;
if (textureFile == NULL) {
return 0;
}
imageData = (unsigned char*)malloc(width * height * 3);
char header[54];
fread(header,1,54,textureFile);
fread(imageData, width * height * 3, 1, textureFile);
fclose(textureFile);
for (int i = 0; i < width * height; ++i) {
int nextIndex = i * 3;
unsigned char a = imageData[nextIndex];
unsigned char b = imageData[nextIndex+2];
imageData[nextIndex] = b;
imageData[nextIndex+2] = a;
}
glEnable(GL_TEXTURE_2D);
glGenTextures( 1, &textureImage );
glBindTexture( GL_TEXTURE_2D, textureImage );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
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);
/*glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, imageData );*/
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, imageData);
glGenerateMipmap(GL_TEXTURE_2D);
free(imageData);
return textureImage;
}
Here is my usage of this code:
if (showTexture) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, material.texture);
}
Here is my load call:
green1.texture = TextureLoader::LoadTexture("/Users/hewitt/Desktop/OpenGLImages/face6.bmp",green1, 256, 256);
And then I just use:
glTexCoord2f(1.0f,0.0f);
to draw depending on the corner it changes. And this draws the one texture correctly it just repeats the texture even when I glBind a different number.
Any help is greatly appreciated.
Edit ----------
glBegin(mode);
Spatial::Vector3 normal = Mesh::calculateNormal (pointGroup);
glNormal3f(normal.x, normal.y, normal.z);
Material material = mesh.getMaterial();
if (showTexture) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, material.texture);
}
int counter = 0;
for (Spatial::Vector3 point : pointGroup) {
if (showTexture == false) {
Material::Colour colour = material.getColour();
glColor3f(colour.red, colour.green, colour.blue);
} else {
if (counter == 0) {
glTexCoord2f(1.0f,0.0f);
} else if (counter == 1) {
glTexCoord2f(1.0f,1.0f);
} else if (counter == 2) {
glTexCoord2f(0.0f,1.0f);
} else if (counter == 3) {
glTexCoord2f(0.0f,0.0f);
}
}
glVertex3f(point.x,point.y,point.z);
counter ++;
}
glEnd();
You cannot call glEnable or glBindTexture while in between glBegin and glEnd. You must bind your texture before glBegin. In fact the set of OpenGL functions you can call between glBegin and glEnd is limited to those transferring vertex attributes (glVertex, glColor, glTexCoord, etc...).
Note that the immediate mode API (that is glBegin/glEnd and friends) is legacy and was deprecated for more than 10 years. I suggest you switch to the modern OpenGL pipeline, that will sort many things out for you.