Skip to main content
gamedev.net
🔒 Locked

Color problems

Started by DarkNebula 14 replies 600+ views
Original Post
DarkNebula
DarkNebula
sBlock Block[15][15];
GLfloat block_Color[1][2];

void BlockEngine::InitBlocks(void)
{
	// Red - 0

	block_Color[0][0] = 1.0f;
	block_Color[0][1] = 0.0f;
	block_Color[0][2] = 0.0f;
	// Green - 1

	block_Color[1][0] = 0.0f;
	block_Color[1][1] = 1.0f;
	block_Color[1][2] = 0.0f;
	
	srand(static_cast<unsigned>(time(0)));
	
	for (unsigned int loop=0; loop<16; loop++)
	{
		for (unsigned int loop1=0; loop1<16; loop1++)
		{
			unsigned int Random_Color = (rand()%2);

			Block[loop][loop1].BlockColor[0]=block_Color[Random_Color][0];
			Block[loop][loop1].BlockColor[1]=block_Color[Random_Color][1];
			Block[loop][loop1].BlockColor[2]=block_Color[Random_Color][2];	
		
		}
	}
}


   
void BlockEngine::CreateBlocks(void)
{
	GLfloat X = 0.0f;
	GLfloat Y = -40.0f;

	for (unsigned int loop=0; loop<16; loop++)
	{
		X=-40;
		Y+=40;
		for (unsigned int loop1=0; loop1<16; loop1++)
		{
			X+=40;

			glColor3f(Block[loop][loop1].BlockColor[0],
					  Block[loop][loop1].BlockColor[1],
					  Block[loop][loop1].BlockColor[2]);

			cGUIEngine->CreateImage(tex_Block.TextureID, 40, 40, X, Y);
		}
	}
}

When the blocks are actually made during the main loop, they come all different colors. I only set red and green as you can see. In VC++ I added a watch to see if the right values were going into the variables and they were, but when they come out in the end they are all different colors. Does anyone have any ideas why this would happen? Are there any problems with my code? Thanks. EDIT: sBlock struct:
struct sBlock {
	GLfloat BlockColor[2];	
};
- DarkNebula [edited by - DarkNebula on November 22, 2003 7:58:39 PM]
JonnyQuest
JonnyQuest
I can''t get my head round your code right now, but the thing that jumps out at me is that you need to specify R, G and B for each pixel, but you''ve only got red and green components as far as I can see. Even if you only want red and green, you have to set blue to zero.

- JQ
~phil
DarkNebula
DarkNebula
They are set to zero. The only thing that can be assigned to the array is up to 2. Which is filled with zero as needed.

- DarkNebula
Daaark
Daaark
You should be doing

sBlock Block[16][16];
GLfloat block_Color[2][3];


[edited by - Vampyre_Dark on November 22, 2003 11:20:43 PM]
DarkNebula
DarkNebula
I don't see what that has to do with it, but even if I do change the array it still doesn't work...

- DarkNebula

[edited by - DarkNebula on November 22, 2003 11:27:16 PM]
Daaark
Daaark
Don''t see what that has to do with is? You have 2 colors, and 3 components, but you make an array for 1 color that has 2 components.

You also made an array of 30 for 32 blocks.
DarkNebula
DarkNebula
Doesn''t zero count in an array?

- DarkNebula
DarkNebula
DarkNebula
Still doesn''t work correctly. :/

- DarkNebula
Daaark
Daaark
quote:
Doesn''t zero count in an array?


Yes.

block_Color[0][0] = 1.0f; <--1
block_Color[0][1] = 0.0f; <--2
block_Color[0][2] = 0.0f; <--3

That''s three components, not two.
DarkNebula
DarkNebula
I was saying when you acutally init. the array, doesn't zero count as part of it? So array[1][2] is actually 2 by 3?

- DarkNebula

[edited by - DarkNebula on November 22, 2003 12:02:12 AM]
DarkNebula
DarkNebula
Even if I''m wrong (which I prob. am) I changed the arrays like you said but it still doesn''t work.

- DarkNebula
TravisWells
TravisWells
array[1][2] is size 1 by 2. So your valid elements are:
array[0][0]
array[0][1]

"1" and "2" are not the max value, they are the size. If you declare it size 1, it only has one element, which is 0.
DarkNebula
DarkNebula
Okay, but is there anything else wrong with my code? I fixed the arrays but it still comes out with the same results.

- DarkNebula
Daaark
Daaark
I usually store them like this

float fColorBlack [] = {0.25,0.25,0.25,1.00};
float fColorRed [] = {0.85,0.00,0.00,1.00};
float fColorGreen [] = {0.00,0.85,0.00,1.00};
float fColorBlue [] = {0.00,0.00,0.85,1.00};
float fColorCyan [] = {0.00,0.85,0.85,1.00};
float fColorYellow [] = {0.85,0.85,0.00,1.00};
float fColorPurple [] = {0.85,0.00,0.85,1.00};

then something like this (yeah, I know this is slow/unoptimized). Just change the material calls to color calls. Will do what you want.

if (nColor == 0) glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,fColorBlack);
if (nColor == 1) glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,fColorRed);
if (nColor == 2) glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,fColorGreen);
if (nColor == 3) glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,fColorBlue);
if (nColor == 4) glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,fColorCyan);
if (nColor == 5) glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,fColorYellow);
if (nColor == 6) glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,fColorPurple);
DarkNebula
DarkNebula
That did work. I fogot I could use glColor4fv... That''s a newbie for ya. Heh. Thanks for the help everyone. And clearing that up with arrays.

- DarkNebula

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.