Archived

This topic is now archived and is closed to further replies.

spiffgq

OpenGL Create a texture from a [OpenGL-type] bitmap

Recommended Posts

spiffgq    122
I''m trying to create a texture object in OpenGL from a bitmap. (And by bitmap, I mean a true one-bit-per-pixel OpenGL type bitmap, not that Microsoft abomination file type.) My first thought was to just use glTexImage2D as follows:
  
const unsigned char BITMAP_DATA [] = { /* pixels are here */ };
const int BITMAP_PITCH = 1; // Number of bytes per row

const int BITMAP_ROWS = 12; // Number of rows (the height)

const int BITMAP_COLS =  8; // Number of pixels per column


// Some code goes here blah blah blah


unsigned int texObj;
glGenTextures (1, &texObj);
	
glBindTexture (GL_TEXTURE_2D, texObj);
glTexImage2D (
   GL_TEXTURE_2D, 
   0, 
   GL_INTENSITY, 
   BITMAP_COLS, 
   BITMAP_ROWS, 
   0, 
   GL_LUMINANCE, 
   GL_BITMAP, 
   BITMAP_DATA
);

// and so on

  
Unfortunately, this doesn''t work. The resulting "texture" is just a solid color and does not contain the bitmap''s pixels.
  
glBindTexture (GL_TEXTURE_2D, texObj);
glBegin (GL_QUADS);
glColor3f (1.0, 0.0, 0.0);
glTexCoord2f (0.0, 1.0); glVertex3f (  0.0f,   0.0f, 0.0f);
glTexCoord2f (1.0, 1.0); glVertex3f (300.0f,   0.0f, 0.0f);
glTexCoord2f (1.0, 0.0); glVertex3f (300.0f, 300.0f, 0.0f);
glTexCoord2f (0.0, 0.0); glVertex3f (  0.0f, 300.0f, 0.0f);
glEnd ();
  
The above code creates a solid red rectangle instead of showing the pixels of the bitmap. Am I on the right track or do I need to go another route completely? If I am on the right track, what am I doing wrong? Thanks for your help.

Share this post


Link to post
Share on other sites
spiffgq    122
quote:
Original post by Subotron
I believe the LUMINANCE is for 8 bits... (black & white)
So I guess that''s the problem indeed


No, I changed the GL_TexImage_2D line from

glTexImage2D (GL_TEXTURE_2D, 0, GL_INTENSITY, BITMAP_COLS, BITMAP_ROWS, 0, GL_LUMINANCE, GL_BITMAP, BITMAP_DATA);


to


glTexImage2D (GL_TEXTURE_2D, 0, GL_INTENSITY, BITMAP_COLS, BITMAP_ROWS, 0, GL_BITMAP, GL_UNSIGNED_BYTE, BITMAP_DATA);


but end up with the same, blank quad.

Here is a complete listing of the code. Perhaps the problem lies somewhere else (for example, maybe I''m forgetting to initialize something).


  
#include <stdio.h>

#ifdef WIN32
# include <windows.h>
#endif

#include <GL/gl.h>

#include <SDL/SDL.h>



#if 0
const unsigned char BITMAP_DATA [] = {
0x41, 0x00,
0x41, 0x00,
0x42, 0x00,
0x22, 0x00,
0x22, 0x00,
0x14, 0x00,
0x14, 0x00,
0x1C, 0x00,
0x0C, 0x00,
0x08, 0x00,
0x08, 0x00,
0x30, 0x00,
};
#endif

//#if 0

const unsigned char BITMAP_DATA [] = {
0x41,
0x41,
0x42,
0x22,
0x22,
0x14,
0x14,
0x1C,
0x0C,
0x08,
0x08,
0x30
};
//#endif


const int BITMAP_PITCH = 1;
const int BITMAP_ROWS = 12;
const int BITMAP_COLS = 8;
const int NUM_PIXELS = BITMAP_PITCH * 8 * BITMAP_ROWS;

