1 /*****************************************************************************
2  * Copyright 1999,
3  *****************************************************************************
4  *     File: mesh.cpp
5  *   Author: Duane Marhefka
6  *  Project: DynaMechs 3.0 - GLUT example
7  *  Created:
8  *  Summary:
9  *      $Id:
10  *****************************************************************************/
11 
12 #include <GL/glut.h>
13 
14 #include <dmGLMouse.hpp>
15 #include <dmGLPolarCamera_zup.hpp>
16 
17 #include <dm.h>
18 #include <dmSystem.hpp>
19 #include <dmEnvironment.hpp>
20 #include <dmIntegRK45.hpp>
21 
22 #include <dmu.h>
23 
24 dmGLMouse *mouse;
25 dmGLPolarCamera_zup *camera;
26 GLfloat view_mat[4][4];
27 
28 Float idt, actual_idt;
29 int paused_flag = false;
30 
31 dmSystem *G_robot;
32 dmIntegRK45 *G_integrator;
33 
34 int render_rate;
35 int timer_count = 0;
36 
37 //----------------------------------------------------------------------------
38 //    Summary: Initialize material property and light source.
39 // Parameters:
40 //    Returns:
41 //----------------------------------------------------------------------------
myinit(void)42 void myinit (void)
43 {
44    GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
45    GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
46    GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
47    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
48 
49    glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
50    glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
51    glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
52    glLightfv (GL_LIGHT0, GL_POSITION, light_position);
53 
54    glEnable (GL_LIGHTING);
55    glEnable (GL_LIGHT0);
56    glDepthFunc(GL_LESS);
57    glEnable(GL_DEPTH_TEST);
58 
59    glShadeModel(GL_FLAT);
60    glEnable(GL_CULL_FACE);
61    glCullFace(GL_BACK);
62 }
63 //----------------------------------------------------------------------------
64 //    Summary:
65 // Parameters:
66 //    Returns:
67 //----------------------------------------------------------------------------
display(void)68 void display (void)
69 {
70    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
71 
72    glMatrixMode (GL_MODELVIEW);
73    glPushMatrix ();
74 
75    // ===============================================================
76    (dmEnvironment::getEnvironment())->draw();
77 
78    glPushAttrib(GL_ALL_ATTRIB_BITS);
79    G_robot->draw();
80    glPopAttrib();
81    // ===============================================================
82 
83    glPopMatrix ();
84 
85    glFlush ();
86    glutSwapBuffers();
87 }
88 
89 //----------------------------------------------------------------------------
90 //    Summary:
91 // Parameters:
92 //    Returns:
93 //----------------------------------------------------------------------------
myReshape(int w,int h)94 void myReshape(int w, int h)
95 {
96    glViewport (0, 0, w, h);
97    mouse->win_size_x = w;
98    mouse->win_size_y = h;
99 
100    camera->setPerspective(45.0, (GLfloat)w/(GLfloat)h, 1.0, 200.0);
101 
102    camera->setViewMat(view_mat);
103    camera->applyView();
104 }
105 
106 //----------------------------------------------------------------------------
107 //    Summary:
108 // Parameters:
109 //    Returns:
110 //----------------------------------------------------------------------------
processKeyboard(unsigned char key,int,int)111 void processKeyboard(unsigned char key, int, int)
112 {
113    switch (key)
114    {
115       case 27:
116          glutDestroyWindow(glutGetWindow());
117          exit(1);
118          break;
119 
120       case 'p':
121          paused_flag = !paused_flag;
122          break;
123    }
124 }
125 
126 
127 //----------------------------------------------------------------------------
128 //    Summary:
129 // Parameters:
130 //    Returns:
131 //----------------------------------------------------------------------------
updateSim()132 void updateSim()
133 {
134    if (!paused_flag)
135      {
136        actual_idt = idt;
137        G_integrator->simulate(actual_idt);
138      }
139 
140    camera->update(mouse);
141    camera->applyView();
142 
143    display();
144 }
145 
146 //----------------------------------------------------------------------------
147 //    Summary:
148 // Parameters:
149 //    Returns:
150 //----------------------------------------------------------------------------
main(int argc,char ** argv)151 int main(int argc, char** argv)
152 {
153    glutInit(&argc, argv);
154 
155    //=========================
156    char *filename = "mesh.cfg";
157    if (argc > 1)
158    {
159       filename = argv[1];
160    }
161 
162    glutInitWindowSize(480, 360);
163    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
164    glutCreateWindow("Spherical Joint Mesh");
165 
166    myinit();
167    mouse = dmGLMouse::dmInitGLMouse();
168 
169    camera = new dmGLPolarCamera_zup();
170    camera->setRadius(10.0);
171    camera->setCOI(2.0, 2.0, 2.5);
172    camera->setTranslationScale(0.02f);
173 
174    // load robot stuff
175    ifstream cfg_ptr;
176    cfg_ptr.open(filename);
177 
178    // Read simulation timing information.
179    readConfigParameterLabel(cfg_ptr,"Integration_Stepsize");
180    cfg_ptr >> idt;
181    if (idt <= 0.0)
182    {
183       cerr << "main error: invalid integration stepsize: " << idt << endl;
184       exit(3);
185    }
186 
187    readConfigParameterLabel(cfg_ptr,"Display_Update_Rate");
188    cfg_ptr >> render_rate;
189    if (render_rate < 1) render_rate = 1;
190 
191 // ===========================================================================
192 // Initialize DynaMechs environment - must occur before any linkage systems
193    char env_flname[FILENAME_SIZE];
194    readConfigParameterLabel(cfg_ptr,"Environment_Parameter_File");
195    readFilename(cfg_ptr, env_flname);
196    dmEnvironment *environment = dmuLoadFile_env(env_flname);
197    environment->drawInit();
198    dmEnvironment::setEnvironment(environment);
199 
200 // ===========================================================================
201 // Initialize a DynaMechs linkage system
202    char robot_flname[FILENAME_SIZE];
203    readConfigParameterLabel(cfg_ptr,"Robot_Parameter_File");
204    readFilename(cfg_ptr, robot_flname);
205    G_robot = dmuLoadFile_dm(robot_flname);
206 
207    G_integrator = new dmIntegRK45();
208    G_integrator->addSystem(G_robot);
209    G_integrator->setMaxSteps(400);
210    G_integrator->setErrorBound((Float)1.e-4, (Float)1.e-4);
211 
212    glutReshapeFunc(myReshape);
213    glutKeyboardFunc(processKeyboard);
214    glutDisplayFunc(display);
215    glutIdleFunc(updateSim);
216 
217    cout << endl;
218    cout << "p - toggles dynamic simulation" << endl;
219    cout << "Use mouse to rotate/move/zoom the camera" << endl << endl;
220 
221    glutMainLoop();
222    return 0;             /* ANSI C requires main to return int. */
223 }
224