Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Pot Noodle on August 14, 2011

Title: Colour Array
Post by: Pot Noodle on August 14, 2011
Hi guys,
I am trying to create an array of floats, so I can cycle through the array and produce colours
that will blend from one to the next and so on, I am using opengl.

glColorPointer(3, GL_FLOAT, 0, VarPtr Rainbow)

any help would be appreciated.

Title: Re: Colour Array
Post by: Jim on August 15, 2011
Yeah, this probably isn't going to work the way you want.  You asked by PM how I did the colour cycling on my CDI demo.  The answer is 'the hard way'.
On the Amiga we had the copper, and it could change the colour of a scanline every 4 pixels, so on a 320wide screen you could get 80 colour changes and the lovely colour gradients you are after just by cycling 80 words or so of data every frame.
Obviously no such thing exists on the PC so we have to emulate it the hard way.  You need to draw all the pixels on the scanline every frame.
So the problem is in two parts
1) prepare the colours for plotting
2) plot the colours
There are loads of ways to approach this.  I can't remember what I did for my demo.  I suspect I filled an array with the 32 bit colours and just used GL_POINT to plot them. ie.

C pseudo-code
Code: [Select]
const int width=320
int y=150
float r[width]
float g[width]
float b[width]
// fill r,g,b arrays with something
glBegin(GL_POINT)
for x = 0 to width
{
   glColor3f(r[x],g[x],b[x])
   glVertex2i(x, y)
}
glEnd()

Try it with random numbers in the floats to start off with.  Using GL_POINT is a pretty slow way to draw things though.

<edit>Just checked, my conversion of the CDI demo was entirely software rendered

Jim
Title: Re: Colour Array
Post by: Pot Noodle on August 15, 2011
Hi Jim,
Thanks for the info, I have tried many ways of which none work, plotting a color pixel that does not move is the most I get.
I have had a cool looking gradient effect though a horizontal line but could not get it to cycle.
Shame  :'( would have made the finishing touch to my Demo.
Title: Re: Colour Array
Post by: hellfire on August 15, 2011
Quote
I have had a cool looking gradient effect though a horizontal line but could not get it to cycle.
Extending on Jim's example, the cycling would look like this:
Code: [Select]
int cycleCounter= 0;
...
while (...) {
  glClear(...);
  ...
  glBegin(GL_POINT);
  for x = 0 to width
  {
    int colorIndex= (x + cycleCounter) % width; // % is the modulo operation
     glColor3f(r[colorIndex],g[colorIndex],b[colorIndex])
     glVertex2i(x, y)
  }
  glEnd();
  cycleCounter++;
  ...
  swapBuffers(...);
}
I can't recommend drawing single pixels, though - it totally contradicts the scalable approach of any vector api.
Title: Re: Colour Array
Post by: Pot Noodle on August 15, 2011
Thanks Hellfire,
I agree with you, glpoint is not the way forward for a few reasons.
I wonder what that effect is called, it must have a name?

I would like to get my hands on some code even if it is old code for this effect, but thanks anyway.
Title: Re: Colour Array
Post by: hellfire on August 15, 2011
I wonder what that effect is called, it must have a name?
It's called color-cycling (http://en.wikipedia.org/wiki/Color_cycling) but since modern hardware doesn't have indexed colors anymore, you have to modify the actual pixels to emulate the effect.
Title: Re: Colour Array
Post by: combatking0 on August 15, 2011
I could be wrong, but don't colours work as integers rather than as floating point values?

Float data will work, but a 32 bit integer may be more RAM efficient and could run faster if you're managing thousands of pixels.
Title: Re: Colour Array
Post by: hellfire on August 15, 2011
I could be wrong, but don't colours work as integers rather than as floating point values?
That's true but a real world implementation wouldn't draw single pixels but quads of suitable size.
I don't think you'd ever need more than 20 values for a smooth gradient...