const int SCREEN_WIDTH = 900;
const int SCREEN_HEIGHT = 750;

int main () {

// Init SDL

SDL_Init (SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL){ return 0; }
SDL_WM_SetCaption ("Test Bitmap OpenGL Texture Program", NULL);

// Init OpenGL

glViewport (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, -1, 1);
glMatrixMode (GL_MODELVIEW);

glClearColor (0.0, 0.0, 0.0, 0.0);
glClearDepth (1.0);
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LEQUAL);
glPolygonMode (GL_FRONT, GL_FILL);
glPolygonMode (GL_BACK, GL_LINE);

glEnable (GL_TEXTURE_2D);

// Create texture

unsigned int texObj;
glGenTextures (1, &texObj);

if (texObj == 0){
fprintf (stderr, "Unable to create an OpenGL texture object\n");
SDL_Quit();
return 1;
}

glBindTexture (GL_TEXTURE_2D, texObj);
glTexImage2D (
/*target*/ GL_TEXTURE_2D,
/*level*/ 0,
/*internalFormat*/ GL_INTENSITY,
/*width*/ BITMAP_COLS,
/*height*/ BITMAP_ROWS,
/*border*/ 0,
/*format*/ GL_BITMAP,
/*type*/ GL_UNSIGNED_BYTE,
/*texels*/
BITMAP_DATA
);

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

// Show a quad with the texture

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glTranslatef (200.0f, 150.0f, 0.0f);
//glBindTexture (GL_TEXTURE_2D, texObj);

glBegin (GL_QUADS);
glColor3f (1.0, 0.0, 0.0);
glTexCoord2f (0.0, 1.0); glVertex3f ( 0.0f, 0.0f, 0.0f);
glTexCoord2f (1.0, 1.0); glVertex3f (100.0f, 0.0f, 0.0f);
glTexCoord2f (1.0, 0.0); glVertex3f (100.0f, 150.0f, 0.0f);
glTexCoord2f (0.0, 0.0); glVertex3f ( 0.0f, 150.0f, 0.0f);
glEnd ();


SDL_GL_SwapBuffers ();

SDL_Event event;
bool done = false;

while (!done){

SDL_PollEvent (&event);

if (event.type == SDL_QUIT){
done = true;
}else if (event.type == SDL_KEYUP){

if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
}


}


SDL_Quit ();
glDeleteTextures (1, &texObj);


return 0;
}




It is a very simple program that uses SDL as the window manager. It starts, creates the texture, displays the texture, and waits for the user to exit the program. It should compile on Windows and Linux (although it has only been tested on Linux).

Here is a Makefile that will compile the above program (assuming you have SDL and OpenGL libs installed in the same place ):

  
CC=g++
LIBS= -lSDL -L/usr/X11R6/lib -lGL

opengl_test_program: main.o
$(CC) $(LIBS) main.o -o opengl_test_program


Thank you for your help.

Share this post


Link to post
Share on other sites
michel    122
Shouldn''t you bind your texture before you draw code?
Before the quad drawing you put //glBindTexture (GL_TEXTURE_2D, texObj);
Maybe uncommenting it would help?
Not a pro, just a thought :D

Share this post


Link to post
Share on other sites
Brother Bob    10344
You''re using one byte per row in the image, which requires the unpack alignment to be set to 1. The default is 4, which means each new row must start on a 4 byte aligned offset from the start address. I don''t know if this is the problem you are currently experiencing, but with a one byte wide image, it will be a problem sooner or later. Put a glPixelStorei(GL_UNPACK_ALIGNMENT, 1) before uploading the texture.

Either that, or pad the image with enough bytes so that each new row starts on 4 byte aligned offset from the start address.

Share this post


Link to post
Share on other sites
spiffgq    122
quote:
Original post by michel
Shouldn''t you bind your texture before you draw code?
Before the quad drawing you put //glBindTexture (GL_TEXTURE_2D, texObj);
Maybe uncommenting it would help?
Not a pro, just a thought :D


