The above video goes away if you are a member and logged in, so log in now!

 Would you like to get all the newest Gaming News fromQJ.NET in your email each day? Want to learn more about the team who brings you the QJ news?

Cycle triangle colours at runtime?

This is a discussion on Cycle triangle colours at runtime? within the Developer's Dungeon forums, part of the PSP Development Forum category; Hi Guys, Is there a way to use variables instead of constants here so colours of triangles and polygons can ...

 Tweet
1. Cycle triangle colours at runtime?

Hi Guys,
Is there a way to use variables instead of constants here so colours of triangles and polygons can
be cycled at runtime?
Code:
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.6f, -0.2f },
Cheers, Art.

2. Yeah, just change your vertex[#].color (provided it's named color in your struct) to whatever you want it to be.
Example:
Code:
typedef struct {
unsigned int color;
float x, y, z;
} Vertex;

Vertex __attribute__((aligned(16))) triangle[3] = {
{ 0xFF0000FF, 1.0f, 0.0f, -1.0f },
{ 0xFF0000FF, 0.0f, 1.0f, -1.0f },
{ 0xFF0000FF, -1.0f, 0.0f, -1.0f }
}

triangle[0].color = 0xFF554433;
triangle[1].color = GU_COLOR(0.3f, 0.5f, 1.0f, 0.4f);
triangle[2].color = some_u32_variable;

3. GU_COLOR translates four clamped floats into a 32-bit number. You can just piece together the number you want yourself. IIRC, the format it returns is AABBGGRR, so you could define an inline function:

Code:
u32 rgba_to_gu_color(float r, float g, float b, float a) {
u8 r_char = (fabs(r) <= 1?(u8)(r*0xFF):1);
u8 g_char = (fabs(g) <= 1?(u8)(g*0xFF):1);
u8 b_char = (fabs(b) <= 1?(u8)(b*0xFF):1);
u8 a_char = (fabs(a) <= 1?(u8)(a*0xFF):1);
return r|(g << 8)|(b << 16)|(a << 24);
}
You could also probably put variables into GU_COLOR, but I wouldn't try putting function calls in it due to the nature of #define macros.

4. Code:
rgbg = rgbg - 2;
if (rgbg < 0x10) {rgbg = 0xFF;}

rgbr++;
if (rgbr > 0xF0) {rgbr = 0;}

cycle = 0;
cycle = rgbr;
cycle = cycle + (rgbg * 0x100);
cycle = cycle + (rgbb * 0x10000);
cycle = cycle + 0xFF000000;

for (colcnt=1; colcnt<72; colcnt++) { // image height
ttt[colcnt].color = cycle;
}
for (colcnt=1; colcnt<60; colcnt++) { // image height
rrr[colcnt].color = cycle;
}
Hmm, the colours are cycling but not as they should...

5. Any reason you're not using rgbg -= 2;? And, uh, that color = 0; line is kinda unnecessary. Actually, you're not using any of the convenience _= operators.

Anyway, given that snippet of code, I can't see anything specifically wrong. Any more information than just "wrong"?

6. Thanks for looking,
Yeah the cycle 0 was needed at some stage but I've been playing around.
Some triangles cycle, some stay a constant colour, and some seem to cycle out of time with each other.
I am only meaning to change the value of cycle (u32 var for current colour)
once every main iteration.

I suppose they start out the original colour for the first time.
One object:
Code:
Vertex __attribute__((aligned(16))) ttt[3*24] =
{
{ GU_COLOR( 1.0f, 1.0f, 1.0f, 0.0f  ),  0.2f, 0.8f, 0.0f },	// t top
{ GU_COLOR( 1.0f, 1.0f, 1.0f, 0.0f  ),  0.4f, 0.8f, 0.0f },
{ GU_COLOR( 1.0f, 1.0f, 1.0f, 0.0f  ),  0.4f, 0.8f, -0.2f },

{ GU_COLOR( 1.0f, 1.0f, 1.0f, 0.0f  ),  0.2f, 0.8f, 0.0f },
{ GU_COLOR( 1.0f, 1.0f, 1.0f, 0.0f  ),  0.2f, 0.8f, -0.2f },
{ GU_COLOR( 1.0f, 1.0f, 1.0f, 0.0f  ),  0.4f, 0.8f, -0.2f },

{ GU_COLOR( 0.2f, 0.2f, 0.2f, 0.0f  ),  0.2f, 0.0f, 0.0f },	// t bottom
{ GU_COLOR( 0.2f, 0.2f, 0.2f, 0.0f  ),  0.4f, 0.0f, 0.0f },
{ GU_COLOR( 0.2f, 0.2f, 0.2f, 0.0f  ),  0.4f, 0.0f, -0.2f },

{ GU_COLOR( 0.2f, 0.2f, 0.2f, 0.0f  ),  0.2f, 0.0f, 0.0f },
{ GU_COLOR( 0.2f, 0.2f, 0.2f, 0.0f  ),  0.2f, 0.0f, -0.2f },
{ GU_COLOR( 0.2f, 0.2f,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0f },

{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.2f, 0.4f, 0.0f },
{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.2f, 0.6f, 0.0f },
{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.0f, 0.6f, 0.0f },

{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.4f, 0.4f, 0.0f },
{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.6f, 0.4f, 0.0f },
{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.4f, 0.6f, 0.0f },

{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.2f, 0.6f, 0.0f },
{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.4f, 0.6f, 0.0f },
{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.2f, 0.8f, 0.0f },

{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.4f, 0.6f, 0.0f },
{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.4f, 0.8f, 0.0f },
{ GU_COLOR( 0.9f, 0.9f, 0.9f, 0.0f  ),  0.2f, 0.8f, 0.0f },

{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.0f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.0f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.2f, -0.2f },

{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.0f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.2f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.2f, -0.2f },

{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.2f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.2f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.4f, -0.2f },

{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.2f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.4f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.4f, -0.2f },

{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.4f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.4f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.6f, -0.2f },

{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.4f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.6f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.6f, -0.2f },

{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.4f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.6f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.0f, 0.6f, -0.2f },

{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.4f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.6f, 0.4f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.6f, -0.2f },

{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.6f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.6f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.8f, -0.2f },

{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.6f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.4f, 0.8f, -0.2f },
{ GU_COLOR( 0.4f, 0.4f, 0.4f, 0.0f  ),  0.2f, 0.8f, -0.2f }
};
I still don't know what colcnt represents (one point, one triangle, slice of pizza)
Code:
ttt[colcnt].color = cycle;

7. colcnt should start at 0, not 1.

8. It was at first.. some attempts to change things have made it look worse
Have been able to change colours ok, but still having trouble keeping it uniform.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

All times are GMT -8. The time now is 04:40 PM.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY