Hey guys, I’m hoping you can give me a bit of help.
This is my first time using OpenGL, and I’m not even a great programmer, so please bear with my simple problem.
I have a 101x101 integer matrix, which I want to convert to a simple image. Each matrix element should translate to one pixel in the image…its color dependant on the integer value. Make sense?
I figure this is pretty much basic, but I’m not sure how to start on it. I’m using win2k, and a lot of custom (C++) libraries already. Please let me know what I need, and the best way to start off.
I don’t know if opengl is what you want. OpenGL is a 3D api and while you can certainly do what you want using glDrawPixels, if you know nothing at all about OpenGL its won’t be worth the effort of setting up a window. If you want to learn OpenGL check out nehe.gamedev.net, that will get you started
When you say image, you mean an image for a texture or write directly to the screen?
OpenGL just one thing to remember, openGL is made for 3D graphics rendering, not direct pixel drawing to the screen. You can draw directly to the screen but at a loss of speed.
You can get better preformance if you create your matrix image as a texture and map it to a quad. nehe.gamedev.net has good tutors on that.
Another option would be to draw in ortho mode, and use something like GL_POINT.
draw code would look something like this
for(y = 0; y < 101; y++)
for(x = 0; x < 101; x++)
glColor3i( int Red, int Blue, int Green); // You need to have your matrix data convert to RGB color data.
glVertex2i( x, y);
Please ignore what nexusone said.
OpenGL is not a 3D library. It is a graphics library: “OpenGL is the premier environment for developing portable, interactive 2D and 3D graphics applications.” If it was only for 3D it would be named something like “Direct3D”.
There are two general paths to do what you want:
- calculate your data into an array.
- setup a color table to convert the array’s integer values to whatever RGB colors you want.
- use glDrawPixels to copy the data to the current raster location.
or, same as above except
3) upload the array data as a texture, using the color table to convert the RGB texture data.
4) draw the texture onto a quad.
Probably the first way is easier for you.
There are a lot of little details to get right in both cases, read the documentation to figure out how raster drawing, color tables, pixel transfer operations, textures etc work, try it out, then come back and ask questions on the parts that break.
Thanks for all the replies!
Here’s what I’ve tried.
- I converted my 101x101 matrix to a vector
- Normalized all of my data so its between 0.0 and 1.0
I’m taking the glDrawPixels approach, but it doesn’t seem to be working…
More or less, this is what I have
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );
glRasterPos2d ( 0.0f , 0.0f );
Where my data is stored in ‘grid’ and I’m trying to get a ‘redscale’ graphic interpretation. But, this just makes a black pixel in my raster position. Is this because its trying to use ALL of my ‘grid’ data in one raster position? How can I use just one vector-element per raster?
Thanks again guys, you’re helping alot!
The first two arguments to glDrawPixels are the width and height of the source rect. You’re getting one pixel because that’s what you asked for.
Also be careful of the format conversion. It sounds like you normalized your source data as floats? But you’re asking GL to interpret the array as 32 bit ints. That will draw something, but it probably won’t be what you want.
Well, I guess my real question is, do I need to use glDrawPixel for each pixel in my image, or will one call draw multiple pixels. In other words, to create a 100x100 pixel image do I use
called recursively 100*100 times, each time using a different raster position.
just called one time, at a base raster of 0.0f,0.0f?
Also, my data values are in the range 0.0f-1.0f, is this the right format to use GL_RED, and GL_FLOAT? Thanks!
[This message has been edited by Twisted29 (edited 02-14-2004).]
The latter. glDrawPixels(100,100,GL_RED,GL_FLOAT,grid) will pull 100x100 floats and draw 100x100 pixels. One float per pixel. This should be what you want.
Range from 0.0 to 1.0 is fine for the GL_FLOAT source format (OTOH integers are normalized to their full range to get maximum precision; GL_UNSIGNED_SHORT => [0…65535]).
Raster position (0;0) will be the viewport center in the inital state. (-1;-1) is lower left. Once you change your modelview and/or projection matrix, it will get more complicated
I’d really suggest u use orthographic mode and render a quad. It’s easyer, faster and you can mess around with it using a lot of easy opengl functions.
So try the first couple of:
tutorials (helped me a lot) for the basics openGL and then do this one about texture mapping: