gldesign2_old.c

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 2,0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 - 2007 OpenFX Development Team
00004 -- */
00005 
00006 /* file GLDESIGN2.C   */
00007 
00008 #define _DSCRUCT_SUB 1
00009 
00010 #include "gldesign1.h"
00011 
00012 extern unsigned char *LoadMAP(char *InFile, long *xx,long *yy);
00013 
00014 extern GLfloat up_angle,round_angle,scale_view;
00015 extern GLfloat gbRed,gbGreen,gbBlue;
00016 extern GLfloat gwRed,gwGreen,gwBlue;
00017 extern GLfloat near_plane,far_plane;
00018 extern GLint listID,mode,bounding_box,current_map;
00019 extern BOOL  Mapped,Update,Fastest,Clamped,
00020       Cmaterial,AttenuateLight,TwoSided,
00021       ScrollBars,bDrawMapping,bOverlayWire,
00022       bThickWire,bNurbsWireOnly,
00023       bStereo,bStereoDraw;
00024 extern char gszBMPfile[],gszBMPdir[],gszRootDir[];
00025 extern GLMAP    *glMapList;
00026 extern long     NglMapList;
00027 extern void TextureWithAlpha(long x, long y, unsigned char *pin, unsigned char alpha, BOOL, 
00028                       unsigned char,unsigned char, unsigned char);
00029 
00030 extern GLuint g_check_texture;
00031 extern GLuint g_map_texture;
00032 extern GLuint g_movie_texture;
00033 extern GLuint g_env_texture;
00034 extern GLuint g_envB_texture;
00035 extern GLuint g_bump_texture;
00036 extern GLuint g_ref_texture;
00037 extern GLuint g_tran_texture;
00038 extern GLuint g_noise_texture;
00039 extern GLubyte checkImage[checkImageWidth][checkImageHeight][3];
00040 
00041 extern void DrawGlNurbs(GLfloat);
00042 extern void DrawProjectMapRectangle(void);
00043 extern void makeCheckImageMap(int id);
00044 extern void GetMappingCoordS(int id, vector n, vector dx, vector dy, vector p0, long *pv,
00045                  GLfloat * a, GLfloat * b);
00046 extern void GetMappingCoordP(int id, vector n, vector x, vector y, vector p0,
00047                              long *pv,
00048                              GLfloat *a, GLfloat *b);
00049 extern void GetMappingCoordC(int id, vector n, vector x, vector y, vector p0,
00050                 long *pv,
00051                 GLfloat *a, GLfloat *b);
00052 extern void GetMapNormal(int k, vector mP, vector mX, vector mY,
00053                          vector mN);
00054 extern BOOL normalise(GLfloat *v);
00055 extern BOOL Normalize(point x,point y,point z,GLfloat *n);
00056 
00057 extern BOOL inview(vertex  *vp);
00058 extern void FixUpMapEdge(GLfloat alpha[]);
00059 
00060 static void DrawMappedPolysProg(GLfloat scale, normal *nv);
00061 static void GetRelativeCoord(vector n, vector x, vector y, vector pb, vector pf,
00062                 GLfloat * a, GLfloat * b, GLfloat *c);
00063 static void GetTextureNormal(int k, vector mP, vector mX, vector mY,
00064                          vector mN);
00065 
00066 
00067 void Make3dDisplayListProg(void){
00068  GLfloat x,y,z,dxn,dyn,dzn,scale,n[3],color[4],vv[3],*spec_color,
00069          glos_color[]={1.0,1.0,1.0,1.0},
00070          dark_color[]={0.0,0.0,0.0,1.0},
00071          black[]={0.1,0.1,0.1,1.0},
00072          dull[]={0.0},
00073          shiny[]={100.0},dot;
00074  face *fp;
00075  vertex *v,*v1,*v2,*v0,*vnext;
00076  normal *nv;
00077  int i,j,k,m,Vi,V[3];
00078  if(Nface == 0 && Nnurbs == 0){
00079    if(listID > 0){glDeleteLists(1,1);listID=0;}
00080    return;
00081  }
00082  if(Nvert > 0){
00083    if((nv=(normal *)X__Malloc(sizeof(normal)*Nvert)) == NULL){
00084      return;
00085    }
00086    for(i=0;i<Nvert;i++)nv[i][0]=nv[i][1]=nv[i][2]=0.0;
00087  }
00088  fp=MainFp; if(Nface > 0)for(i=0;i<Nface;i++){  // calculate the surface normals
00089    V[0]=fp->V[0]; V[1]=fp->V[1]; V[2]=fp->V[2];
00090    v0=(MainVp+V[0]); v1=(MainVp+V[1]); v2=(MainVp+V[2]);
00091    if(inview(v0) || inview(v1) || inview(v2)){
00092      if(Normalize(v0->xyz,v1->xyz,v2->xyz,n)){
00093        for(j=0;j<3;j++){
00094          dot=DOT(nv[V[j]],n);
00095          if(fabs(dot) < 1.e-10){
00096            VECCOPY(n,nv[V[j]])
00097          }
00098          else if(dot >  0.5){
00099            VECSUM(nv[V[j]],n,nv[V[j]])
00100          }
00101          else if(dot < -0.5){
00102            VECSUB(nv[V[j]],n,nv[V[j]])
00103          }
00104        }
00105      }
00106    }
00107    fp++;
00108  }
00109  if(Nvert > 0)for(i=0;i<Nvert;i++)normalise(nv[i]);
00110 
00111  if(listID > 0){glDeleteLists(1,1);listID=0;}
00112  glNewList(1,GL_COMPILE);
00113  
00114  color[3]=1.0; color[0]=1.0; color[1]=1.0; color[2]=1.0;
00115  scale=4.0/TVsizeX;
00116  glEnable(GL_LIGHTING);
00117  
00118  // first draw the polygon with basic attributes only
00119  glEnable(GL_COLOR_MATERIAL);
00120  UseShaderProgram(1); 
00121  SetUniformVariable(1,"SpecularContribution",0.5);   // only mix in a little
00122  glBegin(GL_TRIANGLES);
00123  glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
00124  glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,glos_color);
00125  fp=MainFp; if(Nface > 0)for(i=0;i<Nface;i++,fp++){
00126    //   if(Mapped && fp->imagemap >= 0)continue; // draw the mapped polygons these will be blended in later
00127    if(fp->material  >= 0)continue;          // materials in this pass
00128    v0=(MainVp+fp->V[0]); v1=(MainVp+fp->V[1]); v2=(MainVp+fp->V[2]);
00129    if(inview(v0) || inview(v1) || inview(v2)){
00130        color[0]=(GLfloat)fp->color[0]/255.0;
00131        color[1]=(GLfloat)fp->color[1]/255.0;
00132        color[2]=(GLfloat)fp->color[2]/255.0;
00133        glColor4fv(color);
00134        if(Normalize(v0->xyz,v1->xyz,v2->xyz,n)){
00135          for(j=0;j<3;j++){
00136            Vi=fp->V[j]; v=(MainVp+Vi);
00137            x=((GLfloat)(v->xyz[0]-TVpointX-TVsizeX/2))*scale;
00138            y=((GLfloat)(v->xyz[1]-TVpointY-TVsizeY/2))*scale;
00139            z=((GLfloat)(v->xyz[2]-TVpointZ-TVsizeZ/2))*scale;
00140            if(fp->bSmooth){ /* smoothed */
00141              dot=DOT(nv[Vi],n);
00142              if(fabs(dot) < 0.5)glNormal3f(n[0],n[2],-n[1]);
00143              else if(dot  < 0.0)glNormal3f(-nv[Vi][0],-nv[Vi][2], nv[Vi][1]);
00144              else               glNormal3f( nv[Vi][0], nv[Vi][2],-nv[Vi][1]);
00145            }
00146            else{/* not smoothed */
00147              glNormal3f( n[0], n[2],-n[1]);
00148            }
00149            glVertex3f(x,z,-y);
00150          }
00151        }
00152    }
00153  }
00154  glEnd();
00155  UseShaderProgram(0); 
00156  glDisable(GL_COLOR_MATERIAL);
00157 
00158  DrawGlNurbs(GLU_FILL);
00159 
00160  if(nMats > 0 && Nface > 0){ // render the materials
00161    int pass,matID=0;
00162    vector mN,mX,mY,mP,mp;
00163    GLfloat uu,vv,ww,spower;  
00164    color[0]=1.0; color[1]=1.0; color[2]=1.0;
00165    glEnable(GL_COLOR_MATERIAL);
00166    glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
00167    for(pass=0;pass<2;pass++) // render transparency last
00168    for(k=0;k<nMats;k++){
00169      if(pass == 0 && iMat[k].transp > 0)continue;
00170      if(pass == 1 && iMat[k].transp == 0)continue;
00171      matID=0;
00172      GetTextureNormal(k,mP,mX,mY,mN);
00173      for(i=0;i<3;i++)color[i]=(GLfloat)iMat[k].mcolour1[i]/255.0;
00174      // set gl properites for this face
00175      glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
00176      glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,glos_color);
00177      if(iMat[k].transp > 0){ // Set transparent blending
00178        glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,dull);
00179        glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,dark_color);
00180        glEnable(GL_BLEND);
00181        glDepthMask(GL_FALSE);
00182        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00183        color[3]=(1.0 - (GLfloat)iMat[k].transp / 255.0);
00184      }
00185      else color[3]=1.0;
00186      glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,color);
00187      glColor4fv(color);
00188      UseShaderProgram(2);   
00189      SetUniformVariable(2,"MixRatio",(GLfloat)iMat[k].refl/255.0);
00190      if(iMat[k].refl > 0){
00191        glActiveTexture(GL_TEXTURE0);
00192        glBindTexture(GL_TEXTURE_2D,g_env_texture);
00193        SetUniformInteger(2,"RefMap",0); // texture unit 0
00194      }
00195      if(matID == 2){
00196        //glDisable(GL_TEXTURE_2D);
00197        //glEnable(GL_TEXTURE_3D);
00198        //glBindTexture(GL_TEXTURE_3D,g_noise_texture);
00199        //SetUniformInteger(2,"Noise",0); // texture unit 0  (was 1)
00200        glActiveTexture(GL_TEXTURE1);
00201        glBindTexture(GL_TEXTURE_2D,g_envB_texture);
00202        SetUniformInteger(2,"NormalMap",1);
00203      }
00204      spower=(20.0 + (80.0 - 10.0)*(GLfloat)(iMat[k].gloss)/255.0);
00205      SetUniformVariable(2,"SpecularPower",spower);
00206      if(iMat[k].bShiny)SetUniformVariable(2,"SpecularContribution",1.0);
00207      else              SetUniformVariable(2,"SpecularContribution",0.0);
00208      SetUniformVector(2,"MaterialC",(GLfloat)iMat[k].mcolour2[0]/255.0,
00209                                     (GLfloat)iMat[k].mcolour2[1]/255.0,
00210                                     (GLfloat)iMat[k].mcolour2[2]/255.0);
00211      SetUniformInteger(2,"ShaderID",matID);
00212      glBegin(GL_TRIANGLES);
00213      fp=MainFp; if(Nface > 0)for(i=0;i<Nface;i++,fp++){ // daw the mapped polys
00214        if(fp->material != k /* || (Mapped && fp->imagemap >= 0) */ || fp->material < 0)continue; //skip
00215        v0=(MainVp+fp->V[0]); v1=(MainVp+fp->V[1]); v2=(MainVp+fp->V[2]);
00216        if(inview(v0) || inview(v1) || inview(v2)){
00217          if(Normalize(v0->xyz,v1->xyz,v2->xyz,n)){
00218            for(j=0;j<3;j++){
00219              Vi=fp->V[j]; v=(MainVp+Vi);
00220              x=((GLfloat)(v->xyz[0]-TVpointX-TVsizeX/2))*scale;  mp[0]=(double)v->xyz[0];
00221              y=((GLfloat)(v->xyz[1]-TVpointY-TVsizeY/2))*scale;  mp[1]=(double)v->xyz[1];
00222              z=((GLfloat)(v->xyz[2]-TVpointZ-TVsizeZ/2))*scale;  mp[2]=(double)v->xyz[2];
00223              if(j == 0){
00224                v=(MainVp+(fp->V[1]));
00225                dxn=((GLfloat)(v->xyz[0]-TVpointX-TVsizeX/2))*scale - x; 
00226                dyn=((GLfloat)(v->xyz[1]-TVpointY-TVsizeY/2))*scale - y;
00227                dzn=((GLfloat)(v->xyz[2]-TVpointZ-TVsizeZ/2))*scale - z;
00228              }   
00229              if(iMat[k].bSmooth){ /* smoothed */
00230                dot=DOT(nv[Vi],n);
00231                if(fabs(dot) < 0.5){
00232                  glNormal3f(n[0],n[2],-n[1]);
00233                  SetAttributeVector(2,"Tangent",dxn,dzn,-dyn); 
00234                }
00235                else if(dot  < 0.0){
00236                  glNormal3f(-nv[Vi][0],-nv[Vi][2], nv[Vi][1]);
00237                  SetAttributeVector(2,"Tangent",dxn,-dzn,dyn); 
00238                }
00239                else{
00240                  glNormal3f( nv[Vi][0], nv[Vi][2],-nv[Vi][1]);
00241                  SetAttributeVector(2,"Tangent",dxn,dzn,-dyn); 
00242                }
00243              }
00244              else{/* not smoothed */
00245                glNormal3f( n[0], n[2],-n[1]);
00246                SetAttributeVector(2,"Tangent",dxn,dzn,-dyn); 
00247              }
00248              GetRelativeCoord(mN,mX,mY,mP,mp, &uu, &vv, &ww);
00249              SetAttributeVector(2,"ShaderPosition",uu,ww,-vv);
00250              glVertex3f(x,z,-y);
00251            }
00252          }
00253        }
00254      }
00255      glEnd();
00256      UseShaderProgram(0); 
00257      // any material attributes that need to be undone ?
00258      if(iMat[k].transp > 0){ // Set transparent blending
00259        glDepthMask(GL_TRUE);
00260        glDisable(GL_BLEND);
00261      }
00262      if(matID == 2){
00263        glActiveTexture(GL_TEXTURE1);
00264        glBindTexture(GL_TEXTURE_3D,0);
00265        glActiveTexture(GL_TEXTURE0);
00266 //glDisable(GL_TEXTURE_3D);
00267 //glEnable(GL_TEXTURE_2D);
00268      }
00269      if(iMat[k].refl > 0){
00270        glActiveTexture(GL_TEXTURE0);
00271        glBindTexture(GL_TEXTURE_2D,0);
00272      }
00273    }
00274    glEnable(GL_COLOR_MATERIAL);
00275  }
00276  // Finally the mapping - ALPHA blend it in !!!
00277  glEnable(GL_BLEND);
00278  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00279  if(Mapped && Nface > 0)DrawMappedPolysProg(scale,nv);
00280  glDisable(GL_BLEND);
00281 
00282  glDisable(GL_LIGHTING);
00283  bThickWire=TRUE;
00284  if(bOverlayWire)DrawModelEdges();
00285  bThickWire=FALSE;
00286 
00287  glEndList();
00288  listID=1;
00289 
00290  X__Free(nv);
00291  return;
00292 }
00293 
00294 static void DrawMappedPolysProg(GLfloat scale, normal *nv){  
00295  GLfloat mixColour[4]={0.0,0.0,0.0,0.0};
00296  GLfloat x,y,z,n[3],
00297          dot;
00298  face *fp;
00299  vertex *v,*v1,*v2,*v0;
00300  int i,j,k,m,Vi;
00301  BOOL refmap;
00302  int brushID;
00303  vector mN,mX,mY,mP;
00304  GLfloat alpha[3],beta[3];
00305  GLfloat bc[4]={0.0,0.0,0.0,0.0};
00306 
00307  if(nImaps > 0)for(k=0;k<nImaps;k++){       // loop over all maps
00308    GetMapNormal(k,mP,mX,mY,mN);
00309    if(glMapList != NULL && k < NglMapList){
00310      refmap=FALSE;
00311 
00312      glActiveTexture(GL_TEXTURE0);
00313      glBindTexture(GL_TEXTURE_2D,0);
00314      if(iMap[k].s == 1){    // .sp  
00315        if(glMapList[k].bMovie)glBindTexture(GL_TEXTURE_2D,g_movie_texture);
00316        else if(glMapList[k].pixels != NULL){
00317          glBindTexture(GL_TEXTURE_2D,g_map_texture);
00318          TextureWithAlpha(glMapList[k].x,glMapList[k].y,glMapList[k].pixels,255,
00319          iMap[k].bDecal,iMap[k].k_colour[0],iMap[k].k_colour[1],iMap[k].k_colour[2]);
00320        }
00321        else glBindTexture(GL_TEXTURE_2D,g_check_texture);
00322        if(iMap[k].bTiled){
00323          if(iMap[k].Map == PLANE_MOZIAC || iMap[k].Map == CYLINDER_MOZIAC){
00324            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_MIRRORED_REPEAT);
00325            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_MIRRORED_REPEAT);
00326          }
00327          else{
00328            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00329            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00330          }
00331        }
00332        else{
00333          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
00334          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
00335        } 
00336        glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
00337        glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_INTERPOLATE);
00338        glTexEnvf(GL_TEXTURE_ENV,GL_SRC0_RGB,GL_TEXTURE);
00339        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
00340        glTexEnvf(GL_TEXTURE_ENV,GL_SRC1_RGB,GL_PREVIOUS);
00341        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);
00342        if(iMap[k].bShaded)
00343          mixColour[3]=((GLfloat)iMap[k].sp)*0.005;  
00344        else
00345          mixColour[3]=((GLfloat)iMap[k].sp)*0.01; 
00346        glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR,mixColour);
00347        glTexEnvf(GL_TEXTURE_ENV,GL_SRC2_RGB,GL_CONSTANT);
00348        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND2_RGB,GL_SRC_ALPHA);
00349      }
00350 
00351      glActiveTexture(GL_TEXTURE1);
00352      glBindTexture(GL_TEXTURE_2D,0);
00353      if(iMap[k].r == 1){
00354        if(glMapList[k].bMovieR){
00355          glBindTexture(GL_TEXTURE_2D,g_movie_texture);
00356        }
00357        else if(glMapList[k].pixelsR != NULL){
00358          glBindTexture(GL_TEXTURE_2D,g_ref_texture);
00359          glTexImage2D(GL_TEXTURE_2D,0,3,
00360                           glMapList[k].xR,glMapList[k].yR,
00361                           0,GL_RGB,GL_UNSIGNED_BYTE,
00362                           (GLvoid *)glMapList[k].pixelsR);
00363        }
00364        else glBindTexture(GL_TEXTURE_2D,g_check_texture);
00365        refmap=TRUE;
00366        glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);  // for reflection
00367        glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);
00368        glEnable(GL_TEXTURE_GEN_S);
00369        glEnable(GL_TEXTURE_GEN_T);
00370        glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
00371        glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_INTERPOLATE);
00372        glTexEnvf(GL_TEXTURE_ENV,GL_SRC0_RGB,GL_TEXTURE);
00373        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
00374        glTexEnvf(GL_TEXTURE_ENV,GL_SRC1_RGB,GL_PREVIOUS);
00375        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);
00376        mixColour[3]=((GLfloat)iMap[k].rp)*0.01;   // ( a value of 0 => No reflection.)
00377        glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR,mixColour);
00378        glTexEnvf(GL_TEXTURE_ENV,GL_SRC2_RGB,GL_CONSTANT);
00379        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND2_RGB,GL_SRC_ALPHA);
00380      }
00381 
00382      glActiveTexture(GL_TEXTURE2);
00383      glBindTexture(GL_TEXTURE_2D,0);
00384      if(iMap[k].b == 1){   
00385        if(glMapList[k].bMovie)glBindTexture(GL_TEXTURE_2D,g_movie_texture);
00386        else if(glMapList[k].pixelsB != NULL){
00387          glBindTexture(GL_TEXTURE_2D,g_bump_texture);
00388          glTexImage2D(GL_TEXTURE_2D,0,3,
00389                           glMapList[k].xB,glMapList[k].yB,
00390                           0,GL_RGB,GL_UNSIGNED_BYTE,
00391                           (GLvoid *)glMapList[k].pixelsB);
00392        }
00393        else glBindTexture(GL_TEXTURE_2D,g_check_texture);
00394        if(iMap[k].bTiled){
00395          if(iMap[k].Map == PLANE_MOZIAC || iMap[k].Map == CYLINDER_MOZIAC){
00396            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_MIRRORED_REPEAT);
00397            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_MIRRORED_REPEAT);
00398          }
00399          else{
00400            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00401            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00402          }
00403        }
00404        else{
00405          glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR,bc);
00406          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
00407          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
00408        } 
00409        glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
00410        glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_INTERPOLATE);
00411        glTexEnvf(GL_TEXTURE_ENV,GL_SRC0_RGB,GL_TEXTURE);
00412        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
00413        glTexEnvf(GL_TEXTURE_ENV,GL_SRC1_RGB,GL_PREVIOUS);
00414        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);
00415        mixColour[3]=((GLfloat)iMap[k].bp)*0.0025;  // 0 = no mix 1 = full mix
00416        glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR,mixColour);
00417        glTexEnvf(GL_TEXTURE_ENV,GL_SRC2_RGB,GL_CONSTANT);
00418        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND2_RGB,GL_SRC_ALPHA);
00419      }
00420      // loop over all materials to get the border colour
00421      // any basic colour attributes will just appear as a grey border
00422 
00423      glActiveTexture(GL_TEXTURE0); 
00424      m = -1; 
00425      matloop:
00426      glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
00427      glEnable(GL_COLOR_MATERIAL);
00428      glBegin(GL_TRIANGLES);
00429      fp=MainFp; if(Nface > 0)for(i=0;i<Nface;i++,fp++){
00430        if(fp->imagemap < 0)continue; /* if not mapped */
00431        if(m >= 0 && fp->material != m)continue; // not this material
00432        if(m == -1 && fp->material >= 0)continue; // or this basic one
00433        brushID=fp->imagemap;
00434        if(brushID != k)continue;         /* skip if not this map */
00435 
00436        if(iMap[k].bShaded){
00437          // work out a color mix so that it blends from background colour to mid grey
00438          GLfloat ff,mc[3];
00439          ff=((GLfloat)iMap[k].sp)*0.01;  // simulate dimming 
00440          if(m == -1)for(j=0;j<3;j++)mc[j]=(GLfloat)(fp->color[j])/255.0;
00441          else       for(j=0;j<3;j++)mc[j]=(GLfloat)(iMat[m].mcolour1[j])/255.0;
00442          for(j=0;j<3;j++)mc[j] = mc[j]*(1.0-ff);// + 0.5*ff;
00443          glColor3fv(mc);
00444        }
00445        else{
00446          if(m == -1)glColor4ub(fp->color[0],fp->color[1],fp->color[2],255);
00447          else       glColor4ub(iMat[m].mcolour1[0],iMat[m].mcolour1[1],iMat[m].mcolour1[2],255);
00448        }
00449        v0=(MainVp+fp->V[0]); v1=(MainVp+fp->V[1]); v2=(MainVp+fp->V[2]);
00450        if(inview(v0) || inview(v1) || inview(v2)){
00451          if(Normalize(v0->xyz,v1->xyz,v2->xyz,n)){
00452            for(j=0;j<3;j++){
00453              Vi=fp->V[j]; v=(MainVp+Vi);
00454              if(fp->gp == TRUE){  // use face mapping coordinates
00455                alpha[j] = (GLfloat)(fp->x[j]); 
00456                beta[j]  = (GLfloat)(fp->y[j]); 
00457              }
00458              else if(iMap[k].Map == MAP_BY_VERTEX){
00459                alpha[j]=(GLfloat)v->x;
00460                beta[j]=(GLfloat)v->y;
00461              }
00462              else if(iMap[k].Map == MAP_SPHERICAL)
00463                   GetMappingCoordS(k,mN,mX,mY,mP,v->xyz,&(alpha[j]),&(beta[j]));
00464              else if(iMap[k].Map == CYLINDER || iMap[k].Map == CYLINDER_MOZIAC)
00465                   GetMappingCoordC(k,mN,mX,mY,mP,v->xyz,&(alpha[j]),&(beta[j]));
00466              else GetMappingCoordP(k,mN,mX,mY,mP,v->xyz,&(alpha[j]),&(beta[j]));
00467            }
00468            if(!fp->gp && (iMap[k].Map == CYLINDER || iMap[k].Map == CYLINDER_MOZIAC || iMap[k].Map == MAP_SPHERICAL)){
00469              FixUpMapEdge(alpha);
00470            }
00471            for(j=0;j<3;j++){
00472              Vi=fp->V[j]; v=(MainVp+Vi);
00473              x=((GLfloat)(v->xyz[0]-TVpointX-TVsizeX/2))*scale;
00474              y=((GLfloat)(v->xyz[1]-TVpointY-TVsizeY/2))*scale;
00475              z=((GLfloat)(v->xyz[2]-TVpointZ-TVsizeZ/2))*scale;
00476              if(fp->bSmooth){ /* smoothed */
00477                dot=DOT(nv[Vi],n);
00478                if(fabs(dot) < 0.5)glNormal3f(n[0],n[2],-n[1]);
00479                else if(dot  < 0.0)glNormal3f(-nv[Vi][0],-nv[Vi][2], nv[Vi][1]);
00480                else               glNormal3f( nv[Vi][0], nv[Vi][2],-nv[Vi][1]);
00481              }
00482              else{/* not smoothed */
00483                glNormal3f( n[0], n[2],-n[1]);
00484              }
00485              if(!fp->gp && (iMap[k].Map == CYLINDER || iMap[k].Map == CYLINDER_MOZIAC))alpha[j] *= 360.0/(GLfloat)iMap[k].Angle;
00486              //glTexCoord2f(alpha[j],beta[j]);
00487              glMultiTexCoord2f(GL_TEXTURE0,alpha[j],beta[j]);  // for non reflective textures
00488              glMultiTexCoord2f(GL_TEXTURE2,alpha[j],beta[j]);
00489              glVertex3f(x,z,-y);
00490            }
00491          }
00492        }
00493      }
00494      glEnd();
00495      glDisable(GL_COLOR_MATERIAL);
00496      m++;
00497      if(nMats > 0 && m >= 0 && m < nMats)goto matloop;
00498      glActiveTexture(GL_TEXTURE2); 
00499      glBindTexture(GL_TEXTURE_2D,0);
00500      glActiveTexture(GL_TEXTURE1); 
00501      glBindTexture(GL_TEXTURE_2D,0);
00502      if(refmap){
00503        glDisable(GL_TEXTURE_GEN_S);
00504        glDisable(GL_TEXTURE_GEN_T);
00505      }
00506      glActiveTexture(GL_TEXTURE0); 
00507      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00508      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00509      glBindTexture(GL_TEXTURE_2D,0);
00510    } // valid map list
00511  }   // each map
00512 }
00513 
00514 
00515 
00516 #define TOL 1.e-4;
00517 
00518 static void GetRelativeCoord(vector n, vector x, vector y, vector pb, vector pf,
00519                 GLfloat * a, GLfloat * b, GLfloat *c){
00520  /* n        is normal to plane of axis
00521     x & y    are vectors along the edges of the map  x= left y = down
00522     pb       is position vector to base point on map
00523     fp       is position vector to vertex to be mapped
00524     a & b    are output of mapping coordinates [0 - 1]
00525  */
00526  int k;
00527  vector v1,p;
00528  GLfloat lg;
00529  GLfloat mu,dm,detmax,det1,det2,det3,ve1,ve2,ve3,vp1,vp2,vp3,p1,p2,p3;
00530  VECSUB(pb,pf,v1)
00531  mu=DOT(n,v1);
00532  VECSCALE(mu,n,v1)
00533  VECSUM(pf,v1,p)
00534  ve1=x[0]; ve2=x[1]; ve3=x[2];
00535  vp1=y[0]; vp2=y[1]; vp3=y[2];
00536  p1=p[0]-pb[0];
00537  p2=p[1]-pb[1];
00538  p3=p[2]-pb[2];
00539  det1=ve1*vp2-vp1*ve2;
00540  det2=ve1*vp3-vp1*ve3;
00541  det3=ve2*vp3-vp2*ve3;
00542  k=0; detmax=TOL;
00543  if((dm=fabs(det1)) > detmax){k=1; detmax=dm;}
00544  if((dm=fabs(det2)) > detmax){k=2; detmax=dm;}
00545  if((dm=fabs(det3)) > detmax){k=3; detmax=dm;}
00546  if(k == 1){
00547    *a=( vp2*p1-vp1*p2)/det1;
00548    *b=(-ve2*p1+ve1*p2)/det1;
00549  }
00550  else if(k == 2){
00551    *a=( vp3*p1-vp1*p3)/det2;
00552    *b=(-ve3*p1+ve1*p3)/det2;
00553  }
00554  else if(k == 3){
00555    *a=( vp3*p2-vp2*p3)/det3;
00556    *b=(-ve3*p2+ve2*p3)/det3;
00557  }
00558  else {
00559    *a = *b = 0.0;
00560  }
00561  lg = max(1.0,sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]));
00562  *c = mu / lg;
00563 }
00564 
00565 static void GetTextureNormal(int k, vector mP, vector mX, vector mY,
00566                          vector mN){
00567  GLfloat n[3];
00568  VECCOPY((double)iMat[k].ax.P,mP)
00569  VECSUB((double)iMat[k].ax.X,mP,mX)
00570  VECSUB((double)iMat[k].ax.Y,mP,mY)
00571  CROSS(mX,mY,mN)
00572  VECCOPY((GLfloat)mN,n)
00573  if(!normalise(n))MessageBeep(MB_OK);
00574  VECCOPY((double)n,mN)
00575  return;
00576 }
00577 
00578 #if 0
00579 //glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR,mc);
00580 //         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00581 //         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00582 //         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00583 //         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00584 //{
00585 //static float CC[4]={0.0,1.0,0.0,0.4};
00586 //glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
00587 //glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR,CC);
00588 //glTexEnvf(GL_TEXTURE_ENV,GL_SRC2_RGB,GL_CONSTANT);
00589 //glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND2_RGB,GL_SRC_ALPHA);
00590 //}
00591 TextureWithAlphaAndBorder(long x, long y, unsigned char *pin){   // border only for testing
00592   unsigned char *pout,*p;
00593   long i,j;
00594   pout=X__Malloc((x+2)*(y+2)*4); p=pout; memset(pout,255,(x+2)*(y+2)*4);
00595   p += (x+3)*4;
00596   for(i=0;i<y;i++){
00597     for(j=0;j<x;j++){
00598      *p++ = *pin++;
00599      *p++ = *pin++;
00600      *p++ = *pin++;
00601      *p++ = 255;
00602     }
00603     p += 2*4;
00604   }
00605   glTexImage2D(GL_TEXTURE_2D,0,4,x+2,y+2,1,GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid *)pout);
00606   X__Free(pout);
00607 }
00608 
00610 static void DrawMappedPolyAlphaSurface(GLfloat scale, normal *nv){ // for debugging only
00611  GLfloat x,y,z,n[3],dot;
00612  face *fp;
00613  vertex *v,*v1,*v2,*v0;
00614  int i,j,k,m,Vi;
00615  BOOL refmap;
00616  int brushID;
00617  vector mN,mX,mY,mP;
00618  GLfloat alpha[3],beta[3];
00619  GLfloat bc[4];
00620 
00621  if(nImaps > 0)for(k=0;k<nImaps;k++){       // loop over all maps
00622    GetMapNormal(k,mP,mX,mY,mN);
00623    if(glMapList != NULL && k < NglMapList){
00624      refmap=FALSE;
00625      glBindTexture(GL_TEXTURE_2D,0);
00626      if(iMap[k].s == 1){    // .sp  
00627        if(glMapList[k].bMovie)glBindTexture(GL_TEXTURE_2D,g_movie_texture);
00628        else if(glMapList[k].pixels != NULL){
00629          glBindTexture(GL_TEXTURE_2D,g_map_texture);
00630          TextureWithAlpha(glMapList[k].x,glMapList[k].y,glMapList[k].pixels,255,
00631          iMap[k].bDecal,iMap[k].k_colour[0],iMap[k].k_colour[1],iMap[k].k_colour[2]);
00632        }
00633        else glBindTexture(GL_TEXTURE_2D,g_check_texture);
00634        if(iMap[k].bTiled){
00635          if(iMap[k].Map == PLANE_MOZIAC || iMap[k].Map == CYLINDER_MOZIAC){
00636            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_MIRRORED_REPEAT);
00637            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_MIRRORED_REPEAT);
00638          }
00639          else{
00640            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00641            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00642          }
00643        }
00644        else{
00645          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
00646          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
00647        } 
00648        glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_REPLACE);
00649      }
00650      glEnable(GL_COLOR_MATERIAL);
00651      glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
00652      glColor4f(1.0,1.0,1.0,1.0);
00653      m = -1; 
00654      matloop:
00655      glBegin(GL_TRIANGLES);
00656      fp=MainFp; if(Nface > 0)for(i=0;i<Nface;i++,fp++){
00657        if(fp->imagemap < 0)continue; /* if not mapped */
00658        if(m >= 0 && fp->material != m)continue; // not this material
00659        if(m == -1 && fp->material >= 0)continue; // or this basic one
00660        brushID=fp->imagemap;
00661        if(brushID != k)continue;         /* skip if not this map */
00662        v0=(MainVp+fp->V[0]); v1=(MainVp+fp->V[1]); v2=(MainVp+fp->V[2]);
00663        if(inview(v0) || inview(v1) || inview(v2)){
00664          if(Normalize(v0->xyz,v1->xyz,v2->xyz,n)){
00665            for(j=0;j<3;j++){
00666              Vi=fp->V[j]; v=(MainVp+Vi);
00667              if(fp->gp == TRUE){  // use face mapping coordinates
00668                alpha[j] = (GLfloat)(fp->x[j]); 
00669                beta[j]  = (GLfloat)(fp->y[j]); 
00670              }
00671              else if(iMap[k].Map == MAP_BY_VERTEX){
00672                alpha[j]=(GLfloat)v->x;
00673                beta[j]=(GLfloat)v->y;
00674              }
00675              else if(iMap[k].Map == MAP_SPHERICAL)
00676                   GetMappingCoordS(k,mN,mX,mY,mP,v->xyz,&(alpha[j]),&(beta[j]));
00677              else if(iMap[k].Map == CYLINDER || iMap[k].Map == CYLINDER_MOZIAC)
00678                   GetMappingCoordC(k,mN,mX,mY,mP,v->xyz,&(alpha[j]),&(beta[j]));
00679              else GetMappingCoordP(k,mN,mX,mY,mP,v->xyz,&(alpha[j]),&(beta[j]));
00680            }
00681            if(!fp->gp && (iMap[k].Map == CYLINDER || iMap[k].Map == CYLINDER_MOZIAC || iMap[k].Map == MAP_SPHERICAL)){
00682              FixUpMapEdge(alpha);
00683            }
00684            for(j=0;j<3;j++){
00685              Vi=fp->V[j]; v=(MainVp+Vi);
00686              x=((GLfloat)(v->xyz[0]-TVpointX-TVsizeX/2))*scale;
00687              y=((GLfloat)(v->xyz[1]-TVpointY-TVsizeY/2))*scale;
00688              z=((GLfloat)(v->xyz[2]-TVpointZ-TVsizeZ/2))*scale;
00689              if(fp->bSmooth){ /* smoothed */
00690                dot=DOT(nv[Vi],n);
00691                if(fabs(dot) < 0.5)glNormal3f(n[0],n[2],-n[1]);
00692                else if(dot  < 0.0)glNormal3f(-nv[Vi][0],-nv[Vi][2], nv[Vi][1]);
00693                else               glNormal3f( nv[Vi][0], nv[Vi][2],-nv[Vi][1]);
00694              }
00695              else{/* not smoothed */
00696                glNormal3f( n[0], n[2],-n[1]);
00697              }
00698              if(!fp->gp && (iMap[k].Map == CYLINDER || iMap[k].Map == CYLINDER_MOZIAC))alpha[j] *= 360.0/(GLfloat)iMap[k].Angle;
00699              glTexCoord2f(alpha[j],beta[j]);
00700              glVertex3f(x,z,-y);
00701            }
00702          }
00703        }
00704      }
00705      glEnd();
00706      m++;
00707      if(nMats > 0 && m >= 0 && m < nMats)goto matloop;
00708      glEnable(GL_COLOR_MATERIAL);
00709      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00710      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00711      glBindTexture(GL_TEXTURE_2D,0);
00712    } // valid map list
00713  }   // each map
00714 }
00716            tangent[0] =  mX[0];
00717            tangent[1] =  mX[2];
00718            tangent[2] = -mX[1];
00719            // pass the tangent vector to the shader as a vertex attribute.
00720            glVertexAttrib3f(1,tangent[0],tangent[1],tangent[2]);
00721 
00722 
00723 
00724 
00725 #endif

Generated on Sun Apr 27 14:20:11 2014 for OpenFX by  doxygen 1.5.6