1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| #include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <ode/ode.h>
int LightPos[4] = {0,3,5,1};
static dWorldID world;
static dSpaceID space;
static dGeomID sol;
static dJointGroupID contactgroup;
dGeomID geom_sphere;
dBodyID body_sphere;
void set_transf(const dReal *pos,const dReal *R)
{
GLfloat local_matrix[16];
local_matrix[0] = R[0];
local_matrix[1] = R[4];
local_matrix[2] = R[8];
local_matrix[3] = 0;
local_matrix[4] = R[1];
local_matrix[5] = R[5];
local_matrix[6] = R[9];
local_matrix[7] = 0;
local_matrix[8] = R[2];
local_matrix[9] = R[6];
local_matrix[10] = R[10];
local_matrix[11] = 0;
local_matrix[12] = pos[0];
local_matrix[13] = pos[1];
local_matrix[14] = pos[2];
local_matrix[15] = 1;
glMultMatrixf(local_matrix);
}
static void nearCallback(void *data, dGeomID o1, dGeomID o2)
{
int i;
// exit without doing anything if the two bodies are connected by a joint
dBodyID b1 = dGeomGetBody(o1);
dBodyID b2 = dGeomGetBody(o2);
if (b1 && b2 && dAreConnected (b1,b2)) return;
dContact contact[3]; // up to 3 contacts per box
for (i=0; i<3; i++)
{
contact[i].surface.mode = dContactBounce | dContactSoftCFM;
contact[i].surface.mu = 50;
contact[i].surface.soft_cfm = 0.01;
}
if (int numc = dCollide (o1,o2,3,&contact[0].geom,sizeof(dContact)))
{
for (i=0; i<numc; i++)
{
dJointID c = dJointCreateContact (world,contactgroup,contact+i);
dJointAttach (c,b1,b2);
}
}
}
void ode_loop()
{
dSpaceCollide(space,0,&nearCallback);
dWorldQuickStep(world,0.01);
}
void ode_init()
{
world = dWorldCreate();
space = dHashSpaceCreate(0);
dWorldSetGravity(world,0,-0.2,0);
sol = dCreatePlane (space,0,1,0,0);
contactgroup = dJointGroupCreate (0);
dMass m;
dMassSetSphere(&m,1,1);
dMassAdjust(&m,1);
body_sphere = dBodyCreate(world);
dBodySetMass(body_sphere,&m);
geom_sphere = dCreateSphere(space,0.5);
dGeomSetBody(geom_sphere,body_sphere);
dBodySetPosition(body_sphere,0,2,0);
}
void terrain()
{
glPushMatrix();
glBegin(GL_QUADS);
glColor3d(0,0,1);
glVertex3i(-1,0,-1);
glVertex3i(-1,0,1);
glVertex3i(1,0,1);
glVertex3i(1,0,-1);
glEnd();
glPopMatrix();
}
void balle()
{
glPushMatrix();
glColor3d(0,1,0);
set_transf(dBodyGetPosition(body_sphere),dBodyGetRotation(body_sphere));
glutSolidSphere(0.2,25,25);
glPushMatrix();
}
void InitGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glMateriali(GL_FRONT_AND_BACK,GL_SHININESS,100);
glLightiv(GL_LIGHT0,GL_POSITION,LightPos);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(3,2,3,0,1,0,0,1,0);
ode_loop();
terrain();
balle();
glutSwapBuffers();
glutPostRedisplay();
}
void Reshape(int width, int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,float(width)/float(height),0.1,100);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(1024,768);
glutCreateWindow("test ODE");
glutReshapeFunc(Reshape);
glutDisplayFunc(Draw);
InitGL();
ode_init();
glutMainLoop();
dWorldDestroy(world);
dSpaceDestroy(space);
dCloseODE();
return 0;
} |
Partager