i
i
i
i
i
i
i
i
330 13. Programming 3D Graphics in Real Time
else { // and ordinary map painted onto surface
Image=LoadMAP(MapsFp[k].filename,&xm,&ym);
refmap=FALSE;
}
if(Image != NULL)makeNextMap(Image,xm,ym); // If map pixels were created
// make the OGL map in
// texture RAM.
glEnable(GL_TEXTURE_2D); // render from a map instead of
// a color
// Tell OGL which type of texture to use and which image map to use.
// If we could
// not load the map we tell OGL to use the simple checker board texture
if(Image != NULL)glBindTexture(GL_TEXTURE_2D,IMAGE_TEXTURE);
else glBindTexture(GL_TEXTURE_2D,CHECK_TEXTURE);
// For OpenFX models we need to get its map parameters so that
// we can generate
// texture coordinates.
GetMapNormal(MapsFp[k].map,MapsFp[k].p,MapsFp[k].x,MapsFp[k].y,mP,mX,mY,mN);
glBegin(GL_TRIANGLES); // now render the polygons
fp=MainFp; for(i=0;i<Nface;i++,fp++){
if((fp->texture & 0x40)!=0x40)continue; // skip if polygon not mapped
brushID=(fp->brush & 0x1f); // which image ?
if(brushID != k)continue; // skip if not this image map
v0=(MainVp+fp->V[0]); v1=(MainVp+fp->V[1]); v2=(MainVp+fp->V[2]);
if(Normalize(v0->p,v1->p,v2->p,n)){ // get polygon normal
for(j=0;j<3;j++){
Vi=fp->V[j]; v=(MainVp+Vi);
x=((GLfloat)(v->p[0]-c[0]))
*
scale; // scale and center model
..// same for y and z ccords ; // in view volume
if(smooth_shading && ((fp->texture >> 7) != 0)){ // smoothed
glNormal3f(nv[Vi][0], nv[Vi][1],nv[Vi][2]);
} else glNormal3f( n[0], n[1], n[2]); // not smoothed
if(MapsFp[k].map == MAP_BY_VERTEX){
// texture coords exist - use them!!
alpha=(GLfloat)v->x; beta=(GLfloat)v->y;
}
else if(MapsFp[k].map == CYLINDER || // OpenFX model - must
// calculate texture coords
MapsFp[k].map == CYLINDER_MOZIAC)
GetMappingCoordC(mN,mX,mY,mP,MapsFp[k].angle,
v->p,&alpha,&beta);
else GetMappingCoordP(mN,mX,mY,mP,v->p,&alpha,&beta);
if(!refmap)glTexCoord2f(alpha,beta);
glVertex3f(x,z,-y);
}}}
glEnd(); // all polygons done with this map
glDisable(GL_TEXTURE_2D); // done texturing with this map
if(refmap){
glDisable(GL_TEXTURE_GEN_S); // We were rendering reflection
glDisable(GL_TEXTURE_GEN_T); // mapping.
}
} // end of map loop
} // end of rendering image maps
Listing 13.13. (continued).