Sign in to follow this  
snowfell

OpenGL Importing bitmaps then using them

Recommended Posts

snowfell    122
//SOLVED// I have been troubled with this opengl/C++ issue for sometime and I could use some help. I was able to get started on one a function to import and use bitmaps. I spent a week trying to find info on such a subject and very little seem helpful. I know that to import a bitmap into a program (C++,windows, opengl) you first need to tell the program to open the file, then gather info from it, then convert it to bytes so it can be used. That data is then bind to the program for further use. My issue is this: I've been having issues with the whole converting to bytes part of the process. I believe that the importing and opengl parts are correct but I know that the BYTE part is incomplete and I dont know where to go next. I temporarily put texture_obj in glTexImage2d as the last argument, I know that the byte info should go there not this. Any tips or advise? I am really stumped and could use some help. Just looking for a simple way of importing and using .bmp files in a opengl application. Thanks EDIT revised code:
bool ImportBMP(char *filename, int num_list)
{
     AUX_RGBImageRec * Tx=auxDIBImageLoad(filename);
     
     if (!Tx)
     {
          return false;  
     }
     
     glGenTextures(1,&texture_lib[num_list]);
     glBindTexture(GL_TEXTURE_2D,texture_lib[num_list]);
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); 
     glTexImage2D(GL_TEXTURE_2D, 0, 3, Tx->sizeX, Tx->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Tx->data);
     
     if (Tx)
     {
	      if (Tx->data) 
          {
               free(Tx->data);
          }
	      free(Tx);
     } 
     return true;
}


[Edited by - snowfell on August 13, 2008 7:59:35 PM]

Share this post


Link to post
Share on other sites
davek20    127
Well, you could always use an image library such as DevIL or SOIL. Those two are always suggested in many threads. I believe they support all common formats such as BMP, PNG, GIF, JPEG, etc.

Of course if you want to write your own, then take a look at the BMP file format Some things I remember from the BMP format is that the red and blue channels are swapped, so instead of RGB its actually BGR. The image is also upside down.

Here is my code for loading a BMP (C++):


unsigned char* BitmapLoader::Load(std::string filename){
std::ifstream file(filename.c_str(), std::ios::binary);

//if the file was opened successfully, read the data
if(file.is_open()){

//read the offset value to the data portion
unsigned int offset = 0;
file.seekg(10, std::ios::beg);
file.read((char*)&offset, sizeof(unsigned int));

//read the width and height of the image
file.seekg(18, std::ios::beg);
file.read((char*)&width, sizeof(unsigned int));
file.read((char*)&height, sizeof(unsigned int));

//create the image array
data = new unsigned char[height * width * 3];

//reposition the current file position to the image data
file.seekg(offset, std::ios::beg);

//read the image data
for(unsigned int i=0; i<height; i++){
for(unsigned int j=0; j<width*3; j+=3){
//read the R,G,B values (actually BGR)
unsigned char colour[3];
file.read((char *)&colour[0], 3 * sizeof(char));

data[i*width*3 + j] = colour[2]; //red
data[i*width*3 + j+1] = colour[1]; //green
data[i*width*3 + j+2] = colour[0]; //blue
}
}

//close the file
file.close();

return data;
}
//else, the file didn't open successfully
else{
return NULL;
}
}

[/SOURCE]



Share this post


Link to post
Share on other sites
empirical2    567
Bind to the texture you want to load into then....


AUX_RGBImageRec * Tx=auxDIBImageLoad(*FileName);
if (!Tx) return 0;
glTexImage2D(GL_TEXTURE_2D, 0, 3, Tx->sizeX, Tx->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Tx->data);

if (Tx)
{
if (Tx->data) free(Tx->data);
free(Tx);
}

glEnable(GL_TEXTURE_2D);


Share this post


