add loader cars on OSG by Gaetan

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@5045 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: 085da7286ac9246a1bfc4b15a24c28f0398f51d3
Former-commit-id: e5edb422e5240673f672f94dc9793bee070f8bc5
This commit is contained in:
torcs-ng 2012-11-13 19:24:25 +00:00
parent a755723a98
commit d7b9f73927
11 changed files with 385 additions and 36 deletions

View file

@ -1,10 +1,10 @@
INCLUDE(../../../../cmake/macros.cmake)
SET(OSGGRAPH_HEADERS AccGeode.h AccException.h ReaderWriterACC.h
OsgLoader.h OsgScene.h OsgMath.h OsgMain.h osggraph.h)
OsgLoader.h OsgScene.h OsgMath.h OsgMain.h osggraph.h OsgCar.h)
SET(OSGGRAPH_SOURCES AccGeode.cpp AccException.cpp ReaderWriterACC.cpp
OsgLoader.cpp OsgScene.cpp OsgMain.cpp osggraph.cpp)
OsgLoader.cpp OsgScene.cpp OsgMath.cpp OsgMain.cpp osggraph.cpp OsgCar.cpp)
ADD_INTERFACE_INCLUDEDIR()
ADD_SDLIB_INCLUDEDIR(portability math tgf tgfclient robottools)

View file

