Thread: Can I have some Sample Code? (OpenGL, glDrawPixels();)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Can I have some Sample Code? (OpenGL, glDrawPixels();)

    Posting this quote to show that I have looked, can anyone gimme some sample code that uses glDrawpixels()?


    Preferably something that uses NeHe's base GL code... Really not interested in fudging with GLUT, seeing as how its not being used anymore...

    14.015 How do I use glDrawPixels() and glReadPixels()?

    glDrawPixels() and glReadPixels() write and read rectangular areas to and from the framebuffer, respectively. Also, you can access stencil and depth buffer information with the format parameter. Single pixels can be written or read by specifying width and height parameters of 1.

    glDrawPixels() draws pixel data with the current raster position at the lower left corner. Problems using glDrawPixels() typically occur because the raster position is set incorrectly. When the raster position is set with the glRasterPos*() function, it is transformed as if it were a 3D vertex. Then the glDrawPixels() data is written to the resulting device coordinate raster position. (This allows you to tie pixel arrays and bitmap data to positions in 3D space).

    When the raster position is outside the view volume, it's clipped and the glDrawPixels() call isn't rendered. This occurs even when part of the glDrawPixels() data would be visible. Here's info on how to render when the raster position is clipped.

    glReadPixels() doesn't use the raster position. Instead, it obtains its (X,Y) device coordinate address from its first two parameters. Like glDrawPixels(), the area read has x and y for the lower left corner. Problems can occur when reading pixels if:

    The area being read is from a window that is overlapped or partially offscreen. glReadPixels() will return undefined data for the obscured area. (More info.)
    Memory wasn't allocated for the return data (the 7th parameter is a NULL pointer) causing a segmentation fault, core dump, or program termination. If you think you've allocated enough memory, but you still run into this problem, try doubling the amount of memory you've allocated. If this causes your read to succeed, chances are you've miscalculated the amount of memory needed.
    For both glDrawPixels() and glReadPixels(), keep in mind:

    The width and height parameters are in pixels.
    If the drawn or read pixel data seems correct, but is slightly off, make sure you've set alignment correctly. Argument values are controlled with the glPixelStore*() functions. The PACK and UNPACK values control sending and receiving pixel data, from and to OpenGL, respectively.
    Last edited by Shamino; 10-18-2005 at 08:35 AM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    In my experience with Shakti, one of my friends from the board, I don't recommend using glDrawPixels to do anything. It seems to require 2 copies - 1 to get the data to a buffer, and 1 internal to GL to get that data to the actual frame buffer.

    Not a good idea.

    GL does not let you 'lock' surfaces like DX (from what I know of GL which is very little).

    Direct access to buffers is becoming less and less popular and/or needed due to modern advances like vertex and pixel shaders which give you a huge amount of control over rendering processes - down to the pixel level.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    it seems Shamino just isn't having much luck

  4. #4
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by Shamino
    Preferably something that uses NeHe's base GL code... Really not interested in fudging with GLUT, seeing as how its not being used anymore...
    I'm sure GLUT is used more than glDrawPixels(). You'd be better off simply creating a quad, drawing the pixels to a texture, and then putting the texture on the quad. That's the way I'd do it.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Thats funny frobozz, cuz I just got done rewriting my entire program so that i don't loop render 392 quads to do that job, it lags, bad.....

    at first I had it so I would loop 392 quads, give them click detection, and a green/red sold flag all in one punch...

    That lagged really really bad......

    now I made it so I used lines to create my grid, rather than quads, runs uber smooth, mouse click detection will be easy to implement, but...

    I need to turn the cells of my grid different colors depending whether or not the cell on the grid is "sold" or not....

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Here's a hint. Stop drawing quads everywhere. All you need is this:

    A vertex buffer holding vertex positions, normals, texture coords, etc.
    An index buffer which indexes into the vertex buffer to create triangles.
    A call to OpenGL to render the triangles. The texture coords should be set so that every 2 triangles makes up one quad.

    I'm not sure how GL works but in D3D you do not iterate through your vertex buffer to render it. You pass the vertex buffer, index buffer, and set the correct rendering states and then call DrawPrimitive() or ID3DXMesh::DrawSubset(x) and D3D does the rest.

    Manually looping through the vertex buffer during a render is basically a software hack for what should be done in hardware.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    Shamino might be getting confused the vertex buffer thing. OpenGL doesn't have vertex buffers as part of the standard API but you can use vertex buffers with the ARB extensions.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Okay.... soooo, I can't play with the vertex buffer in standard OGL....

    okay, so is there a way I can solve my problem without adding on other libraries and such?

    If not, what do I need, and maybe even a link to a tutorial if you're feeling nice ....

    Ultimately, what i need to do, is change a certain set of pixels (20x20) green, which arent outlined by a polygon of any sort....without killing my system... I really don't know where to look anymore

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Quote Originally Posted by Shamino
    Ultimately, what i need to do, is change a certain set of pixels (20x20) green, which arent outlined by a polygon of any sort....without killing my system... I really don't know where to look anymore
    So... you know exactly what pixels you need to change? If so, then change to a 2D ortho perspective with Width and Height the same as the window and draw a single poly untextured 20x20.

    If you want a way to optimize the change from 2D and 3D I can help on that.

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I'm already in 2d orthoscopic....

    Making quads the "normal" way is what im trying to avoid, because ultimately when you end up with alot of them it lags, unless you're using vertex buffer objects... or vertex arrays, which is what i'm trying to learn how to do....

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Are they all the same size? You could use a DisplayList to create one, and then just repeat it. That should be fast enough.

    Code:
    unsigned int ID;  //this is a unique ID for the list, like a texture ID
    ID = glGenLists(1);
    glNewList(ID, GL_COMPILE);
       glBegin(GL_QUADS);
       //Draw your model here
       glEnd();
    glEndList();
    
    //------------------------------------------------
    
    //Anywhere you want to draw the above object, just type
    
    glCallList(ID);
    A display list creates a cache of any commands you insert inside of this list so the can be executed quickly. Vertex arrays are on par with Display Lists as far as speed goes, vertex arrays allow you to alter the data during the program, you have to delete the display list and remake it to alter the data there. The tradeoff for this reduced flexability is that display lists are very simple.
    Last edited by Eber Kain; 10-24-2005 at 07:58 AM.

  12. #12
    ---
    Join Date
    May 2004
    Posts
    1,379
    Maybe I didn't say that properly.
    It's not an add on library they are just extensions that should come with your grpahics card drivers.
    http://oss.sgi.com/projects/ogl-sample/registry/

  13. #13
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Alright.... sooo Learn how to manipulate the vertex buffer through the ARB extensions... This will ultimately lead me to the solution to my problem, correct?

  14. #14

  15. #15
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    So doing that lesson, and fully understanding it (vertex buffer objects), will ultimately let me apply it to my simple tiny 2d program to color a set of pixels green when i need them to be green, without making a quad?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SMS sample code
    By hagenuk in forum C Programming
    Replies: 1
    Last Post: 05-30-2008, 11:47 AM
  2. Sample code please help!!!
    By aewing30 in forum C Programming
    Replies: 6
    Last Post: 05-29-2008, 10:51 AM
  3. The code sample of classes in the tutorial. :o
    By Wall in forum C++ Programming
    Replies: 3
    Last Post: 08-20-2004, 09:18 AM
  4. looking for book with sample code
    By stella in forum C++ Programming
    Replies: 5
    Last Post: 06-25-2003, 10:48 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM