Results 1 to 9 of 9

Thread: Basic OPenGL Question, easy for you pros

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    3

    Basic OPenGL Question, easy for you pros

    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.

    Thanks!

  2. #2
    Senior Member Regular Contributor
    Join Date
    Oct 2001
    Location
    Princeton, NJ
    Posts
    380

    Re: Basic OPenGL Question, easy for you pros

    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

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: Basic OPenGL Question, easy for you pros

    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

    int x,y;

    for(y = 0; y < 101; y++)
    {
    for(x = 0; x < 101; x++)
    {

    glBegin(GL_POINT);
    glColor3i( int Red, int Blue, int Green); // You need to have your matrix data convert to RGB color data.

    glVertex2i( x, y);
    glEnd();
    }
    }

  4. #4
    Senior Member Frequent Contributor arekkusu's Avatar
    Join Date
    Nov 2003
    Posts
    892

    Re: Basic OPenGL Question, easy for you pros

    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:

    1) calculate your data into an array.
    2) setup a color table to convert the array's integer values to whatever RGB colors you want.
    3) 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.

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    3

    Re: Basic OPenGL Question, easy for you pros

    Thanks for all the replies!

    Here's what I've tried.
    1) I converted my 101x101 matrix to a vector
    2) 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 );
    glDrawPixels(1,1,GL_RED,GL_INT,grid);
    glEnd();

    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!

  6. #6
    Senior Member Frequent Contributor arekkusu's Avatar
    Join Date
    Nov 2003
    Posts
    892

    Re: Basic OPenGL Question, easy for you pros

    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.

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    3

    Re: Basic OPenGL Question, easy for you pros

    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

    glDrawPixel(1,1,GL_RED,GL_FLOAT,grid)
    called recursively 100*100 times, each time using a different raster position.

    Or

    glDrawPixel(100,100,GL_RED,GL_FLOAT,grid)
    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).]

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Feb 2002
    Location
    Bonn, Germany
    Posts
    1,633

    Re: Basic OPenGL Question, easy for you pros

    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

  9. #9
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    Oegstgeest, The Netherlands
    Posts
    11

    Re: Basic OPenGL Question, easy for you pros

    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:
    http://NeHe.gamedev.net
    tutorials (helped me a lot) for the basics openGL and then do this one about texture mapping:
    http://nehe.gamedev.net/data/lessons....asp?lesson=06

    Good luck,
    Marty666

Similar Threads

  1. Replies: 5
    Last Post: 01-30-2013, 11:26 PM
  2. Basic OpenGL question
    By gordon1 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 12-30-2011, 10:49 AM
  3. Qt and OpenGL, Really Basic Question!
    By Cpp Deever in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 02-11-2009, 09:25 PM
  4. Really basic OpenGL question
    By Conan in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 11-10-2004, 03:26 AM
  5. SGIs OpenGL Optimizer - comments, pros/cons?
    By plg in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 05-10-2000, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean