PDA

View Full Version : how to : draw & throw a dice ?



ahamedos
12-01-2012, 05:32 AM
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:p
--
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 ...

V-man
12-01-2012, 07:09 AM
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.

ahamedos
12-02-2012, 05:22 AM
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

Decesive
12-02-2012, 09:56 AM
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.

newbiecow
12-03-2012, 05:09 AM
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.

ahamedos
12-05-2012, 05:19 AM
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




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_FILTE R,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,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);
};

Decesive
12-05-2012, 10:59 AM
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 :


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 ; )

ahamedos
12-06-2012, 01:46 AM
i've done it (Rotating Cube + Dice Textured),

i used this image (divided it into 6 parts)

http://www.geminoidi.com/images/dice_texture_uv_map.jpg

here is another one

http://www.potatoland.com/glart/week5/images/cube_uvw_map_dice.png

thanks for your reply s

this is the code if anyone is interested
Loading the Textures (6 in total)



void LoadGLTextures() {
// Load Texture
Image *image[6];
int i;
for(i=0;i<6;i++){
// allocate space for texture
image[i] = (Image *) malloc(sizeof(Image));
if (image[i] == NULL) {
printf("Error allocating space for image");
exit(0);
}
}
if ((!ImageLoad("Data/01.bmp", image[0])) || (!ImageLoad("Data/02.bmp", image[1])) || (!ImageLoad("Data/03.bmp", image[2])) || (!ImageLoad("Data/04.bmp", image[3])) || (!ImageLoad("Data/05.bmp", image[4])) || (!ImageLoad("Data/06.bmp", image[5]))) {
exit(1);
}
for(i=0;i<6;i++){
// Create Texture
glGenTextures(1, &texture[i]);
glBindTexture(GL_TEXTURE_2D, texture[i]);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR); // scale linearly when image smalled than texture

glTexImage2D(GL_TEXTURE_2D, 0, 3, image[i]->sizeX, image[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image[i]->data);
}
};



now I will get into the physics engine (The Hard Part)

newbiecow
12-07-2012, 04:01 AM
Anything hard is also hard to learn. I think you may have to make references to some chapters of mechanics if you want to simulate accurately.

newbiecow
12-07-2012, 04:41 AM
So, if you'd like to do everything manually, you have to learn a lot and become harder and harder and harder.

newbiecow
12-07-2012, 04:44 AM
Well, but I think at first you just do something for fun and give some beginner's hint.

newbiecow
12-08-2012, 08:45 AM
Ok, if I were you and just do something for fun, I will do as the following.

Every instant, to record the position of the center of the dice(three float values), the velocity of its move's direction(three float values), the direction of its axis of rotation(three float values), the angular velocity of its rotation(one float value). Any time, the six vertices of the dice's postion can be caculated by the stored values. If the lowest vertex is very close the ground plane(supposing being a plane) say the distance of them is below some threshold value, then a collision detected. Making some calculation to rectify the above saved value, then to the next instant. Until the velocity of movement and angular velocity of rotation become below a threshold value, then movement of the dice ended.

Best Regards,

newbiecow

newbiecow
12-09-2012, 08:13 AM
Ok, if I were you and just do something for fun, I will do as the following.

Every instant, to record the position of the center of the dice(three float values), the velocity of its move's direction(three float values), the direction of its axis of rotation(three float values), the angular velocity of its rotation(one float value). Any time, the six vertices of the dice's postion can be caculated by the stored values. If the lowest vertex is very close the ground plane(supposing being a plane) say the distance of them is below some threshold value, then a collision detected. Making some calculation to rectify the above saved value, then to the next instant. Until the velocity of movement and angular velocity of rotation become below a threshold value, then movement of the dice ended.

Best Regards,

newbiecow

I'm very sorry to neglect to indicate storing another important float value for each instant, that is the initial angle of the dice relative to its current axis of rotation, it can be calculated through one definite point of the dice, for instance, one vertex relative to some initial vector, for instance, the cross product of the vector of the rotation axis and the outward vector, for instance.

Best Regards,

newbiecow

newbiecow
12-12-2012, 11:14 PM
Of course, if you consider the friction of the surface, this simulation even becomes much harder.
But for fun, I think the above is adequate.

Best Regards,

newbiecow