I''ve tried it both ways with similar results.

quote:
Original post by Brother Bob
You''re using one byte per row in the image, which requires the unpack alignment to be set to 1. The default is 4, which means each new row must start on a 4 byte aligned offset from the start address. I don''t know if this is the problem you are currently experiencing, but with a one byte wide image, it will be a problem sooner or later. Put a glPixelStorei(GL_UNPACK_ALIGNMENT, 1) before uploading the texture.

Either that, or pad the image with enough bytes so that each new row starts on 4 byte aligned offset from the start address.



I''ll try that when I get home. To pad the image, should I change the data type to unsigned integer or add three more bytes per row in unsigned chars?

Share this post


Link to post
Share on other sites
Brother Bob    10344
Add three bytes after the one you already have. If you use unsigned ints instead, you have to make sure the information ends up in the correct byte (little vs big endian issues), but it is possible.

Share this post


Link to post
Share on other sites
spiffgq    122
quote:
Original post by CnCMasta
kills this GL_LUMINANCE and put GL_BITMAP on its place.
Then put GL_UNSIGNED_BYTE on the place where GL_BITMAP was!


One thing I don''t understand is according to the OpenGL red book, GL_BITMAP and GL_UNSIGNED_BYTE are type constants whereas GL_LUMINANCE is a format constant. That would suggest that I cannot replace GL_LUMINANCE with GL_BITMAP because they are different categories of constants. Where is my reasoning wrong?

quote:
Original post by Brother Bob
Put a glPixelStorei(GL_UNPACK_ALIGNMENT, 1) before uploading the texture.


I''m not sure what you mean by "before uploading the texture". I''m assuming that means before the glTexImage2D call. Either way, no such luck.

quote:
Original post by Brother Bob
Add three bytes after the one you already have. If you use unsigned ints instead, you have to make sure the information ends up in the correct byte (little vs big endian issues), but it is possible.


I changed the constants to


const unsigned char BITMAP_DATA [] = {
0x41, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00,
0x42, 0x00, 0x00, 0x00,
0x22, 0x00, 0x00, 0x00,
0x22, 0x00, 0x00, 0x00,
0x14, 0x00, 0x00, 0x00,
0x14, 0x00, 0x00, 0x00,
0x1C, 0x00, 0x00, 0x00,
0x0C, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
0x30, 0x00, 0x00, 0x00
};
const int BITMAP_PITCH = 4;
const int BITMAP_ROWS = 12;
const int BITMAP_COLS = 8 * BITMAP_PITCH;
const int NUM_PIXELS = BITMAP_COLS * BITMAP_ROWS;


but that didn''t work, either. Thanks for the suggestions. Any other ideas?

Share this post


Link to post
Share on other sites
spiffgq    122
OK, I did make a rather bone-headed mistake. The texture height is not a power of two (12) as OpenGL requires. I added some rows to make it 16 rows high. The pitch is one which is a power of two and the number of columns is 8 which is also a power of two. So now everything should be covered. Unfortunately, this doesn''t fix the problem.

Share this post


Link to post
Share on other sites
Brother Bob    10344
quote:

const int BITMAP_ROWS = 12;
const int BITMAP_COLS = 8 * BITMAP_PITCH;


If these are the width and height of the texture, the width is wrong. Your image is still 8 bits wide, not 8*4. The extra padding has nothing to do with the texture data, but with pointer alignment after each row is read.

And after looking in the spec (you should look there too concerning what parameters to pass), you should pass GL_BITMAP as the second last parameter, and if you do, you must pass GL_COLOR_INDEX as third last parameter. Like this.

glTexImage2D (
...
GL_COLOR_INDEX,
GL_BITMAP,
BITMAP_DATA
);

This is the only valid combination including GL_BITMAP.

After reading through the specification quickly, I get the impression you can''t use bitmaps as "normal" textures. I strongly suggest you have a look in the specification, you find it here.

Share this post


Link to post
Share on other sites

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now