@ -0,0 +1,201 @@
#include <osg/MatrixTransform>
#include "OsgLoader.h"
#include "OsgCar.h"
#include "OsgMath.h"
/*float * flatten(float mat[4][4]){
float res[16];
for(int i=0;i<16;i++){
res[i] = mat[i/4][i%4];
GfOut("%d ",mat[i/4][i%4]);
if(i%4==3)
GfOut("\n");
}
GfOut("________\n");
return res;
}*/
osg::ref_ptr<osg::Node> SDCar::loadCar(tCarElt *car)
{
this->car = car;
// static const char* pszTexFileExt = ".png";
static const int nMaxTexPathSize = 4096;
char buf[nMaxTexPathSize];
int index;
int selIndex;
// ssgEntity *carEntity;
// ssgSelector *LODSel;
/* ssgBranchCb *branchCb; */
// ssgTransform *wheel[4];
// int nranges;
// int i, j;
void *handle;
const char *param;
int lg;
char path[256];
// grssgLoaderOptions options;
// sgVec3 lightPos;
// int lightNum;
// const char *lightType;
//int lightTypeNum;
//TRACE_GL("loadcar: start");
/*if (!CarsAnchorTmp) {
CarsAnchorTmp = new ssgBranch();
}*/
index = car->index; /* current car's index */
handle = car->_carHandle;
/* Initialize board */
//grInitBoardCar(car);
/* Schedule texture mapping if we are using a custom skin and/or a master 3D model */
const bool bMasterModel = strlen(car->_masterModel) != 0;
GfOut("[gr] Init(%d) car %s for driver %s index %d\n", index, car->_carName, car->_modName, car->_driverIndex);
lg = 0;
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "%sdrivers/%s/%d/%s;",
GfLocalDir(), car->_modName, car->_driverIndex, car->_carName);
if (bMasterModel)
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "%sdrivers/%s/%d/%s;",
GfLocalDir(), car->_modName, car->_driverIndex, car->_masterModel);
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "%sdrivers/%s/%d;",
GfLocalDir(), car->_modName, car->_driverIndex);
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "%sdrivers/%s/%s;",
GfLocalDir(), car->_modName, car->_carName);
if (bMasterModel)
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "%sdrivers/%s/%s;",
GfLocalDir(), car->_modName, car->_masterModel);
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "%sdrivers/%s;",
GfLocalDir(), car->_modName);
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "drivers/%s/%d/%s;",
car->_modName, car->_driverIndex, car->_carName);
if (bMasterModel)
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "drivers/%s/%d/%s;",
car->_modName, car->_driverIndex, car->_masterModel);
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "drivers/%s/%d;",
car->_modName, car->_driverIndex);
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "drivers/%s/%s;",
car->_modName, car->_carName);
if (bMasterModel)
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "drivers/%s/%s;",
car->_modName, car->_masterModel);
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "drivers/%s;", car->_modName);
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "cars/%s;", car->_carName);
if (bMasterModel)
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "cars/%s;", car->_masterModel);
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "data/objects;");
lg += snprintf(buf + lg, nMaxTexPathSize - lg, "data/textures");
// ssgModelPath(buf);
// ssgTexturePath(buf);
/* loading raw car level 0*/
selIndex = 0; /* current selector index */
snprintf(buf, nMaxTexPathSize, "%s.ac",
bMasterModel ? car->_masterModel : car->_carName); /* default car 3D model file */
snprintf(path, 256, "%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);
//GfOut("HEHEHHEHEHEHHEHHEH %s\n",path);
//carEntity = grssgCarLoadAC3D(param, NULL, index);
std::string strPath = GetDataDir();
sprintf(buf, "cars/%s/%s.acc", car->_carName, car->_carName);
strPath+=buf;
osgLoader loader;
//GfOut("Chemin Textures : %s\n", m_strTexturePath.c_str());
//loader.AddSearchPath(m_strTexturePath);
//osg::Node *pCar = loader.Load3dFile("/usr/local/share/games/speed-dreams-2/cars/mp1-cavallo-tr06/mp1-cavallo-tr06-lod1.acc");
osg::Node *pCar = loader.Load3dFile(strPath);
osg::Vec3 p;
p[0] = car->_pos_X;//+ car->_drvPos_x;
p[1] = car->_pos_Y;//+car->_drvPos_y;
p[2] = car->_pos_Z;//+car->_drvPos_z;
// osgXformPnt3(p, car->_posMat);
osg::ref_ptr<osg::MatrixTransform> transform1 = new osg::MatrixTransform;
/* transform1->setMatrix( osg::Matrix::translate(p[0],p[1], p[2]) );*/
// transform1->setMatrix( osg::Matrix(flatten(car->pub.posMat)) );
transform1->addChild(pCar);
//osgcars->addChild(transform1.get());
//GfOut("LE POINTEUR %d\n",osgcars.get());
GfOut("loaded car %d",pCar);
this->car_branch = transform1;
return this->car_branch;
}
void SDCar::updateCar()
{
osg::Vec3 p;
p[0] = car->_pos_X;//+ car->_drvPos_x;
p[1] = car->_pos_Y;//+car->_drvPos_y;
p[2] = car->_pos_Z;//+car->_drvPos_z;
osg::Matrix Ry = osg::Matrix::rotate(car->_yaw,osg::Z_AXIS);
osg::Matrix Rp = osg::Matrix::rotate(-car->_pitch,osg::X_AXIS);
osg::Matrix Rr = osg::Matrix::rotate(car->_roll,osg::Y_AXIS);
osg::Matrix T = osg::Matrix::translate(p[0],p[1], p[2]);
Rr.mult(Ry,Rr);
Rp.mult(Rr,Rp);
T.mult(Rp,T);
this->car_branch->setMatrix(T);
//this->car_branch->setMatrix(osg::Matrix(flatten(car->pub.posMat)));
}
void SDCars::addSDCar(SDCar * car)
{
the_cars.insert(the_cars.end(),car);
}
osg::ref_ptr<osg::Node> SDCars::loadCars(tSituation * pSituation)
{
tSituation *s = pSituation;
this->situation = pSituation;
for (int i = 0; i < s->_ncars; i++)
{
tCarElt* elt = s->cars[i];
SDCar * car = new SDCar;
this->addSDCar(car);
this->cars_branch->addChild(car->loadCar(elt));
}
return cars_branch;
}
void SDCars::updateCars()
{
std::vector<SDCar *>::iterator it;
for(it = the_cars.begin(); it!= the_cars.end(); it++)
{
(*it)->updateCar();
}
}

View file

@ -0,0 +1,36 @@
#ifndef _OSGCAR_H_
#define _OSGCAR_H_
#include <raceman.h>
#include <osg/Group>
#include <vector>
class SDCar
{
private :
osg::ref_ptr<osg::MatrixTransform> car_branch;
tCarElt *car;
public :
osg::ref_ptr<osg::Node> loadCar(tCarElt *car);
void updateCar();
};
class SDCars
{
private :
std::vector<SDCar *> the_cars;
osg::ref_ptr<osg::Group> cars_branch = new osg::Group;
tSituation * situation;
void addSDCar(SDCar * car);
public :
osg::ref_ptr<osg::Node> loadCars(tSituation * pSituation);
void updateCars();
};
#endif /* _OSGCAR_H_ */

