Thread: pixels for fractals!

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    pixels for fractals!

    Mebbe this is an oft repeated graphics question but i would really like to know if or how i can use the graphics package like open GL to simply plot coloured points? i say this because i like writing fractal programs and want to be able to colour them nicely as they run. There does not seem to be any simple way to just colour a pixel or part of pixel at co-ordinates xy according to say, the number of iterations.

    cheers for any suggestions!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So basically, you're just looking for a function like this?
    Code:
    void plot_pixel(int xposition, int yposition, COL colour);
    It's really easy to do in 2D graphics libraries like the SDL. I'm sure it can be done in OpenGL, too -- you'd just have to look.

    If you really had to, you could blit 1x1 squares. http://nehe.gamedev.net/data/lessons....asp?lesson=02
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Touching pixels in modern graphics is a no-no. That's memory reserved for use by the hardware only which is why you find very few examples of it being done.

    In modern graphics APIs you will have to either render to a texture (just like accessing a screen array in memory) and draw it or you will have to find some other texture based method like locking a surface, drawing, unlocking, and rendering a textured quad.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Thumbs down

    To plot pixels all you really need to do is be able to convert a one dimensional memory address of your image buffer to 2 dimensional co-ordinates, or the other way around. Heres a basic example to plot pixels with 32 bit colour:
    Code:
    void WritePixel(int x, int y, Uint32 col)
    {
        Uint32 *pix;
        pix=screen->pixels+((x+y*GRAPHICS_WIDTH)<<2);
        *pix = col;
    }
    You may also want to use bounds checking to make sure you arent writing outside of the buffer.

    [edit] Dont know what that thumb thing is doing at the top of my post. Dont know how to get rid of it either.[/edit]

    [edit 2] Yeah locking image buffers makes pixel read/write operations a lot faster. Don't know how it all works in Open GL myself as I've never used it. [/edit]
    Last edited by mike_g; 12-20-2007 at 06:01 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Locking and unlocking makes the operation slower since you must issue the lock, alter the buffer, and issue the unlock. It's also a severe performance hit because lockable buffers can only exist in system memory. This means after you unlock the buffer it is finally pushed out to the video card to render. If you use render to texture methods then you are still operating within the confines of hardware. All you do is treat the texture like the back buffer and then draw a quad the size of the screen onto the primary buffer using the texture you created. Render to texture does not require any locks and the everything can exist in video memory instead of system memory. It is best to 'play nice' with the hardware or you will suffer some severe penalties in performance.

    So locking and unlocking is not only the slowest way to do things it is also become the obsolete way to do things. Direct3D has provided many newer functions to remedy the oft problem of gaining access at the texel level and OpenGL has as well.

    The easiest way is to create the texture in a pixel shader but that explanation/discussion is best reserved for a different thread.
    Last edited by VirtualAce; 12-20-2007 at 08:02 PM.

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Thats strange. I found that locking image buffers while you write to them was much faster. Admittedly that was in Basic or Java, and I don't know exactly how it all works. But it was definitely a faster than doing the same thing without locking the buffer. Maye that was because I never actually had direct access to the buffers anyway in these languages, or at least thats the only explanation I can think of.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Your checklist for modern hardware. If your algo or your thinking falls into these categories you probably need to rethink your design as it relates to modern hardware.

    • If it requires anything with locking surfaces/buffers
    • If it requires a significant amount of these locks/unlocks (locks/unlocks incur a performance hit, possible card render dead lock if done improperly, bandwidth issues as the new data is streamed over the bus to the card, and possible cache thrash and/or miss issues)
    • If you feel you must absolutely have access to each texel
    • If you are trying to use Direct3D/OpenGL like old school DOS video buffers
    • If you are attempting to do special effects at the pixel level w/o using a pixel shader or fixed function pipeline texture blending (Direct3D).


    Remember there is usually a way to get at the texels without really ever needing access to them. In this case you probably do have to perform a lock, get the surface and alter it, and then unlock it. If you use a pixel shader or fragment you can do the entire fractal in the confines of a vertex/pixel shader combo.

    The ONLY ways to get the texels is either indirectly through texture blending, directly via locking, or directly using pixel shaders which do NOT require a lock.
    Last edited by VirtualAce; 12-20-2007 at 08:19 PM.

  9. #9

    Join Date
    May 2005
    Posts
    1,042
    Touching pixels in modern graphics is a no-no.
    That's really not true, there are API functions in GL and Direct3D for doing just that. I don't remember off-hand, but I think you should MSDN the glWritePixels function. If that isn't it then the 'related hits' tab should give you what you need. I believe I used this to transmit the results of a software raster function to the screen.
    Last edited by BobMcGee123; 12-20-2007 at 08:31 PM.
    I'm not immature, I'm refined in the opposite direction.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There aren't any functions in Direct3D for writing pixels. You can lock a surface and get a pointer to a buffer but that's about it. I believe Shakti and I tried glWritePixels for a buffer copy and had significant troubles with it.

    The closest thing in Direct3D to glWritePixels is StretchRect.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And in all cases, the processor (CPU) is highly likely to be the better candidate for generating individual pixels, compared to a GPU - the CPU has a much shorter pipeline for example.

    GPUs are excellent at dealing with "many" pixels at once, in parallell. But if you want to draw an individual pixel [independently of other pixels around it], then the CPU will be the "better" option.

    Of course, it's certainly best if you can get the GPU to do LOTS OF PIXELS, rather than doing individual ones in the first place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Now, if you can implement your fractal as a pixel shader, that's obviously cool, and probably the fastest way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    Now, if you can implement your fractal as a pixel shader, that's obviously cool, and probably the fastest way.
    Yes, indeed. A collegue of mine working with graphics drivers actually wrote a "self modifying fractal shader", which was only a dozen or so instructions long.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Shaders can do a lot of fancy stuff and fast, but I think the OP was asking for the simplest way to draw 2d graphics. I would suggest TinyPTC as its very simple and only sets up the graphics buffers for drawing to.

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That's the problem. There isn't a simple way to draw 2D graphics anymore. The simplest way is working with the hardware.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bitmap pixels???????????
    By SuperNewbie in forum Windows Programming
    Replies: 2
    Last Post: 03-23-2004, 01:53 AM
  2. How to store pixels in memory created with Setpixel(...)
    By Kristian25 in forum Windows Programming
    Replies: 2
    Last Post: 03-11-2003, 06:09 AM
  3. creating image from pixels stored in file
    By Kristian25 in forum Windows Programming
    Replies: 3
    Last Post: 01-21-2003, 02:08 PM
  4. Algo needed for 'Fit to page'
    By Unregged in forum Windows Programming
    Replies: 6
    Last Post: 10-03-2002, 07:09 AM
  5. What Else can I do besides Plot Pixels?
    By Xterria in forum C++ Programming
    Replies: 13
    Last Post: 10-02-2001, 07:11 PM