/** \brief Perform the basic scenegraph initialization * * Initializes the PLIB SSG library, creates the scenegraph * root, a rendering context and the sun. */ void initialize_scenegraph() { // Initialize SSG ssgInit(); // add to SSG function for read JPEG Textures ::ssgAddTextureFormat ( ".jpg", ssgLoadJPG); // font puSetDefaultFonts ( FONT_HELVETICA_14, FONT_HELVETICA_14 ); // Some basic OpenGL setup sgVec4 skycol; sgSetVec4 ( skycol, 0.0f, 0.0f, 0.0f, 1.0f ) ; glClearColor ( skycol[0], skycol[1], skycol[2], skycol[3] ) ; glEnable ( GL_DEPTH_TEST ) ; // Set up the viewing parameters context = new ssgContext(); context->setFOV ( 35.0f, 0 ) ; context->setNearFar ( 1.0f, 10000.0f ) ; context->makeCurrent(); ssgModelPath(""); ssgTexturePath("textures"); // Create a root node scene = new ssgRoot(); // Set up a light source sgVec4 lightamb; sgSetVec4(lightamb , 0.2f, 0.2f, 0.2f, 1.0f); ssgGetLight(0)->setPosition(lightposn); ssgGetLight(0)->setColour(GL_AMBIENT, lightamb); ssgGetLight(0)->setPosition(lightposn); }
/** \brief The per-frame OpenGL display routine * * This function does the complete OpenGL drawing. First * the 3D scene is drawn, then some 2D overlays * (wind and battery indicator, GUI). */ void display() { CRRCMath::Vector3 plane_pos = FDM2Graphics(Global::aircraft->getPos()); // Prepare the current frame buffer and reset // the modelview matrix (for non-SSG drawing) GLbitfield clearmask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; if (vidbits.stencil) { clearmask |= GL_STENCIL_BUFFER_BIT; } glClear(clearmask); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); //~ glBlendFunc(GL_ONE, GL_ZERO); //~ glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); // Set up the viewing transformation (for SSG drawing) sgVec3 viewpos, planepos, up; sgSetVec3(viewpos, player_pos.r[0], player_pos.r[1], player_pos.r[2]); sgSetVec3(planepos, looking_pos.r[0], looking_pos.r[1], looking_pos.r[2]); sgSetVec3(up, 0.0, 1.0, 0.0); context->setCameraLookAt(viewpos, planepos, up); // 3D scene if( Global::scenery != NULL) { // 3D scene: sky sphere draw_sky(&viewpos, Global::Simulation->getTotalTime()); // 3D scene: airplane if (Global::aircraft->getModel() != NULL) { // For SSG rendering, this call does not draw anything, // but calculates the airplane's transformation matrix glDisable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); Global::aircraft->getModel()->draw(Global::aircraft->getFDM()); } // 3D scene: scenery Global::scenery->draw(Global::Simulation->getTotalTime()); } // Lighting setup. Only needed as long as there are // non-SSG parts that modify the light sources. sgVec4 lightamb; sgSetVec4(lightamb , 0.2f, 0.2f, 0.2f, 1.0f); ssgGetLight(0)->setPosition(lightposn); ssgGetLight(0)->setColour(GL_AMBIENT, lightamb); // Draw the scenegraph (airplane model, shadow) ssgCullAndDraw(scene); context->forceBasicState(); // ssgCullAndDraw() ends up with an identity modelview matrix, // so we have to set up our own viewing transformation for // the thermals glLoadIdentity(); gluLookAt(player_pos.r[0], player_pos.r[1], player_pos.r[2], looking_pos.r[0], looking_pos.r[1], looking_pos.r[2], 0.0, 1.0, 0.0); if (Global::training_mode==TRUE) { draw_thermals(Global::aircraft->getPos()); } // 3D scene: game-mode-specific stuff (pylons etc.) Global::gameHandler->draw(); glPopMatrix(); // Overlay: game handler Global::gameHandler->display_infos(window_xsize, window_ysize); // Overlay: scope for audio interface if ( Global::testmode.test_mode && (Global::TXInterface->inputMethod() == T_TX_Interface::eIM_audio) ) { GlOverlay::setupRenderingState(window_xsize, window_ysize); oscillo(); GlOverlay::restoreRenderingState(); } // Overlay: wind direction indicator { double dx = (plane_pos.r[2] - player_pos.r[2]); double dy = (player_pos.r[0] - plane_pos.r[0]); double dir = atan2(dy, dx); GlOverlay::setupRenderingState(window_xsize, window_ysize); draw_wind(dir); GlOverlay::restoreRenderingState(); } // Overlay: battery capacity/fuel left { int r = window_ysize >> 5; int w = r >> 1; int h = window_ysize >> 3; int ht = (int)(Global::aircraft->getFDM()->getBatCapLeft() * h); #if 0 glDisable(GL_LIGHTING); glMatrixMode (GL_PROJECTION); glPushMatrix(); glLoadIdentity (); gluOrtho2D (0, window_xsize-1, 0, window_ysize); #endif GlOverlay::setupRenderingState(window_xsize, window_ysize); // Background glColor3f (0, 0, 0); glRectf(window_xsize-w, r+ht, window_xsize-1, r+h); glTranslatef(0,0,0.1); // Indicator glColor3f (0, 1, 0.); glRectf(window_xsize-w, r, window_xsize-1, r+ht); #if 0 glPopMatrix(); glEnable(GL_LIGHTING); glMatrixMode(GL_MODELVIEW); #endif GlOverlay::restoreRenderingState(); } // Overlay: console console->render(window_xsize, window_ysize); // Overlay: gui Global::gui->draw(); // check for any OpenGL errors evaluateOpenGLErrors(); // Force pipeline flushing and flip front and back buffer glFlush(); SDL_GL_SwapBuffers(); }
int grInitScene(void) { void *hndl = grTrackHandle; ssgLight * light = ssgGetLight(0); GLfloat mat_specular[] = {0.3, 0.3, 0.3, 1.0}; GLfloat mat_shininess[] = {50.0}; GLfloat light_position[] = {0, 0, 200, 0.0}; GLfloat lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0}; GLfloat lmodel_diffuse[] = {0.8, 0.8, 0.8, 1.0}; GLfloat fog_clr[] = {1.0, 1.0, 1.0, 0.5}; if (grHandle==NULL) { sprintf(buf, "%s%s", GetLocalDir(), GR_PARAM_FILE); grHandle = GfParmReadFile(buf, GFPARM_RMODE_STD | GFPARM_RMODE_CREAT); } mat_specular[0] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_SPEC_R, NULL, mat_specular[0]); mat_specular[1] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_SPEC_G, NULL, mat_specular[1]); mat_specular[2] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_SPEC_B, NULL, mat_specular[2]); lmodel_ambient[0] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_AMBIENT_R, NULL, lmodel_ambient[0]); lmodel_ambient[1] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_AMBIENT_G, NULL, lmodel_ambient[1]); lmodel_ambient[2] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_AMBIENT_B, NULL, lmodel_ambient[2]); lmodel_diffuse[0] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_DIFFUSE_R, NULL, lmodel_diffuse[0]); lmodel_diffuse[1] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_DIFFUSE_G, NULL, lmodel_diffuse[1]); lmodel_diffuse[2] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_DIFFUSE_B, NULL, lmodel_diffuse[2]); mat_shininess[0] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_SHIN, NULL, mat_shininess[0]); light_position[0] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_LIPOS_X, NULL, light_position[0]); light_position[1] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_LIPOS_Y, NULL, light_position[1]); light_position[2] = GfParmGetNum(hndl, TRK_SECT_GRAPH, TRK_ATT_LIPOS_Z, NULL, light_position[2]); glShadeModel(GL_SMOOTH); light->setPosition(light_position[0],light_position[1],light_position[2]); light->setColour(GL_AMBIENT,lmodel_ambient); light->setColour(GL_DIFFUSE,lmodel_diffuse); light->setColour(GL_SPECULAR,mat_specular); light->setSpotAttenuation(0.0, 0.0, 0.0); sgCopyVec3 (fog_clr, grTrack->graphic.bgColor); sgScaleVec3 (fog_clr, 0.8); glFogi(GL_FOG_MODE, GL_LINEAR); glFogfv(GL_FOG_COLOR, fog_clr); glFogf(GL_FOG_DENSITY, 0.05); glHint(GL_FOG_HINT, GL_DONT_CARE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); if (!sun) { ssgaLensFlare *sun_obj = NULL ; sun_obj = new ssgaLensFlare () ; sun = new ssgTransform ; sun -> setTransform ( light_position ) ; sun -> addKid ( sun_obj ) ; SunAnchor-> addKid(sun) ; } /* GUIONS GL_TRUE */ glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE); #ifdef GL_SEPARATE_SPECULAR_COLOR glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,GL_SEPARATE_SPECULAR_COLOR); #else #ifdef GL_SEPARATE_SPECULAR_COLOR_EXT glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT,GL_SEPARATE_SPECULAR_COLOR_EXT); #endif #endif return 0; }