Author Topic: Colour Array  (Read 4740 times)

0 Members and 1 Guest are viewing this topic.

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Colour Array
« 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.


Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Colour Array
« Reply #1 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
« Last Edit: August 15, 2011 by Jim »
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Colour Array
« Reply #2 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.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Colour Array
« Reply #3 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.
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Colour Array
« Reply #4 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.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Colour Array
« Reply #5 on: August 15, 2011 »
I wonder what that effect is called, it must have a name?
It's called color-cycling but since modern hardware doesn't have indexed colors anymore, you have to modify the actual pixels to emulate the effect.
« Last Edit: August 15, 2011 by hellfire »
Challenge Trophies Won:

Offline combatking0

  • JavaScript lives!
  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4569
  • Karma: 235
  • Retroman!
    • View Profile
    • Combat King's Barcode Battler Home
Re: Colour Array
« Reply #6 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.
You are our 9001st visitor.
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Colour Array
« Reply #7 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...
Challenge Trophies Won: