Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: paint each side of a cube with dif. color?

  1. #1

    paint each side of a cube with dif. color?

    "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/wikipedi...onal_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.
    From brazil (:

    Pascal pownz!

  2. #2

    paint each side of a cube with dif. color?

    I use this code to render a cube. You can call "glColor3f" after each face to render it in a different color.
    [pascal]
    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;
    [/pascal]

  3. #3

    paint each side of a cube with dif. color?

    thanks, it solves the problem with the colored faces, but what about colored borders?
    From brazil (:

    Pascal pownz!

  4. #4

    paint each side of a cube with dif. color?

    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.

  5. #5

    paint each side of a cube with dif. color?

    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.

  6. #6

    paint each side of a cube with dif. color?

    Quote Originally Posted by User137
    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
    From brazil (:

    Pascal pownz!

  7. #7

    paint each side of a cube with dif. color?

    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?
    From brazil (:

    Pascal pownz!

  8. #8

    paint each side of a cube with dif. color?

    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.

    [pascal]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;
    [/pascal]

  9. #9

    paint each side of a cube with dif. color?

    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)

    [pascal] 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;[/pascal]

    ps: in this code i rneder 2 cubes
    From brazil (:

    Pascal pownz!

  10. #10

    paint each side of a cube with dif. color?

    Quote Originally Posted by arthurprs
    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.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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