Jump to content
  • Advertisement

Archived

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

Cybervic

Help Draw a Plasma in OpenGL (Not sine)

This topic is 5306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to convert some pygame plasma code to Opengl. However I cannot figure out how to do a getpixel command in Opengl. Any guides, help or directions will be appreciated. Here is the pygame code : import pygame, sys from Numeric import * from RandomArray import * import random # Global Values ScreenRes=array((640,480)) global screen global color #global colorarray pygame.init() screen=pygame.display.set_mode(ScreenRes,0,8) #screen.set_palette([[0,0,0],[0,255,0],[255,255,255]]) #we will use this to setup our palette for plasma def ramp(rs,re,gs,ge,bs,be,startcol,endcol): n=1 nadd=int(255/(endcol-startcol)) for i in range(startcol,endcol): r=int((((rs-re)*n)/255)+re) g=int((((gs-ge)*n)/255)+ge) b=int((((bs-be)*n)/255)+be) screen.set_palette_at(i,[r,g,b]) n=n+nadd # #Rotate the palette # def rotatepalette(): y=screen.get_palette() y = list(y) for s in range(254): x = y.pop(1) y.append(x) screen.set_palette(tuple(y)) pygame.display.update() # #Put down the starting points # def startplasma(x0,y0,x1,y1): screen.lock() screen.set_at((x0,y0),(random.randint(0,255)+1)) screen.set_at((x1,y0),(random.randint(0,255)+1)) screen.set_at((x0,y1),(random.randint(0,255)+1)) screen.set_at((x1,y1),(random.randint(0,255)+1)) screen.unlock() # #Calculate the new color # def newcolor(x0,y0,x,y,x1,y1): # c=screen.get_at((x,y)) if screen.get_at((x,y)) <> (0,0,0,255): return() color=(x1-x0)+(y1-y0) color=random.randint(0,(color*2))-color a=screen.map_rgb(screen.get_at((x0,y0))) b=screen.map_rgb(screen.get_at((x1,y1))) color=color+int((a+b+1)/2) if color < 1: color=1 elif color > 254: color=254 screen.set_at((x,y),color) # #Do the actual plasma calculations # #START PROCEDURE doblock def doblock(x0,y0,x1,y1): if ((x1-x0)<2) and ((y1-y0)<2): return() x=int((x0+x1+1)/2) y=int((y0+y1+1)/2) newcolor(x0,y0,x,y0,x1,y0) newcolor(x1,y0,x1,y,x1,y1) newcolor(x0,y1,x,y1,x1,y1) newcolor(x0,y0,x0,y,x0,y1) a=screen.map_rgb(screen.get_at((x0,y0))) b=screen.map_rgb(screen.get_at((x1,y0))) c=screen.map_rgb(screen.get_at((x1,y1))) d=screen.map_rgb(screen.get_at((x0,y1))) e=2 color=int((a+b+c+d+e)/4) screen.set_at((x,y),color) doblock(x0,y0,x,y) doblock(x,y0,x1,y) doblock(x,y,x1,y1) doblock(x0,y,x,y1) #END PROCEDURE doblock ramp(255,000,000,000,000,000,1,63) ramp(000,255,000,000,000,000,63,126) ramp(000,000,255,000,000,255,126,189) ramp(000,000,000,255,000,000,189,256) #while 1: # for event in pygame.event.get(): # if event.type == pygame.QUIT: # sys.exit() x0=0 y0=0 x1=639 y1=479 startplasma(x0,y0,x1,y1) doblock(0,0,639,479) pygame.display.update() while 1: rotatepalette() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() It takes a while to draw the plasma on slower systems. This is the reason why I want to port it to Opengl. (Pardon the english, it is not my native language) All of us were put on earth to do a certain amount of work. I am so far behind I will never die.

Share this post


Link to post
Share on other sites
Advertisement
The plasma effect you are using is a 2D effect. OpenGL is a 3D API, it isn't really suited for this kind of thing. You can get direct access to the pixels in OpenGL but it'd probably go slower rather than faster if you use OpenGL as OpenGL is designed primarily for rendering 3D scenes, not drawing 2D stuff. If you want to do this OpenGL though you probably want to look into glReadPixels and glDrawPixels. You will have some trouble though as the pyGame version you've got uses palletes to achieve colour changes. OpenGL does support 256 palleted colour but in the end it'd just be easier to use the pyGame version (and most probably faster).

[edit]And what specs are these slower systems? Remember python is an interpreted language. It should run at a perfectly fine speed on modern systems for things such as this plasma effect. But it could conceivably run rather slowly on very old systems.

[edited by - Monder on April 14, 2004 9:10:41 AM]

Share this post


Link to post
Share on other sites
Thx, I actually want to use the plasma as a surface/texture for a rotating 3d cube. I am in the process of learing opengl.

Share this post


Link to post
Share on other sites
Ahh in that case one way to do it would be to have a buffer where you do the plasma effect and then every frame update this buffer and copy it into a texture. Take a look at glTexSubImage2D. You can use this function to update a texture with new image data. Things are complicated with the use of mipmapping as it means your plasma texture would have to have several mipmap levels as well. So you would have to generate mipmaps every time the plasma image altered. Either that or disable mipmapping.

You will also have to alter the way it cycles the colours. You could use a palleted colour mode in OpenGL but as you start adding in things other than the plasma their colours will cycle along with the plasma, which would look rather odd.

Share this post


Link to post
Share on other sites
You could also create a number of gray scale images that do the plasma effect, then set the colour to get various colour effects. ie red plasma and blue plasma can use the same set of textures.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!