bool grLoadPngTexture (const char *fname, ssgTextureInfo* info) { GLubyte *tex; int w, h; int mipmap = 1; TRACE_GL("Load: grLoadPngTexture start"); tex = (GLubyte*)GfImgReadPng(fname, &w, &h, 2.0); if (!tex) { return false; } if (info) { info -> width = w; info -> height = h; info -> depth = 4; info -> alpha = true; } TRACE_GL("Load: grLoadPngTexture stop"); // TODO: Check if tex is freed. // Check/fix potential problems related to malloc/delete mixture // (instead of malloc/free or new/delete). mipmap = doMipMap(fname, mipmap); GLubyte* tex2 = new GLubyte[w*h*4]; memcpy(tex2, tex, w*h*4); free(tex); tex = tex2; return grMakeMipMaps(tex, w, h, 4, mipmap); }
void grDrawBackground(class cGrCamera *cam, class cGrBackgroundCam *bgCam) { TRACE_GL("grDrawBackground: ssgCullAndDraw start"); bgCam->update(cam); bgCam->action(); ssgCullAndDraw(TheBackground); TRACE_GL("grDrawBackground: ssgCullAndDraw"); }
int refresh(tSituation *s) { int i; START_PROFILE("refresh"); nFrame++; grCurTime = GfTimeClock(); grDeltaTime = grCurTime - OldTime; if ((grCurTime - OldTime) > 1.0) { /* The Frames Per Second (FPS) display is refreshed every second */ grFps = (tdble)nFrame / (grCurTime - OldTime); nFrame = 0; OldTime = grCurTime; } TRACE_GL("refresh: start"); START_PROFILE("grRefreshSound*"); grRefreshSound(s, grScreens[0]->getCurCamera()); STOP_PROFILE("grRefreshSound*"); START_PROFILE("grDrawBackground/glClear"); glDepthFunc(GL_LEQUAL); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); STOP_PROFILE("grDrawBackground/glClear"); for (i = 0; i < GR_NB_MAX_SCREEN; i++) { grScreens[i]->update(s, grFps); } grUpdateSmoke(s->currentTime); STOP_PROFILE("refresh"); return 0; }
void grDrawCar(tCarElt *car, tCarElt *curCar, int dispCarFlag, int dispDrvFlag, double curTime, class cGrPerspCamera *curCam) { sgCoord wheelpos; int index, i, j; static float maxVel[3] = { 20.0, 40.0, 70.0 }; float lod; TRACE_GL("cggrDrawCar: start"); index = car->index; if (car->priv.collision_state.collision_count > 0) { tCollisionState* collision_state = &car->priv.collision_state; grPropagateDamage (grCarInfo[index].carEntity, collision_state->pos, collision_state->force, 0); collision_state->collision_count = 0; } grCarInfo[index].distFromStart=grGetDistToStart(car); grCarInfo[index].envAngle=RAD2DEG(car->_yaw); if ((car == curCar) && (dispCarFlag != 1)) { grCarInfo[index].LODSelector->select(0); } else { lod = curCam->getLODFactor(car->_pos_X, car->_pos_Y, car->_pos_Z); i = 0; while (lod < grCarInfo[index].LODThreshold[i] * grLodFactorValue) { i++; } if ((car->_state & RM_CAR_STATE_DNF) && (grCarInfo[index].LODThreshold[i] > 0.0)) { i++; } grCarInfo[index].LODSelector->select(grCarInfo[index].LODSelectMask[i]); if (dispDrvFlag) { grCarInfo[index].driverSelector->select(1); } else { grCarInfo[index].driverSelector->select(0); } } sgCopyMat4(grCarInfo[index].carPos, car->_posMat); grCarInfo[index].px=car->_pos_X; grCarInfo[index].py=car->_pos_Y; grCarInfo[index].carTransform->setTransform(grCarInfo[index].carPos); if ((car == curCar) && (dispCarFlag != 1)) { grDrawShadow(car, 0); } else { grDrawShadow(car, 1); } grUpdateSkidmarks(car, curTime); grDrawSkidmarks(car); grAddSmoke(car, curTime); if ((car == curCar) && (dispCarFlag != 1)) { grUpdateCarlight(car, curCam, 0); } else { grUpdateCarlight(car, curCam, 1); } /* Env mapping selection by the position on the track */ grCarInfo[index].envSelector->selectStep(car->_trkPos.seg->envIndex); /* wheels */ for (i = 0; i < 4; i++) { float *clr; sgSetCoord(&wheelpos, car->priv.wheel[i].relPos.x, car->priv.wheel[i].relPos.y, car->priv.wheel[i].relPos.z, RAD2DEG(car->priv.wheel[i].relPos.az), RAD2DEG(car->priv.wheel[i].relPos.ax), 0); grCarInfo[index].wheelPos[i]->setTransform(&wheelpos); sgSetCoord(&wheelpos, 0, 0, 0, 0, 0, RAD2DEG(car->priv.wheel[i].relPos.ay)); grCarInfo[index].wheelRot[i]->setTransform(&wheelpos); for (j = 0; j < 3; j++) { if (fabs(car->_wheelSpinVel(i)) < maxVel[j]) break; } grCarInfo[index].wheelselector[i]->select(1<<j); clr = grCarInfo[index].brkColor[i]->get(0); clr[0] = 0.1 + car->_brakeTemp(i) * 1.5; clr[1] = 0.1 + car->_brakeTemp(i) * 0.3; clr[2] = 0.1 - car->_brakeTemp(i) * 0.3; } /* push the car at the end of the display order */ CarsAnchorTmp->addKid(grCarInfo[index].carTransform); CarsAnchor->removeKid(grCarInfo[index].carTransform); CarsAnchor->addKid(grCarInfo[index].carTransform); CarsAnchorTmp->removeKid(grCarInfo[index].carTransform); TRACE_GL("cggrDrawCar: end"); }
void grInitCar(tCarElt *car) { const int BUFSIZE=4096; char buf[BUFSIZE]; int index; int selIndex; ssgEntity *carEntity; ssgSelector *LODSel; ssgTransform *wheel[4]; int nranges; int i, j; void *handle; const char *param; int lg; const int PATHSIZE=256; char path[PATHSIZE]; myLoaderOptions options; sgVec3 lightPos; int lightNum; const char *lightType; int lightTypeNum; if (!CarsAnchorTmp) { CarsAnchorTmp = new ssgBranch(); } grInitBoardCar(car); TRACE_GL("loadcar: start"); ssgSetCurrentOptions ( &options ) ; grCarIndex = index = car->index; /* current car's index */ handle = car->_carHandle; /* Load visual attributes */ car->_exhaustNb = GfParmGetEltNb(handle, SECT_EXHAUST); car->_exhaustNb = MIN(car->_exhaustNb, 2); car->_exhaustPower = GfParmGetNum(handle, SECT_EXHAUST, PRM_POWER, NULL, 1.0); for (i = 0; i < car->_exhaustNb; i++) { snprintf(path, PATHSIZE, "%s/%d", SECT_EXHAUST, i + 1); car->_exhaustPos[i].x = GfParmGetNum(handle, path, PRM_XPOS, NULL, -car->_dimension_x / 2.0); car->_exhaustPos[i].y = -GfParmGetNum(handle, path, PRM_YPOS, NULL, car->_dimension_y / 2.0); car->_exhaustPos[i].z = GfParmGetNum(handle, path, PRM_ZPOS, NULL, 0.1); } snprintf(path, PATHSIZE, "%s/%s", SECT_GROBJECTS, SECT_LIGHT); lightNum = GfParmGetEltNb(handle, path); for (i = 0; i < lightNum; i++) { snprintf(path, PATHSIZE, "%s/%s/%d", SECT_GROBJECTS, SECT_LIGHT, i + 1); lightPos[0] = GfParmGetNum(handle, path, PRM_XPOS, NULL, 0); lightPos[1] = GfParmGetNum(handle, path, PRM_YPOS, NULL, 0); lightPos[2] = GfParmGetNum(handle, path, PRM_ZPOS, NULL, 0); lightType = GfParmGetStr(handle, path, PRM_TYPE, ""); lightTypeNum = LIGHT_NO_TYPE; if (!strcmp(lightType, VAL_LIGHT_HEAD1)) { lightTypeNum = LIGHT_TYPE_FRONT; } else if (!strcmp(lightType, VAL_LIGHT_HEAD2)) { lightTypeNum = LIGHT_TYPE_FRONT2; } else if (!strcmp(lightType, VAL_LIGHT_BRAKE)) { lightTypeNum = LIGHT_TYPE_BRAKE; } else if (!strcmp(lightType, VAL_LIGHT_BRAKE2)) { lightTypeNum = LIGHT_TYPE_BRAKE2; } else if (!strcmp(lightType, VAL_LIGHT_REAR)) { lightTypeNum = LIGHT_TYPE_REAR; } grAddCarlight(car, lightTypeNum, lightPos, GfParmGetNum(handle, path, PRM_SIZE, NULL, 0.2)); } grLinkCarlights(car); GfOut("[gr] Init(%d) car %s for driver %s index %d\n", index, car->_carName, car->_modName, car->_driverIndex); grFilePath = (char*)malloc(BUFSIZE); lg = 0; lg += snprintf(grFilePath + lg, BUFSIZE - lg, "drivers/%s/%d/%s;", car->_modName, car->_driverIndex, car->_carName); lg += snprintf(grFilePath + lg, BUFSIZE - lg, "drivers/%s/%d;", car->_modName, car->_driverIndex); lg += snprintf(grFilePath + lg, BUFSIZE - lg, "drivers/%s/%s;", car->_modName, car->_carName); lg += snprintf(grFilePath + lg, BUFSIZE - lg, "drivers/%s;", car->_modName); lg += snprintf(grFilePath + lg, BUFSIZE - lg, "cars/%s", car->_carName); param = GfParmGetStr(handle, SECT_GROBJECTS, PRM_WHEEL_TEXTURE, ""); if (strlen(param) != 0) { grGammaValue = 1.8; grMipMap = 0; grCarInfo[index].wheelTexture = grSsgLoadTexState(param); } grCarInfo[index].envSelector = (ssgStateSelector*)grEnvSelector->clone(); grCarInfo[index].envSelector->ref(); /* the base transformation of the car (rotation + translation) */ grCarInfo[index].carTransform = new ssgTransform; DBG_SET_NAME(grCarInfo[index].carTransform, car->_modName, index, -1); /* Level of details */ grCarInfo[index].LODSelector = LODSel = new ssgSelector; grCarInfo[index].carTransform->addKid(LODSel); snprintf(path, PATHSIZE, "%s/%s", SECT_GROBJECTS, LST_RANGES); nranges = GfParmGetEltNb(handle, path) + 1; if (nranges < 2) { GfOut("Error not enough levels of detail\n"); FREEZ(grFilePath); return; } /* First LOD */ ssgBranch *carBody = new ssgBranch; DBG_SET_NAME(carBody, "LOD", index, 0); LODSel->addKid(carBody); /* The car's model is under cars/<model> */ snprintf(buf, BUFSIZE, "cars/%s", car->_carName); ssgModelPath(buf); snprintf(buf, BUFSIZE, "drivers/%s/%d;drivers/%s;cars/%s", car->_modName, car->_driverIndex, car->_modName, car->_carName); ssgTexturePath(buf); grTexturePath = strdup(buf); /* loading raw car level 0*/ selIndex = 0; /* current selector index */ snprintf(buf, BUFSIZE, "%s.ac", car->_carName); /* default car name */ snprintf(path, PATHSIZE, "%s/%s/1", SECT_GROBJECTS, LST_RANGES); param = GfParmGetStr(handle, path, PRM_CAR, buf); grCarInfo[index].LODThreshold[selIndex] = GfParmGetNum(handle, path, PRM_THRESHOLD, NULL, 0.0); /*carEntity = ssgLoad(param);*/ carEntity = grssgCarLoadAC3D(param, NULL, index); grCarInfo[index].carEntity = carEntity; /* Set a selector on the driver */ char* stmp = strdup("DRIVER"); ssgBranch *b = (ssgBranch *)carEntity->getByName(stmp); free(stmp); grCarInfo[index].driverSelector = new ssgSelector; if (b) { ssgBranch *bp = b->getParent(0); bp->addKid(grCarInfo[index].driverSelector); grCarInfo[index].driverSelector->addKid(b); bp->removeKid(b); grCarInfo[index].driverSelector->select(1); grCarInfo[index].driverSelectorinsg = true; } else { grCarInfo[index].driverSelectorinsg = false; } DBG_SET_NAME(carEntity, "Body", index, -1); carBody->addKid(carEntity); /* add wheels */ for (i = 0; i < 4; i++){ wheel[i] = initWheel(car, i); carBody->addKid(wheel[i]); } grCarInfo[index].LODSelectMask[0] = 1 << selIndex; /* car mask */ selIndex++; grCarInfo[index].sx=carTrackRatioX; grCarInfo[index].sy=carTrackRatioY; /* Other LODs */ for (i = 2; i < nranges; i++) { carBody = new ssgBranch; snprintf(buf, BUFSIZE, "%s/%s/%d", SECT_GROBJECTS, LST_RANGES, i); param = GfParmGetStr(handle, buf, PRM_CAR, ""); grCarInfo[index].LODThreshold[selIndex] = GfParmGetNum(handle, buf, PRM_THRESHOLD, NULL, 0.0); /* carEntity = ssgLoad(param); */ carEntity = grssgCarLoadAC3D(param, NULL, index);; DBG_SET_NAME(carEntity, "LOD", index, i-1); carBody->addKid(carEntity); if (!strcmp(GfParmGetStr(handle, buf, PRM_WHEELSON, "no"), "yes")) { /* add wheels */ for (j = 0; j < 4; j++){ carBody->addKid(wheel[j]); } } LODSel->addKid(carBody); grCarInfo[index].LODSelectMask[i-1] = 1 << selIndex; /* car mask */ selIndex++; } /* default range selection */ LODSel->select(grCarInfo[index].LODSelectMask[0]); CarsAnchor->addKid(grCarInfo[index].carTransform); //grCarInfo[index].carTransform->print(stdout, "-", 1); FREEZ(grTexturePath); FREEZ(grFilePath); TRACE_GL("loadcar: end"); }
int initCars(tSituation *s) { const int IDXSIZE = 16; char idx[IDXSIZE]; int index; int i; tCarElt *elt; void *hdle; const int BUFSIZE = 1024; char buf[1024]; TRACE_GL("initCars: start"); snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GR_PARAM_FILE); grHandle = GfParmReadFile(buf, GFPARM_RMODE_STD | GFPARM_RMODE_CREAT); grInitCommonState(); grInitCarlight(s->_ncars); grMaxDammage = (tdble)s->_maxDammage; grNbCars = s->_ncars; grCustomizePits(); grCarInfo = (tgrCarInfo*)calloc(s->_ncars, sizeof(tgrCarInfo)); for (i = 0; i < s->_ncars; i++) { elt = s->cars[i]; /* Shadow init (Should be done before the cars for display order) */ grInitShadow(elt); /* Skidmarks init */ grInitSkidmarks(elt); } grNbScreen = 0; for (i = 0; i < s->_ncars; i++) { elt = s->cars[i]; index = elt->index; hdle = elt->_paramsHandle; snprintf(idx, IDXSIZE, "Robots/index/%d", elt->_driverIndex); grCarInfo[index].iconColor[0] = GfParmGetNum(hdle, idx, "red", (char*)NULL, 0); grCarInfo[index].iconColor[1] = GfParmGetNum(hdle, idx, "green", (char*)NULL, 0); grCarInfo[index].iconColor[2] = GfParmGetNum(hdle, idx, "blue", (char*)NULL, 0); grCarInfo[index].iconColor[3] = 1.0; grInitCar(elt); if ((elt->_driverType == RM_DRV_HUMAN) && (grNbScreen < GR_NB_MAX_SCREEN)) { grScreens[grNbScreen]->setCurrentCar(elt); grNbScreen++; } } if (grNbScreen == 0) { grNbScreen = (int)GfParmGetNum(grHandle, GR_SCT_DISPMODE, GR_ATT_NB_SCREENS, NULL, 1.0); } for (i = 0; i < GR_NB_MAX_SCREEN; i++) { grScreens[i]->initCams(s); } TRACE_GL("initCars: end"); grInitSmoke(s->_ncars); grInitSound(s, s->_ncars); grAdaptScreenSize(); return 0; }
void grDrawScene(void) { TRACE_GL("refresh: ssgCullAndDraw start"); ssgCullAndDraw(TheScene); TRACE_GL("refresh: ssgCullAndDraw"); }