PDA

View Full Version : paint each side of a cube with dif. color?



arthurprs
28-04-2008, 08:10 PM
"im using opengl"


hi guys, im very noob on 3d, but i have good know how with opengl 2d (this helped), i already achieved how to draw the cubes and basic control of camera.

but in some cubes i have to colorize 1 side (or 2 or 3) with a dif color,
like http://upload.wikimedia.org/wikipedia/en/thumb/1/1d/Tetragonal_prism.png/160px-Tetragonal_prism.png

and i'm also wondering if i can make the cube bords with another dif color to make it more visible :?

thanks,
Arthur.

Brainer
28-04-2008, 08:25 PM
I use this code to render a cube. You can call "glColor3f" after each face to render it in a different color.

procedure TCube.DoRender;
begin
glBegin(GL_QUADS);
// Top Face
glNormal3f(0.0, 1.0, 0.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
glVertex3f(1.0, 1.0, 1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
glVertex3f(-1.0, 1.0, 1.0);

// Front Face
glNormal3f(0.0, 0.0, 1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
glVertex3f(1.0, -1.0, 1.0);

// Right Face
glNormal3f(1.0, 0.0, 0.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
glVertex3f(1.0, -1.0, 1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
glVertex3f(1.0, -1.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);

// Left Face
glNormal3f(-1.0, 0.0, 0.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);

// Bottom Face
glNormal3f(0.0, -1.0, 0.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
glVertex3f(1.0, -1.0, 1.0);

// Back Face
glNormal3f(0.0, 0.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
glVertex3f(1.0, -1.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);

glEnd();
end;

arthurprs
28-04-2008, 08:40 PM
thanks, it solves the problem with the colored faces, but what about colored borders?

Brainer
28-04-2008, 09:38 PM
The only idea that comes to my mind is to render the same cube twice, but the first one should be rendered with GL_LINES and the second one with GL_QUADS. I don't know if it's the best solution.

User137
28-04-2008, 10:03 PM
The kind of borders you had there in the sample screenshot looks like having 2000 faces used or so in borders to get such a smooth details. That kind is achievable by making it with a modelling program and loading it up in your program, or making the borders with your own function. Consists of 12 cylinder shapes and 8 spheres.

Yeah other way is to render with GL_LINES after (or before, looks different with z-buffer) GL_QUADS and glLineWidth to set line width.

arthurprs
28-04-2008, 10:41 PM
The kind of borders you had there in the sample screenshot looks like having 2000 faces used or so in borders to get such a smooth details. That kind is achievable by making it with a modelling program and loading it up in your program, or making the borders with your own function. Consists of 12 cylinder shapes and 8 spheres.

Yeah other way is to render with GL_LINES after (or before, looks different with z-buffer) GL_QUADS and glLineWidth to set line width.

it don't need to look like the sample, it only need to give a better contrast to the edges

edit: i will try render the same cube twice with GL_Lines on the second draw, i will post results soon

arthurprs
30-04-2008, 08:36 PM
sorry for the delay...

everything is great, gl_lines worked great on the edges :)

but im having bad problems drawing the cubes it the way i need onto the screen =X

i need to draw a cube with some small cubes, and them remove the faces away, but...

i actually don't know how to position things with gltranslatef on the 3d scene =X

any link that explains that?

User137
01-05-2008, 07:44 AM
Assume you would have function that renders small cube, wether it was with glBegin-glEnd or not, and task to render a 10x10 plane filled with cubes.

var x,y: integer;
begin
for y:=0 to 9 do begin
glPushMatrix; // save current modelview matrix to stack
glTranslatef(0,y,0); // go to row start
for x:=0 to 9 do begin
RenderCube;
glTranslatef(1,0,0); // move 1 step on x-axis
end;
glPopMatrix; // load modelview matrix
end;
end;

arthurprs
01-05-2008, 04:11 PM
look what i have, any ideia why i need to move 2 insted of 1 as you say ?
(if i move 1 it does not position the cube above the another, they intersect)

glLoadIdentity;
glTranslatef(-1,1,-20);

for m := 0 to l - 1 do
begin
for n := 0 to h - 1 do
begin
for i := 0 to w - 1 do
begin
test.Draw; //# a TCUBE.draw();
glTranslatef(2,0,0);
end;
glTranslatef(-2*w,2,0);
end;
glTranslatef(0,-2*h,-2);
end;

glTranslatef(6,-6,-10);

for m := 0 to l - 1 do
begin
for n := 0 to h - 1 do
begin
for i := 0 to w - 1 do
begin
test.Draw; //# a TCUBE.draw();
glTranslatef(2,0,0);
end;
glTranslatef(-2*w,2,0);
end;
glTranslatef(0,-2*h,-2);
end;

ps: in this code i rneder 2 cubes

User137
01-05-2008, 07:24 PM
look what i have, any ideia why i need to move 2 insted of 1 as you say ?
(if i move 1 it does not position the cube above the another, they intersect)

That would be, if your cube is min-maxed from -1 to 1. But I always render things from -0.5 to 0.5 or 0 to 1 (use -0.5 to 0.5 if you want to easily rotate around center point). That way you have simpler math when using grid or tile based game.

Your code works i guess, but don't forget to learn glPushMatrix and glPopMatrix. They are very useful, and in theory even faster than glTranslatef because there is no matrix multiplication, only simple memory copy.

arthurprs
01-05-2008, 08:39 PM
look what i have, any ideia why i need to move 2 insted of 1 as you say ?
(if i move 1 it does not position the cube above the another, they intersect)

That would be, if your cube is min-maxed from -1 to 1. But I always render things from -0.5 to 0.5 or 0 to 1 (use -0.5 to 0.5 if you want to easily rotate around center point). That way you have simpler math when using grid or tile based game.

Your code works i guess, but don't forget to learn glPushMatrix and glPopMatrix. They are very useful, and in theory even faster than glTranslatef because there is no matrix multiplication, only simple memory copy.

the code i posted code works,
im using the cube render code of Brainer (see the code on 2¬? post)

ps: i will travel to my grandmothers house today, but soon i will be back and try the results

Brainer
01-05-2008, 09:25 PM
i need to draw a cube with some small cubes, and them remove the faces away, but...

I'm not sure I understand what you mean. :? Do you want to make a big cube from smaller ones? Something like this (http://www.maniacworld.com/rubix-cube.jpg)? If so, use the code I posted and the code posted by User137. That should do the trick. :)