View file

@ -1,7 +1,10 @@
#include "OsgLoader.h"
//#include "ReaderWriterACC.h"
#include <tgf.hpp>
#include <tgf.hpp>
#include <osg/MatrixTransform>
#include <osg/Node>
osgLoader::osgLoader(void)
{
@ -32,22 +35,40 @@ osg::ref_ptr<osg::Image> osgLoader::LoadImageFile(std::string strFile)
osg::Node *osgLoader::Load3dFile(std::string strFile)
{
osg::Node *pNode = NULL;
std::string ext = osgDB::getFileExtension(strFile);
if (ext == "acc")
if (ext == "acc" || ext == "ac")
{
//Use custom ACC file loader
osgDB::ReaderWriter::ReadResult rr = m_ACCReader.readNode(strFile, m_pOpt);
if (rr.validNode())
return rr.takeNode();
else
return NULL;
GfOut("le test %d \n",rr.validNode());
if (rr.validNode()) {
osg::Node * nod = rr.takeNode();
osg::MatrixTransform * rot = new osg::MatrixTransform;
osg::Matrix mat( 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
rot->setMatrix(mat);
rot->addChild(nod);
return rot;
}
else{
return NULL;}
}
else
{
pNode = osgDB::readNodeFile(strFile, m_pOpt);
GfOut("le test %d \n",pNode);
}
osg::ref_ptr<osg::MatrixTransform> rot = new osg::MatrixTransform;
osg::Matrix mat( 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
rot->setMatrix(mat);
rot->addChild(pNode);
GfOut("le test %d \n",pNode);
return pNode;
return rot.get();
}

View file

@ -19,6 +19,8 @@
#include "OsgMain.h"
#include "OsgMath.h"
#include "OsgCar.h"
//#include "grOSG.h"
//#include <osgViewer/Viewer>
@ -30,9 +32,9 @@
//#include "grshadow.h"
//#include "grskidmarks.h"
//#include "grsmoke.h"
//#include "grcar.h"
#include "OsgCar.h"
//#include "grscreen.h"
//#include "grscene.h"
#include "OsgScene.h"
#include "OsgScene.h"
//#include "grsound.h"
//#include "grloadac.h"
@ -48,6 +50,9 @@
//extern osg::Timer m_timer;
//extern osg::Timer_t m_start_tick;
SDCars cars;
int grMaxTextureUnits = 0;
tdble grMaxDammage = 10000.0;
@ -462,7 +467,7 @@ initView(int x, int y, int width, int height, int /* flag */, void *screen)
m_sceneViewer->getCamera()->setName("Cam one");
m_sceneViewer->getCamera()->setViewport(new osg::Viewport(0, 0, grWinw, grWinh));
m_sceneViewer->getCamera()->setGraphicsContext(gw.get());
m_sceneViewer->getCamera()->setProjectionMatrixAsPerspective(67.5, static_cast<double>(ratio), 1, 12000.0);
m_sceneViewer->getCamera()->setProjectionMatrixAsPerspective(67.5, static_cast<double>(ratio), 0.1, 12000.0);
m_sceneViewer->realize();
@ -523,7 +528,8 @@ int
refresh(tSituation *s)
{
//int i;
tCarElt *car = s->cars[0];
int nb = s->_ncars;
tCarElt *car = s->cars[nb-1];
osg::Vec3 eye, center, up, speed, P, p;
float offset = 0;
@ -532,11 +538,11 @@ refresh(tSituation *s)
p[0] = car->_pos_X+ car->_drvPos_x;
p[1] = car->_pos_Y+car->_drvPos_y;
p[2] = car->_pos_Z+car->_drvPos_z;
osgXformPnt3(p, car->_posMat);
// osgXformPnt3(p, car->_posMat);
eye[0] = p[0];
eye[1] = p[2];
eye[2] = -p[1];
eye[1] = p[1];
eye[2] = p[2];
// Compute offset angle and bezel compensation)
/*if (spansplit && viewOffset) {
@ -546,17 +552,17 @@ refresh(tSituation *s)
}*/
P[0] = (car->_pos_X + 30.0 * cos(car->_glance + offset+car->_yaw));
P[1] = -(car->_pos_Y - 30.0 * sin(car->_glance + offset-car->_yaw));
P[1] = (car->_pos_Y + 30.0 * sin(car->_glance + offset+car->_yaw));
P[2] = car->_pos_Z+car->_yaw;
osgXformPnt3(P, car->_posMat);
//osgXformPnt3(P, car->_posMat);
center[0] = P[0];
center[1] = P[2];
center[2] = P[1];
center[1] = P[1];
center[2] = P[2];
up[0] = car->_posMat[2][0];
up[1] = car->_posMat[2][2];
up[2] = car->_posMat[2][1];
up[1] = car->_posMat[2][1];
up[2] = car->_posMat[2][2];
speed[0] = car->pub.DynGCg.vel.x;
speed[1] = car->pub.DynGCg.vel.y;
@ -567,10 +573,14 @@ refresh(tSituation *s)
osg::Camera * camera = m_sceneViewer->getCamera();
camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
//camera->setProjectionMatrixAsPerspective(67.5, ratio, 1, 12000.0);
camera->setViewMatrixAsLookAt( eye, center, osg::Vec3(0,1,0));
m_sceneViewer->frame();
camera->setViewMatrixAsLookAt( eye, center, osg::Vec3(0,0,1));
cars.updateCars();
m_sceneViewer->frame();
return 0;
return 0;
}
/*GfProfStartProfile("refresh");
@ -797,6 +807,11 @@ initTrack(tTrack *track)
return grLoadScene(track);
}
int initCars(tSituation *s){
m_sceneroot->addChild(cars.loadCars(s));
GfOut("All cars loaded\n");
return 0;
}
/*void
shutdownTrack(void)

View file

@ -0,0 +1,36 @@
/***************************************************************************
file : OsgMath.h
created : Fri Aug 18 00:00:41 CEST 2012
copyright : (C) 2012 by Xavier Bertaux
email : bertauxx@yahoo.fr
version : $Id: OsgMath.h 4693 2012-04-13 03:12:09Z torcs-ng $
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <osg/Vec3>
#include "OsgMath.h"
void osgXformPnt3(osg::Vec3 dst, const osg::Vec3 src, const osgMat4 mat)
{
float t0 = src[0];
float t1 = src[1];
float t2 = src[2];
dst[0] = t0*mat[0][0] + t1*mat[1][0] + t2*mat[2][0] + mat[3][0];
dst[1] = t0*mat[0][1] + t1*mat[1][1] + t2*mat[2][1] + mat[3][1];
dst[2] = t0*mat[0][2] + t1*mat[1][2] + t2*mat[2][2] + mat[3][2];
}

View file

@ -17,6 +17,10 @@
* *
***************************************************************************/
#ifndef _OSGMATH_H_
#define _OSGMATH_H_
typedef float osgMat3[3][3];
typedef float osgMat4[4][4];
@ -30,8 +34,8 @@ extern void osgXformPnt3( osg::Vec3 dst, const osg::Vec3 src, const osgMat4 mat
inline void osgXformPnt3( osg::Vec3 dst, const osgMat4 mat ) { osgXformPnt3 ( dst, dst, mat ); }
void osgXformPnt3(osg::Vec3 dst, const osg::Vec3 src, const osgMat4 mat)
{
void osgXformPnt3(osg::Vec3 dst, const osg::Vec3 src, const osgMat4 mat);
/*{
float t0 = src[0];
float t1 = src[1];
float t2 = src[2];
@ -39,4 +43,6 @@ void osgXformPnt3(osg::Vec3 dst, const osg::Vec3 src, const osgMat4 mat)
dst[0] = t0*mat[0][0] + t1*mat[1][0] + t2*mat[2][0] + mat[3][0];
dst[1] = t0*mat[0][1] + t1*mat[1][1] + t2*mat[2][1] + mat[3][1];
dst[2] = t0*mat[0][2] + t1*mat[1][2] + t2*mat[2][2] + mat[3][2];
}
}*/
#endif /* _OSGMATH_H_ */

View file

@ -172,9 +172,23 @@ OsgInitScene(void)
//myLight2->setLinearAttenuation(2.0f/m_sceneroot);
m_sceneViewer->setSceneData( m_sceneroot.get() );
/* osg::ref_ptr<osg::MatrixTransform> rot = new osg::MatrixTransform;
osg::Matrix mat( 1.0f, 0.0f,0.0f, 0.0f,
0.0f, 0.0f,1.0f, 0.0f,
0.0f, -1.0f,0.0f, 0.0f,
0.0f, 0.0f,0.0f, 1.0f );
rot->setMatrix(mat);
rot->addChild(m_sceneroot.get());*/
m_sceneViewer->setSceneData(m_sceneroot.get());
m_sceneViewer->getCamera()->setCullingMode( m_sceneViewer->getCamera()->getCullingMode() & ~osg::CullStack::SMALL_FEATURE_CULLING);
osg::Group *g = new osg::Group;
m_carroot = g;
GfOut("LE POINTEUR %d\n",m_carroot.get());
return 0;
}//grInitScene
@ -228,6 +242,12 @@ void setViewer(osg::ref_ptr<osgViewer::Viewer> msV)
void setSceneRoot(osg::ref_ptr<osg::Group> root)
{
m_sceneroot = root;
// m_sceneroot->addChild(m_carroot.get());
}
void setCarRoot(osg::ref_ptr<osg::Group> root)
{
m_carroot = root;
}
void ClearScene(void)
@ -249,14 +269,24 @@ bool LoadTrack(std::string strTrack)
{
osgUtil::Optimizer optimizer;
optimizer.optimize(pTrack);
osg::Matrixf mat;
//osg::Matrixf mat;
//Rotate track model 90 degrees to match the way plib loader works
//mat = mat.rotate(osg::inDegrees(90.0), osg::X_AXIS);
//osg::MatrixTransform *pTrans = new osg::MatrixTransform(mat);
//pTrans->addChild(pTrack);
//pTrans->getOrCreateStateSet()->setRenderBinDetails(TRACKBIN,"RenderBin");
pTrack->getOrCreateStateSet()->setRenderBinDetails(TRACKBIN,"RenderBin");
m_sceneroot->addChild(pTrack);
//mat = mat.rotate(osg::inDegrees(90.0), osg::X_AXIS);
//osg::MatrixTransform *pTrans = new osg::MatrixTransform(mat);
//pTrans->addChild(pTrack);
//pTrans->getOrCreateStateSet()->setRenderBinDetails(TRACKBIN,"RenderBin");
//pTrack->getOrCreateStateSet()->setRenderBinDetails(TRACKBIN,"RenderBin");
/* osg::ref_ptr<osg::MatrixTransform> rot = new osg::MatrixTransform;
osg::Matrix mat( 1.0f, 0.0f,0.0f, 0.0f,
0.0f, 0.0f,1.0f, 0.0f,
0.0f, -1.0f,0.0f, 0.0f,
0.0f, 0.0f,0.0f, 1.0f );
rot->setMatrix(mat);
rot->addChild(pTrack);
*/
m_sceneroot->addChild(pTrack);
}
else

View file

@ -61,6 +61,7 @@ extern void grUpdateTime(tSituation *s);
extern void setViewer(osg::ref_ptr<osgViewer::Viewer> msV);
extern void ClearScene(void);
extern void setSceneRoot(osg::ref_ptr<osg::Group> root);
extern void setCarRoot(osg::ref_ptr<osg::Group> root);
//TODO: What is this??? kilo
#ifdef GUIONS

View file

@ -1,5 +1,7 @@
#include <string.h> // to remove with OSG 3.2
#include<string.h>
#include <osgDB/FileNameUtils>
#include <osgDB/Registry>
#include <osgDB/ReadFile>

View file

@ -90,6 +90,7 @@ bool OsgGraph::loadTrack(tTrack* pTrack)
bool OsgGraph::loadCars(tSituation* pSituation)
{
initCars(pSituation);
//GfLogDebug("SsgGraph::loadCars\n");
return true; //return ::initCars(pSituation) == 0;
}