rendergl2.c

Go to the documentation of this file.
00001 // File rendergl2.c
00002 
00003 #define MODULE_RENDERGL2
00004 
00005 #include "render.h"
00006 #include "..\shaders\shaders.h"
00007 #include "..\gl2.h"
00008 
00009 extern int RENDERBUFFER_WIDTH;
00010 extern int RENDERBUFFER_HEIGHT;
00011 extern int TEXTUREBUFFER_WIDTH;
00012 extern int TEXTUREBUFFER_HEIGHT;
00013 extern GLuint g_check_texture;
00014 extern GLuint g_map_texture;
00015 extern GLuint g_ref_texture;  
00016 extern GLuint g_bump_texture;
00017 extern GLuint g_tran_texture;
00018 extern GLuint g_movie_texture;
00019 extern GLuint g_env_texture;
00020 extern GLuint g_envB_texture;
00021 extern GLuint g_envN_texture;
00022 extern GLuint g_noise_texture;
00023 extern GLuint g_env3D_texture;
00024 extern GLuint g_sky_texture;
00025 
00026 extern double DepthScalingGL,FrontDepthGL,BackDepthGL;
00027 
00028 extern char gszHomeDir[];
00029 extern double GlobalScale;
00030 
00031 static void DrawExternalTexturePolygons(long k, int pass, // material ID
00032                                         GLuint progID, GLint attrloc, 
00033                                         long Nface, face *MainFp, long Nvert, vertex *MainVp);
00034 
00035 
00036 #include "..\common\postprocess\ximage.h"
00037 
00038 long PostProcessRenderGL(long frame){ // NB: In the renderer "double" is always cast to "float"
00039  int i,j,k;
00040  char fname[256],tparam[256];
00041  long AnimatorIdList[256];
00042  HMODULE hLib;
00043  FARPROC fpFun;
00044  XIMAGE Ximage,*lpXimage;
00045  long l,result=0;
00046  long (*fpFun1)(char *, XIMAGE *);
00047  if(nImageProcesses == 0 || ImageProcesses == NULL)return 0;
00048  // Most of the fields in this structure are not necessary
00049  lpXimage=&Ximage;
00050  memset(lpXimage,0,sizeof(XIMAGE));
00051  Ximage.HWrender=1;
00052  Ximage.version=3;
00053  Ximage.Frame=frame;
00054  Ximage.Xscale=scalex;
00055  Ximage.Yscale=scaley;
00056  Ximage.Xmax=TEXTUREBUFFER_WIDTH;
00057  Ximage.Ymax=TEXTUREBUFFER_HEIGHT;
00058  Ximage.depthscalingGL=DepthScalingGL; 
00059  Ximage.frontbufferGL=FrontDepthGL;
00060  Ximage.backbufferGL=BackDepthGL;
00061  Ximage.Nlights=Nlights;
00062  Ximage.Lights=Lights;
00063  Ximage.ViewPoint = (double *)ViewPoint; 
00064  Ximage.CamPhi=CamPhi;
00065  Ximage.CamTheta=CamTheta;
00066  Ximage.CamAlpha=CamAlpha;
00067  Ximage.CamSx=CamSx;
00068  Ximage.CamSy=CamSy;
00069  Ximage.CamSz=CamSz;
00070  Ximage.fp_Terminate=R_terminator;
00071  Ximage.fp_Yield=RenderYield;
00072  for(i=0;i<4;i++)for(j=0;j<4;j++)Ximage.ViewTransform[i][j]=ViewTransform[i][j];
00073  for(k=0;k<nImageProcesses;k++){
00074    lpXimage->Morph=ImageProcesses[k].imo;
00075    lpXimage->MorphRatio=ImageProcesses[k].fmr;
00076    lpXimage->first_frame=ImageProcesses[k].first_frame;
00077    lpXimage->this_frame=ImageProcesses[k].this_frame;
00078    lpXimage->last_frame=ImageProcesses[k].last_frame;
00079    lpXimage->aParameters=ImageProcesses[k].parameters;
00080    lpXimage->mParameters=ImageProcesses[k].mparameters;
00081    sscanf(ImageProcesses[k].parameters,"%s",tparam);
00082    RestoreWhiteSpaces(tparam);
00083    sscanf(tparam,"%[^\0]",fname);
00084    //if(debug != NULL)fprintf(debug,"Loading OpenGL image process [%s]\n",fname);
00085    hLib=AddLoadedLibrary(fname);
00086    lpXimage->ModuleName=fname;
00087    if(hLib == NULL){ /* try the default ximage directory */
00088      char *sfname,tfname[256];
00089      sfname=short_form(fname);
00090      strcpy(tfname,gszHomeDir);  strcat(tfname,"postprocess\\");
00091      strcat(tfname,sfname);
00092      hLib=AddLoadedLibrary(tfname);
00093      lpXimage->ModuleName=tfname;
00094    }
00095    if(hLib != NULL){
00096      if((fpFun=GetProcAddress(hLib,"_RenderGLexternal")) != NULL){
00097        fpFun1 = (void *)fpFun;
00098        result=(*fpFun1)(ImageProcesses[k].parameters,lpXimage);
00099      }
00100      else MessageBox(GetFocus(), (LPCTSTR) "Failed Function", NULL, MB_OK | MB_SYSTEMMODAL );
00101    }
00102    else MessageBox(GetFocus(),(LPCTSTR) "Failed Load", NULL, MB_OK | MB_SYSTEMMODAL );
00103    if(!result)return FAIL;
00104  }
00105  return 0;
00106 }
00107 
00108 #include "..\textures\rstruct.h"
00109 
00110 extern X__SHADER EviShader;
00111 
00112 static void RenderExternalTextureGL(long tid, long k, // shader ID  material ID 
00113                                     int pass,    // two passes - first is for transparent polys then the non transparent
00114                                     GLuint progID, GLint attrloc, 
00115                                     long Nface, face *MainFp, long Nvert, vertex *MainVp,
00116                                     double bump_scale,
00117                                     unsigned char sc[3],
00118                                     unsigned char ac[3]){
00119  long (*fpTexFunGL)(
00120                    double,
00121                    unsigned char [3],
00122                    unsigned char [3],
00123                    X__SHADER *);
00124  long result;
00125  /*  x and y are same as alpha beta */
00126  if(tid < 0 || tid >= Nshader)return;
00127  fpTexFunGL=(void *)(Shader[tid].functionGL);
00128  EviShader.GLshaderID=Shader[tid].shaderID;
00129  EviShader.fp_DrawShaderPolygons=DrawExternalTexturePolygons;
00130  EviShader.fp_matpass=k;
00131  EviShader.fp_pass=pass;
00132  EviShader.fp_progID=progID;
00133  EviShader.fp_attrloc=attrloc;
00134  EviShader.fp_Nface=Nface;
00135  EviShader.fp_Nvert=Nvert;
00136  EviShader.fp_MainFp=(void *)MainFp;
00137  EviShader.fp_MainVp=(void *)MainVp;
00138  result=(*fpTexFunGL)(bump_scale,sc,ac,&EviShader);
00139 }
00140 
00141 void DrawExternalMaterialPolys(BOOL bDrawTransparent, BOOL bClip, 
00142             long nMats, matl *iMat, 
00143             long Nface, face *MainFp, long Nvert, vertex *MainVp){
00144  BOOL bFirstTex;
00145  int i,//j,
00146      k,n,pass,sID;
00147  vector mN,mX,mY,mP,mp;
00148  GLfloat uu,vv,ww,spower,color[3];
00149  GLuint progID;
00150  GLint  attrloc;
00151  GLfloat //x,y,z,
00152          glos_color[]={1.0,1.0,1.0,1.0},
00153          dark_color[]={0.0,0.0,0.0,1.0},
00154          black[]={0.1,0.1,0.1,1.0},
00155          dull[]={0.0},
00156          shiny[]={100.0},dot;
00157 // vertex *vp;
00158 // face   *fp;
00159  //
00160  color[0]=1.0; color[1]=1.0; color[2]=1.0;
00161  glEnable(GL_COLOR_MATERIAL);
00162  glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
00163  for(k=0;k<nMats;k++){
00164    if(iMat[k].bInternalShader)continue;        // Only renderer external shaders
00165    bFirstTex=TRUE;
00166    for(n=0;n<4;n++){                             // render each shader
00167      if((sID=iMat[k].shaderID[n]) < 0)continue;  // only render active shaders
00168      for(pass=0;pass<2;pass++){ // render transparency last
00169        if(pass == 0 && iMat[k].transp > 0)continue;
00170        if(pass == 1 && iMat[k].transp == 0)continue;
00171        // set gl properites for this face
00172        for(i=0;i<3;i++)color[i]=(GLfloat)iMat[k].color[i]/255.0;
00173        glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
00174        glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,glos_color);
00175        if(iMat[k].transp > 0){ // Set transparent blending
00176          glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,dull);
00177          glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,dark_color);
00178          glEnable(GL_BLEND);
00179          glDepthMask(GL_FALSE);
00180          glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00181          color[3]=(1.0 - (GLfloat)iMat[k].transp / 255.0);
00182        }
00183        else   color[3]=1.0;
00184        glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,color);
00185        glColor4fv(color);
00186        progID=Shader[sID].shaderID;
00187        //if(debug != NULL)fprintf(debug,"Material %ld has plugin shader using ID %ld\n",k,progID);
00188        UseSingleShaderProgram(progID);
00189        // Set up the uniforms that must be part of the shader  
00190        SetUniformInteger(progID,"Clip1",(int)bClip);
00191        SetUniformVariable(progID,"MixRatio",(GLfloat)iMat[k].refl/255.0);
00192        SetUniformVariable(progID,"ShaderParameter",(GLfloat)iMat[k].params[0]/255.0); // shader parameter
00193        SetUniformInteger(progID,"ShaderID",0);
00194        // make noise sampler available & 3D reflection
00195 //       glDisable(GL_TEXTURE_2D); 
00196 //       glEnable(GL_TEXTURE_3D);
00197        glActiveTexture(GL_TEXTURE0);
00198        glBindTexture(GL_TEXTURE_3D,g_noise_texture);
00199        SetUniformInteger(progID,"NoiseMap",0); 
00200        glActiveTexture(GL_TEXTURE1);
00201        glBindTexture(GL_TEXTURE_3D,g_env3D_texture);
00202        SetUniformInteger(progID,"RefMap3D",1); 
00203 
00204        // Set up the reflection transformation for the reflected direction - this will UNDO the camera
00205        // orientation so that the reflection vector still points in a direction that is relative to world coordinates
00206        SetUniformVector(progID,"AntiNormal1",AntiCameraGL[0][0],AntiCameraGL[1][0],AntiCameraGL[2][0]);
00207        SetUniformVector(progID,"AntiNormal2",AntiCameraGL[0][1],AntiCameraGL[1][1],AntiCameraGL[2][1]);
00208        SetUniformVector(progID,"AntiNormal3",AntiCameraGL[0][2],AntiCameraGL[1][2],AntiCameraGL[2][2]);
00209        // make reflection available
00210        glActiveTexture(GL_TEXTURE2);
00211        glBindTexture(GL_TEXTURE_2D,g_env_texture);
00212        SetUniformInteger(progID,"RefMap",2); 
00213        // make bumpy texture available 
00214        glActiveTexture(GL_TEXTURE3);
00215        glBindTexture(GL_TEXTURE_2D,g_envB_texture);
00216        SetUniformInteger(progID,"NormalMap",3);
00217        SetUniformVector(progID,"Uvector",(GLfloat)(iMat[k].ax.bu_norm[0]),
00218                                          (GLfloat)(iMat[k].ax.bu_norm[2]),
00219                                         -(GLfloat)(iMat[k].ax.bu_norm[1]));
00220        SetUniformVector(progID,"Vvector",(GLfloat)(iMat[k].ax.bv_norm[0]),
00221                                          (GLfloat)(iMat[k].ax.bv_norm[2]),
00222                                         -(GLfloat)(iMat[k].ax.bv_norm[1]));
00223        SetUniformVector(progID,"Wvector",(GLfloat)(iMat[k].ax.bn[0]),
00224                                          (GLfloat)(iMat[k].ax.bn[2]),
00225                                         -(GLfloat)(iMat[k].ax.bn[1]));
00226 
00227        // make noise lookup table available
00228        glActiveTexture(GL_TEXTURE4);
00229        glBindTexture(GL_TEXTURE_2D,g_envN_texture);
00230        SetUniformInteger(progID,"NoiseData",4);
00231 
00232        spower=(20.0 + (80.0 - 10.0)*(GLfloat)(iMat[k].gloss)/255.0);
00233        SetUniformVariable(progID,"SpecularPower",spower);
00234        if(iMat[k].bShiny)SetUniformVariable(progID,"SpecularContribution",1.0);
00235        else              SetUniformVariable(progID,"SpecularContribution",0.0);
00236        attrloc=GetAttibuteLocation(progID,"ShaderPosition");
00237        // Set up any shader specific Uniforms in the Shader DLL - this also calls back to draw the polygons
00238        RenderExternalTextureGL(sID,k,pass,progID,attrloc,
00239             Nface,MainFp,Nvert,MainVp,  
00240             (GLfloat)iMat[k].params[0]/255.0,iMat[k].color,iMat[k].texco);
00242        UseShaderProgram(0); 
00243        // any material attributes that need to be undone ?
00244        if(iMat[k].transp > 0){ // Set transparent blending
00245          glDepthMask(GL_TRUE);
00246          glDisable(GL_BLEND);
00247        }
00248        glActiveTexture(GL_TEXTURE4);
00249        glBindTexture(GL_TEXTURE_2D,0);
00250        glActiveTexture(GL_TEXTURE3);
00251        glBindTexture(GL_TEXTURE_2D,0);
00252        glActiveTexture(GL_TEXTURE2);
00253        glBindTexture(GL_TEXTURE_2D,0);
00254        glActiveTexture(GL_TEXTURE1);  
00255        glBindTexture(GL_TEXTURE_3D,0);
00256        glActiveTexture(GL_TEXTURE0);  
00257        glBindTexture(GL_TEXTURE_3D,0);
00258 //       glDisable(GL_TEXTURE_3D);
00259 //       glEnable(GL_TEXTURE_2D);
00260      } // pass
00261      bFirstTex=FALSE;
00262    } // ext shader
00263  }
00264  glDisable(GL_COLOR_MATERIAL);
00265 }
00266 
00267 static void DrawExternalTexturePolygons(long k, int pass, // material ID
00268                                         GLuint progID, GLint attrloc, 
00269                                         long Nface, face *MainFp, long Nvert, vertex *MainVp){
00270  face *fp;  // This function is called back from the shader - possibly several times (blending - depth etc.)
00271  int i,j;
00272  vertex *vp;
00273  GLfloat x,y,z,uu,vv,ww;
00274  glBegin(GL_TRIANGLES);
00275  fp=MainFp; for(i=0;i<Nface;i++,fp++){ // draw the polys with external materials
00276    if(fp->ffmat != k || fp->ffmap >= 0  || fp->ffmat < 0)continue; //skip all except materials
00277    for(j=0;j<3;j++){
00278      vp=(MainVp+(fp->V[j]));
00279      x=((GLfloat)(vp->p[0]))*GlobalScale;
00280      y=((GLfloat)(vp->p[1]))*GlobalScale;
00281      z=((GLfloat)(vp->p[2]))*GlobalScale;
00282      if(fp->bSmooth){
00283        if(fp->pn[j][1] > 0.0)glNormal3f(-fp->pn[j][0],-fp->pn[j][2],fp->pn[j][1]);
00284        else                  glNormal3f(fp->pn[j][0],fp->pn[j][2],-fp->pn[j][1]);
00285      }
00286      else {
00287        if(fp->n[1] > 0.0)glNormal3f(-fp->n[0],-fp->n[2],fp->n[1]);
00288        else              glNormal3f(fp->n[0],fp->n[2],-fp->n[1]);
00289      }
00290      uu=fp->uu[j]; vv=fp->vv[j]; ww=fp->ww[j];
00291      SetAttributeVectorID(progID,attrloc,uu,vv,ww);  
00292      glVertex3f(x,z,-y);
00293    }
00294  }
00295  glEnd();
00296 }
00297 
00298 
00300 
00301 #if 0
00302 long RenderGL(long frame){  // Old way does not  -  blend in hardware and is slow - NOT used at prsent
00303  int  i,aa;
00304  unsigned char *p,*q;
00305  glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, g_frameBuffer );
00306 // glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, 
00307 //              GL_RENDERBUFFER_EXT, g_accumRenderBuffer );
00308  glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, 
00309                 GL_RENDERBUFFER_EXT, g_colorRenderBuffer );
00310  glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, 
00311                 GL_RENDERBUFFER_EXT, g_depthRenderBuffer );
00312 // Stencil Buffers do not work when attached to FBOs
00313 // glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, 
00314 //    GL_RENDERBUFFER_EXT, g_stencilRenderBuffer );
00315 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
00316  glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
00317  glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
00318  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00319  for(aa=0;aa<nAAsamples;aa++){ 
00320    RenderCameraView(TRUE,bAntiAliased,aa,nAAsamples,(GLfloat)RENDERBUFFER_WIDTH/(GLfloat)RENDERBUFFER_HEIGHT);
00321    glFlush();
00322    glReadPixels(0,0,RENDERBUFFER_WIDTH,RENDERBUFFER_HEIGHT,GL_RGBA,GL_UNSIGNED_BYTE,RenderBufferCopy);  // or this
00323    if(aa == 0)memcpy(RenderBufferAccu,RenderBufferCopy,RENDERBUFFER_WIDTH*RENDERBUFFER_HEIGHT*4);
00324    else{
00325      p=RenderBufferCopy; q=RenderBufferAccu; 
00326      for(i=0;i< RENDERBUFFER_WIDTH*RENDERBUFFER_HEIGHT*4;i++){
00327        *q = (unsigned char)((((float)*q)*((float)aa-1.0) + ((float)*p))/(float)aa  );
00328        p++; q++;
00329      }
00330    }
00331  }
00332  glFlush();
00333  glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
00334 
00335 
00337 
00338 #if 0   // this partially works works but is too slow on ATI hardware
00339  glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
00340  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00341  glPixelZoom(1.0,1.0);
00342  //glBlendFunc(GL_ONE,GL_ZERO);
00343  //glBlendFunc(GL_ZERO,GL_ONE);
00344  //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00345  //glEnable(GL_BLEND);
00346  UseShaderProgram(5); // This is impossibly slow on ATI hardware
00347  glCopyPixels(0,0,RENDERBUFFER_WIDTH,RENDERBUFFER_HEIGHT,GL_COLOR);
00348  UseShaderProgram(0); 
00349  //glDisable(GL_BLEND);
00350  glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
00351 #endif
00352 
00353 #if 0
00354  { // this also works - but is too slow on ATI hardware
00355  int i,j;
00356  p=RenderBufferCopy;
00357  glReadPixels(0,0,RENDERBUFFER_WIDTH,RENDERBUFFER_HEIGHT,GL_RGBA,GL_UNSIGNED_BYTE,RenderBufferCopy);
00358  glFlush(); 
00359  //for(i=0;i< RENDERBUFFER_WIDTH*RENDERBUFFER_HEIGHT*4;i++){*p = *p/4; p++; }
00360  glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
00361  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00362  glMatrixMode(GL_PROJECTION);
00363  glLoadIdentity();
00364  glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
00365  glMatrixMode( GL_MODELVIEW );
00366  glPixelZoom(1.0,1.0);
00367  glRasterPos2i(0,0);
00368  UseShaderProgram(5); 
00369  glDrawPixels(RENDERBUFFER_WIDTH,RENDERBUFFER_HEIGHT,GL_RGBA,GL_UNSIGNED_BYTE,RenderBufferCopy);
00370  UseShaderProgram(0); 
00371  glFlush();
00372  glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
00373 }
00374 #endif
00375 
00376  return 1;
00377 }
00378 #endif
00379 
00380 
00381 

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