Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 14

Thread: how to : draw & throw a dice ?

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    4

    how to : draw & throw a dice ?

    hi,

    i'm now to the forum,
    and have only few basics in C++ OpenGL
    like rotating a colored cube (each side with different color)
    (Very basic thing) i guess
    --
    anyway,

    i've searched in google and here, on how to draw a Dice, and throw it to land on a random number (1-6)
    but i couldn't find an explanation on how to do it,

    anyone can help me on how to do it ?
    what do i need to do here ...

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    You have 2 options:
    1. preprogrammed : you generated an animation file that contains the position and orientation at different time steps. Use a tool like Blender, 3D studio max oe Maya to animate it.
    2. use a physics engine : ODE. Bullet, Newton. You still need to have a random starting position and orientation but at least the actual physics is handled by the physics engine.
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    4
    sorry for late reply, i was searching for the second option (ODE & Bullet)
    i don't know anything about it,
    i understood that all i need is a header files in addition to the original program file..
    is that right ?

    another question about the Dice

    all i can do is write the cube. and color it
    how can i write the numbers (points)
    in each surface of the cube to make look like a dice


    -----
    thanks for your reply V-man

  4. #4
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    11
    Quote Originally Posted by ahamedos View Post
    sorry for late reply, i was searching for the second option (ODE & Bullet)
    i don't know anything about it,
    i understood that all i need is a header files in addition to the original program file..
    is that right ?

    another question about the Dice

    all i can do is write the cube. and color it
    how can i write the numbers (points)
    in each surface of the cube to make look like a dice


    -----
    thanks for your reply V-man
    Well, You'll obviously have to use texture for this, create it in Paint, Photoshop, Gimp, whatever program you like, load it to the GPU send it to shader and render it on the Cube ; )
    Those would be six textures, one for each side describing the sides number. I believe you can use SOIL to load the images to GPU memory for the OpenGL to use it, then you'll have to create uniforms for it, I'm also learning so I might have said something wrong, pardon me for it, and best of luck ; )

    P.S. Find out more about textures and texturing here - > http://en.wikibooks.org/wiki/OpenGL_Programming , if I may say so, this is the best set of tutorials I could find on pretty modern OpenGL.

  5. #5
    Intern Contributor
    Join Date
    Dec 2012
    Posts
    74
    1.Make six images each containing dots of 1 to 6 as textures mapped to the dice's each face.
    2.Make a physical simulation of the freely falling and bouncing.

  6. #6
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    4
    I've got the six images . Bmp

    as for loading Texture code
    what i got is only one image, how do i repeat it to get the others ?

    my code looks like this

    Code :
     
    void LoadGLTextures() {	
        // Load Texture
        Image *image1;
     
        // allocate space for texture
        image1 = (Image *) malloc(sizeof(Image));
        if (image1 == NULL) {
    	printf("Error allocating space for image");
    	exit(0);
        }
     
        if (!ImageLoad("Data/01.bmp", image1)) {
    	exit(1);
        }        
     
        // Create Texture	
        glGenTextures(1, &texture[0]);
        glBindTexture(GL_TEXTURE_2D, texture[0]);   // 2d texture (x and y size)
     
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
     
        // 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image, 
        // border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
        glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
    };

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    11
    I can't say this code is correct, because I haven't tried to load textures yet (didn't need it for my tests), as for loading textures, if this function works well, You need to make this function more versatile, meaning - You should pass information about the location of the image to the function like so :
    Code :
    void LoadTexture(const char* filename) 
    {
        //Some code
    }
    , then somewhere else in the code ( probably in init function - if You have one, or just in a place that you set all the values ) load all the textures you want, but from what I know you'll have to figure out how to send them to the shader, and that means that You'll have to store the information about the created texture and bind the data about that texture to a uniform and send that uniform to the shader ; )

Tags for this Thread

Posting Permissions

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