BOOK: Game and Graphics Programming for iOS and Android with OpenGL ES 2.0
 | This is the forum to discuss the Wrox book Game and Graphics Programming for iOS and Android with OpenGL ES 2.0
by Romain Marucchi-Foino; ISBN: 978-1-1199-7591-5
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Game and Graphics Programming for iOS and Android with OpenGL ES 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 29th, 2017, 02:41 PM
|
|
Registered User
|
|
Join Date: May 2017
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Problem adding metal shininess to a car game
Hi, I made a car game working chapter 6-6 of the book, it works. When I mix it with chapter 4-5 for metal shininess the car instead of going straight moves in diagonal like in the physics world moves ok because it does not fall of the road like in the physics world I'm just going forward but in the drawing world something broke because it goes front left.
I updated the bullet physics version like the 3rd older current one, don't remember exact one, the newer does not work.
templateAppDraw working without shininess:
Code:
float nmx=0, nmy=0, nmz=0, ntx, nty, nAcc=0, nAccLast=0;
unsigned n=0;
int nAccelActive=0;
int bBrakesOn=0;
void templateAppDraw( void ) {
//#ifndef __IPHONE_4_0
pthread_mutex_lock( &mAcc );
nAccLast=nAcc;
pthread_mutex_unlock( &mAcc );
//#endif
//B 98ft 30.02 mts
// 60MPH 90.621KMH
// 3472Lbs 1574kg
static int nLastGear;
static float nLastK;
static float nLastRot;
static float nDistance=0;
static float nZeroCount=0;
static float n60Count=0;
static int nPrint=0;
SoundERpm(vehicle->m_currentRpm);
///
float nRotDelta=-1*nLastRot+vehicle->m_vehicle->getWheelInfo(2).m_rotation;
float nWheelDiameter=vehicle->m_vehicle->getWheelInfo(2).m_wheelsRadius*2*3.14159f;
float nDeltaDistance=nRotDelta*nWheelDiameter;
if(bBrakesOn)
{
nDistance+=nRotDelta;
if(abs(vehicle->m_vehicle->getCurrentSpeedKmHour())==0)
{
nZeroCount++;
if(nZeroCount>=1 && !nPrint){
console_print("!!!!!!!! Distance %.1fmts %.1ff",nDistance,nDistance*3.28f);
nPrint=1;
}
}
else{
nZeroCount=0;
}
}
nLastRot=vehicle->m_vehicle->getWheelInfo(2).m_rotation;
// Kmh 87.00 MPH 54.02
// 81.72MPH 2Gear
if(nLastGear==2 && vehicle->gears->m_currentGear==3)
{
console_print("!!!!!!!!!!!!!! Max Kmh %.2f MPH%.2f Gear(2), RPM:%.0f",nLastK*1.5721f
, nLastK*1.5721f*0.621f,(float)vehicle->m_currentRpm);
}
nLastK=abs(vehicle->m_vehicle->getCurrentSpeedKmHour());
nLastGear=vehicle->gears->m_currentGear;
/*if(nLastK*1.5721f*0.621f>=60)
{
n60Count++;
console_print("--> Kmh %.2f MPH%.2f ",nLastK*1.5721f
, nLastK*1.5721f*0.621f);
if(n60Count>=1)
{
bBrakesOn=1;
}
}
else{
n60Count=0;
}*/
if(nAccelActive){
vehicle->steeringClamp=0.3f;
vehicle->gVehicleSteering = -1.f*((nAccLast)/(float)9)*vehicle->steeringClamp;
}
static unsigned int start = get_milli_time(),
fps = 0, lastfps=10;
if( get_milli_time() - start >= 1000 ) {
//console_print( " FPS: %d \n",fps);
console_print( " FPS: %d KMH:%.0f MPH:%.0f RPM:%.0f\n",fps, (float)abs(vehicle->m_vehicle->getCurrentSpeedKmHour())*1.5721f,
(float)abs(vehicle->m_vehicle->getCurrentSpeedKmHour())*0.621f*1.5721f, vehicle->m_currentRpm);
//console_print("Force: %f\n",(float)vehicle->gEngineForce);
//console_print("Gear cur:%d curRatio:%.1f A:%d",vehicle->gears->m_currentGear,vehicle->gears->m_gears[vehicle->gears->m_currentGear].m_ratio, (int)vehicle->gears->m_isAutomatic);
start = get_milli_time();
lastfps=fps;
fps = 0;
}
++fps;
n++;
if( restart_game )
{
templateAppExit();
load_game();
ball_index = 0;
restart_game = 0;
}
glClearColor( 0, 0, 0.5f, 1.0f );
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
GFX_set_matrix_mode( MODELVIEW_MATRIX );
GFX_load_identity(); {
btVector3 vCarO =vehicle->m_carChassis->getWorldTransform().getOrigin();
btMatrix3x3 vCarB=vehicle->m_carChassis->getWorldTransform().getBasis();
btVector3 vF,vCamT, vCam=btVector3(0,-7.49f,-1.2f);
//btVector3 vF,vCamT, vCam=btVector3(-7.49f,0,-1.2f);
//btVector3 vF,vCamT, vCam=btVector3(0,7.49f,-1.2f);
vCamT=btVector3(vCam.x(),vCam.z(),-1*vCam.y());
vF=vCarO-(vCarB*vCamT );
vec3 e = { vF.x(),vF.y(),vF.z()},
c = { vCarO.x(),vCarO.y(),vCarO.z()+1},
u = { 0.0f, 0.0f, 1.0f };
/*vec3 e = { -65.44f+nmx, 305.13f+nmy, -9.71f },
c = { -65.44f,299.39f , -10.00f},
u = { 0.0f, 0.0f, 1.0f };*/
/*vec3 e = {0.0f , -28.0f+nmx, -4.35f},
c = { 0.0f, 0.0f, 1.35f},
u = { 0.0f, 0.0f, 1.0f };*/
/*vec3 e = { 7.4, -6.5, 5.3},
c = { 0.0f, 0, 0 },
u = { 0.0f, 0.0f, 1.0f };*/
/*vec3 e = { 0.0f, +10.36f, 10.64f },
c = { 0.0f, 0, 0 },
u = { 0.0f, 0.0f, 1.0f };*/
GFX_look_at( &e, &c, &u );
}
unsigned int i = 0;
while( i != obj->n_objmesh ) {
OBJMESH *objmesh = &obj->objmesh[ i ];
if(objmesh==oCar || objmesh==rearlight1 || objmesh==rearlight2){
if(n==1 && objmesh==oCar){
OBJMESH *o = &obj->objmesh[ i ];
btVector3 v=o->btrigidbody->getCenterOfMassPosition();
console_print( "\nF Name: %s %s",o->name,(o->btrigidbody->getInternalType()&btCollisionObject::CO_RIGID_BODY)?"Rigid Body":"Not Rigid Body");
console_print( " Loc: %.2f,%.2f,%.2f ",o->location.x,o->location.y,o->location.z);
console_print( " LocPhy: %.2f,%.2f,%.2f ", v.x(),v.y(), v.z());
v=o->btrigidbody->getWorldTransform().getOrigin();
console_print( " LocPhy2: %.2f,%.2f,%.2f ", v.x(),v.y(), v.z());
console_print( " Rot: %.2f,%.2f,%.2f ",o->rotation.x,o->rotation.y,o->rotation.z);
console_print( " Scale: %.2f,%.2f,%.2f ",o->scale.x,o->scale.y,o->scale.z);
console_print( " Min: %.2f,%.2f,%.2f ",o->min.x,o->min.y,o->min.z);
console_print( " max: %.2f,%.2f,%.2f ",o->max.x,o->max.y,o->max.z);
console_print( " dimension: %.2f,%.2f,%.2f ",o->dimension.x,o->dimension.y,o->dimension.z);
console_print( " radius: %.2f ", o->radius);
console_print( " distance: %.2f ", o->distance);
console_print( " stride: %u ", o->stride);
}
i++;
continue;
}
GFX_push_matrix();
GLE
if(vehicle->RigidBodyIsWheel(objmesh))
{
mat4 mat;
int n2=vehicle->GetWheelIndex(objmesh);
vehicle->m_vehicle->getWheelInfo(n2).m_worldTransform.getOpenGLMatrix(( float * )&mat);
//vehicle->m_vehicle->getWheelTransformWS(n2).
vehicle->m_vehicle->getWheelTransformWS(n2).getOpenGLMatrix(( float * )&mat);
//vehicle->m_vehicle->
//vehicle->m_vehicle->getWheelInfo(n2).updateWheel(<#const btRigidBody &chassis#>, <#btWheelInfo::RaycastInfo &raycastInfo#>)
GFX_multiply_matrix( &mat );GLE
if(n==1){
OBJMESH *o = &obj->objmesh[ i ];
const btVector3 v=vehicle->m_vehicle->getWheelInfo(n2).m_worldTransform.getOrigin();
console_print( "\nF Name: %s ",o->name);
console_print( " Loc: %.2f,%.2f,%.2f ",o->location.x,o->location.y,o->location.z);
console_print( " LocPhy: %.2f,%.2f,%.2f ", v.x(),
v.y(), v.z());
}
}
else if(objmesh->oParentTransform)
{
mat4 mat;
btVector3 v,vO,vOT;
objmesh->oParentTransform->getOpenGLMatrix(( float * )&mat);
//GFX_multiply_matrix( &mat );
btMatrix3x3 m= objmesh->oParentTransform->getBasis();
vO=*objmesh->vOrigin;
if(strstr(objmesh->name,"Wheel"))
vOT=btVector3(-1*vO.x(),-1*vO.y(),vO.z());
else
vOT=btVector3(vO.x(),vO.z(),-1*vO.y());
v=objmesh->oParentTransform->getOrigin()-(m*vOT );
if(n==1){
console_print( " LocPhy: %.2f,%.2f,%.2f %s", v.x(),
v.y(), v.z(),objmesh->name);
}
if(!objmesh->oTransform)
objmesh->oTransform=new btTransform();
objmesh->oTransform->setFromOpenGLMatrix(( float * )&mat);
objmesh->oTransform->setOrigin(v);
objmesh->oTransform->getOpenGLMatrix(( float * )&mat);
GFX_multiply_matrix( &mat );GLE
//GFX_translate( v.x(), v.z(),v.y());
}
else if( objmesh->btrigidbody )
{
mat4 mat;
if(n==1){
console_print( "DRAW %s ", objmesh->name);
}
objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix( ( float * )&mat );
GFX_multiply_matrix( &mat );GLE
}
else GFX_translate( objmesh->location.x,
objmesh->location.y, objmesh->location.z );
glUniformMatrix4fv( program->uniform_array[ 0 ].location,
1,
GL_FALSE,
( float * )GFX_get_modelview_projection_matrix() );GLE
OBJ_draw_mesh( obj, i );
GLE
GFX_pop_matrix();
GLE
++i;
}
int n, nEnd;
nFramePrint=1;
if(lastfps<=0)lastfps=5;
bCarFlying=1;
for(nEnd=20,n=0; n<nEnd; n++)
{
if(!n)nFramePrint=1;
else nFramePrint=0;
if(n==1){
//console_print( "Apply central impulse ");
}//console_print( "DRAW", " stepSimulation ");
vehicle->m_carChassis->applyCentralImpulse(btVector3(0,0,abs(vehicle->m_vehicle->getCurrentSpeedKmHour())/-7.f));
vehicle->renderme((1.0f/nEnd) / ((float)lastfps/2.0f));
if(n==1){
//console_print( "StepSimulation lastfps=%f! ->%f ",(float)lastfps,(float)( (1.0f/nEnd) / ((float)lastfps/2.0f)));
}//console_print( "DRAW", " stepSimulation ");
dynamicsworld->stepSimulation( (1.0f/nEnd) / ((float)lastfps/2.0f));
}
GLE
//if(bCarFlying)
// console_print("Car Flying!!!");
}
templateAppDraw not working with shininess, instead of going straight goes straight and to the left without falling of the road:
Code:
float nmx=0, nmy=0, nmz=0, ntx, nty, nAcc=0, nAccLast=0;
unsigned n=0;
int nAccelActive=0;
int bBrakesOn=0;
void templateAppDraw( void ) {
//#ifndef __IPHONE_4_0
//pthread_mutex_lock( &mAcc );
//nAccLast=nAcc;
//pthread_mutex_unlock( &mAcc );
//#endif
//B 98ft 30.02 mts
// 60MPH 90.621KMH
// 3472Lbs 1574kg
static int nLastGear;
static float nLastK;
static float nLastRot;
static float nDistance=0;
static float nZeroCount=0;
static float n60Count=0;
static int nPrint=0;
glClearColor( 0.5f, 0.5f, 0.5f, 1.0f );
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
GFX_set_matrix_mode( MODELVIEW_MATRIX );
GFX_load_identity();
/*vec3 e = { 0.0f, -6.0f, 1.35f },
c = { 0.0f, -5.0f, 1.35f },
u = { 0.0f, 0.0f, 1.0f };*/
//vec3 e = { -65.44f/2, (299.39f-300.67f)*nmx+300.67f/10, (-10.f+8.80f)*nmx+65.8f},
//c = { -65.44f/2, 299.39f/10, 65.00f},
vec3 e = { -65.44f, -8.f, -310.8f},
c = { -65.44f, -10.f, -296.00f},
u = { 0.0f, 1.0f, 0.0f };
GFX_look_at( &e, &c, &u );
if(nAccelActive){
vehicle->steeringClamp=0.3f;
vehicle->gVehicleSteering = -1.f*((nAccLast)/(float)9)*vehicle->steeringClamp;
}
static unsigned int start = get_milli_time(),
fps = 0, lastfps=10;
if( get_milli_time() - start >= 1000 ) {
//console_print( " FPS: %d \n",fps);
console_print( " FPS: %d KMH:%.0f MPH:%.0f RPM:%.0f\n",fps, (float)abs(vehicle->m_vehicle->getCurrentSpeedKmHour())*1.5721f,
(float)abs(vehicle->m_vehicle->getCurrentSpeedKmHour())*0.621f*1.5721f, vehicle->m_currentRpm);
//console_print("Force: %f\n",(float)vehicle->gEngineForce);
//console_print("Gear cur:%d curRatio:%.1f A:%d",vehicle->gears->m_currentGear,vehicle->gears->m_gears[vehicle->gears->m_currentGear].m_ratio, (int)vehicle->gears->m_isAutomatic);
start = get_milli_time();
lastfps=fps;
fps = 0;
}
++fps;
unsigned int i = 0;
while( i != obj->n_objmesh ) {
OBJMESH *objmesh = &obj->objmesh[ i ];
if(strstr(objmesh->name,"Skel")){
i++;
continue;
}
OBJMATERIAL *objmaterial = obj->objmesh[ i ].objtrianglelist[ 0 ].objmaterial;
if( objmaterial->dissolve == 1.0f ) {
GFX_push_matrix();
if(vehicle->RigidBodyIsWheel(objmesh))
{ /*
mat4 mat;
int n2=vehicle->GetWheelIndex(objmesh);
vehicle->m_vehicle->getWheelInfo(n2).m_worldTransform.getOpenGLMatrix(( float * )&mat);
//vehicle->m_vehicle->getWheelTransformWS(n2).
vehicle->m_vehicle->getWheelTransformWS(n2).getOpenGLMatrix(( float * )&mat);
//vehicle->m_vehicle->
//vehicle->m_vehicle->getWheelInfo(n2).updateWheel(<#const btRigidBody &chassis#>, <#btWheelInfo::RaycastInfo &raycastInfo#>)
GFX_multiply_matrix( &mat );GLE
if(n==1){
OBJMESH *o = &obj->objmesh[ i ];
const btVector3 v=vehicle->m_vehicle->getWheelInfo(n2).m_worldTransform.getOrigin();
console_print( "\nF Name: %s ",o->name);
console_print( " Loc: %.2f,%.2f,%.2f ",o->location.x,o->location.y,o->location.z);
console_print( " LocPhy: %.2f,%.2f,%.2f ", v.x(),
v.y(), v.z());
}
*/
}
else if(objmesh->oParentTransform)
{
/*mat4 mat;
btVector3 v,vO,vOT;
objmesh->oParentTransform->getOpenGLMatrix(( float * )&mat);
//GFX_multiply_matrix( &mat );
btMatrix3x3 m= objmesh->oParentTransform->getBasis();
vO=*objmesh->vOrigin;
if(strstr(objmesh->name,"Wheel"))
vOT=btVector3(-1*vO.x(),-1*vO.y(),vO.z());
else
vOT=btVector3(vO.x(),vO.z(),-1*vO.y());
v=objmesh->oParentTransform->getOrigin()-(m*vOT );
if(n==1){
console_print( " LocPhy: %.2f,%.2f,%.2f %s", v.x(),
v.y(), v.z(),objmesh->name);
}
if(!objmesh->oTransform)
objmesh->oTransform=new btTransform();
objmesh->oTransform->setFromOpenGLMatrix(( float * )&mat);
objmesh->oTransform->setOrigin(v);
objmesh->oTransform->getOpenGLMatrix(( float * )&mat);
btMatrix3x3 mat33;
mat33.setValue(mat.m[0].x, mat.m[0].y, mat.m[0].z, mat.m[1].x, mat.m[1].y, mat.m[1].z, mat.m[2].x, mat.m[2].y,mat.m[2].z);
vO=mat33*v;
//GFX_multiply_matrix( &mat );GLE
GFX_translate( vO.getX(), vO.getY(),vO.getZ());*/
}
/*else if( objmesh->btrigidbody )
{
mat4 mat;
if(n==1){
console_print( "DRAW %s ", objmesh->name);
}
objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix( ( float * )&mat );
GFX_multiply_matrix( &mat );GLE
}*/
else {
GFX_translate( obj->objmesh[ i ].location.x,
obj->objmesh[ i ].location.y,
obj->objmesh[ i ].location.z );
}
OBJ_draw_mesh( obj, i );
GFX_pop_matrix();
}
++i;
}
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
i = 0;
while( i != obj->n_objmesh ) {
OBJMESH *objmesh = &obj->objmesh[ i ];
if(strstr(objmesh->name,"Skel")){
i++;
continue;
}
OBJMATERIAL *objmaterial = obj->objmesh[ i ].objtrianglelist[ 0 ].objmaterial;
if( objmaterial->dissolve != 1.0f ) {
GFX_push_matrix();
if(vehicle->RigidBodyIsWheel(objmesh))
{
mat4 mat;
int n2=vehicle->GetWheelIndex(objmesh);
try{
vehicle->m_vehicle->getWheelInfo(n2).m_worldTransform.getOpenGLMatrix(( float * )&mat);
//vehicle->m_vehicle->getWheelTransformWS(n2).
vehicle->m_vehicle->getWheelTransformWS(n2).getOpenGLMatrix(( float * )&mat);
GFX_multiply_matrix( &mat );GLE
}catch(OBJMESH *o)
{
}
if(n==1){
OBJMESH *o = &obj->objmesh[ i ];
const btVector3 v=vehicle->m_vehicle->getWheelInfo(n2).m_worldTransform.getOrigin();
console_print( "\nF Name: %s ",o->name);
console_print( " Loc: %.2f,%.2f,%.2f ",o->location.x,o->location.y,o->location.z);
console_print( " LocPhy: %.2f,%.2f,%.2f ", v.x(),
v.y(), v.z());
}
}
else if(objmesh->oParentTransform)
{
mat4 mat;
btVector3 v,vO,vOT;
objmesh->oParentTransform->getOpenGLMatrix(( float * )&mat);
//GFX_multiply_matrix( &mat );
btMatrix3x3 m= objmesh->oParentTransform->getBasis();
vO=*objmesh->vOrigin;
if(strstr(objmesh->name,"Wheel"))
vOT=btVector3(-1*vO.x(),-1*vO.y(),vO.z());
else
vOT=btVector3(vO.x(),vO.z(),-1*vO.y());
v=objmesh->oParentTransform->getOrigin()-(m*vOT );
if(n==1){
console_print( " LocPhy: %.2f,%.2f,%.2f %s", v.x(),
v.y(), v.z(),objmesh->name);
}
if(!objmesh->oTransform)
objmesh->oTransform=new btTransform();
objmesh->oTransform->setFromOpenGLMatrix(( float * )&mat);
objmesh->oTransform->setOrigin(v);
objmesh->oTransform->getOpenGLMatrix(( float * )&mat);
btMatrix3x3 mat33;
mat33.setValue(mat.m[0].x, mat.m[0].y, mat.m[0].z, mat.m[1].x, mat.m[1].y, mat.m[1].z, mat.m[2].x, mat.m[2].y,mat.m[2].z);
vO=mat33*v;
//GFX_multiply_matrix( &mat );GLE
GFX_translate( vO.getX(), vO.getY(),vO.getZ());
}
/*else if( objmesh->btrigidbody )
{
mat4 mat;
if(n==1){
console_print( "DRAW %s ", objmesh->name);
}
objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix( ( float * )&mat );
GFX_multiply_matrix( &mat );GLE
}*/
else GFX_translate( obj->objmesh[ i ].location.x,
obj->objmesh[ i ].location.y,
obj->objmesh[ i ].location.z );
glCullFace( GL_FRONT );
OBJ_draw_mesh( obj, i );
glCullFace( GL_BACK );
OBJ_draw_mesh( obj, i );
GFX_pop_matrix();
}
++i;
}
glDisable( GL_BLEND );
int n, nEnd;
//nFramePrint=1;
if(lastfps<=0)lastfps=5;
//bCarFlying=1;
for(nEnd=20,n=0; n<nEnd; n++)
{
//if(!n)nFramePrint=1;
//else nFramePrint=0;
if(n==1){
//console_print( "Apply central impulse ");
}//console_print( "DRAW", " stepSimulation ");
vehicle->m_carChassis->applyCentralImpulse(btVector3(0,0,abs(vehicle->m_vehicle->getCurrentSpeedKmHour())/-7.f));
vehicle->renderme((1.0f/nEnd) / ((float)lastfps/2.0f));
if(n==1){
//console_print( "StepSimulation lastfps=%f! ->%f ",(float)lastfps,(float)( (1.0f/nEnd) / ((float)lastfps/2.0f)));
}//console_print( "DRAW", " stepSimulation ");
dynamicsworld->stepSimulation( (1.0f/nEnd) / ((float)lastfps/2.0f));
}
}
please help!
|
|
 |