Link to post
Share on other sites
snowfell    122
Its getting their but not working correctly yet :( Ok I added a few changes are recommended above, but the code still isn't working. Good thing (and sorta a bad thing) is that my revised code gives back no errors. But the textured objects remain white. I tried a few different things (using different textures, reading files from a different folder, ect) but none seemed to help. I am sure their must be one minor mistake somewhere here, and its preventing the rest from working. Could it be that one of the opengl commands arent binding the texture to texture_lib (a GLuint variable that should be holding my texture data).

bool ImportBMP(char *filename, int num_list)
{
AUX_RGBImageRec * Tx=auxDIBImageLoad(filename);

if (!Tx)
{
return false;
}

glGenTextures(1,&texture_lib[num_list]);
glBindTexture(GL_TEXTURE_2D,texture_lib[num_list]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, 3, Tx->sizeX, Tx->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Tx->data);

if (Tx)
{
if (Tx->data)
{
free(Tx->data);
}
free(Tx);
}
return true;
}


[Edited by - snowfell on August 13, 2008 10:31:49 AM]

Share this post


Link to post
Share on other sites
davek20    127
The only thing that I can think of is that you are not calling glEnable(GL_TEXTURE_2D) anywehere. And make sure that you call glBindTexture(GL_TEXTURE_2D, texture_lib[num_list]) before you start drawing.

If you think that some of the GL calls are failing, you can call glGetError() to see if they are failing.

Share this post


Link to post
Share on other sites
snowfell    122
Nope thats not it, I have glEnable(GL_TEXTURE_2D) right with the rest of my glEnables. And I have glBindTexture right before my build functions so I really doubt thats the problem. Any other things that could cause this problem? Are you sure that actual importing code and everything is correct? Please let me know of anything else that could be wrong or that I could check, I really need to get this working correctly.

Share this post


Link to post
Share on other sites
empirical2    567
You must bind to the texture first or it wont know which named texture your loading to.

1. Create a name
2. Bind to the texture
3. Do the glTexImage2D to copy your picture to the texture memory.
4. ENABLE textures.(Seems to require this to be recalled after loading)
5. Profit.


Also remember basic implementations must have the texture's width and height being one of the following values
2,4,8,16,32,64,128,256,512,1024,etc

(Width does not need to equal height)

Share this post


Link to post
Share on other sites
snowfell    122
Well I believe I have meet all the requirements above and still nothing... just white polygons. Are you guys sure that the code above is foolproof? I mean I not getting any error, and I know that the textures are being read, I just don't know if its successfully getting the date from the bmp files.

If their is any other information I need to give please let me know, I am in dire need of a answer here.

Should I be running this function as a bool type? Should I be using void or int?

[Edited by - snowfell on August 13, 2008 3:52:06 PM]

Share this post


Link to post
Share on other sites
empirical2    567
Ok this code below works for me. I just ran it in a test environment.

Some notes.
The order in which things done is clearly important since it didnt work when I tried your code above.

As well as the order I changed the texture name to a constant 1. This is because I use my own name generator (which is bad btw :) ) So i am not certain about your num_list thing.

It is important to remember to bind to this texture each frame and must be done outside of glBegin and glEnd.

Try this with one texture when you get it to work re-enable the name generator that way if it then breaks you know thats the problem. :)


bool ImportBMP(char *filename, int num_list)
{
glBindTexture(GL_TEXTURE_2D, 1);
AUX_RGBImageRec * Tx=auxDIBImageLoad(filename);
if (!Tx) {return 0;}
glTexImage2D(GL_TEXTURE_2D, 0, 3, Tx->sizeX, Tx->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Tx->data);

if (Tx)
{
if (Tx->data) free(Tx->data);
free(Tx);
}

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glEnable(GL_TEXTURE_2D);
return true;
}

Share this post


Link to post
Share on other sites
snowfell    122
Dude now I got it running, I have no idea why it wasn't working before. Thanks tons empirical2 and everyone else. This was a big issue, now its fixed :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
      Thanks,
       
  • Popular Now