shaders.c

Go to the documentation of this file.
00001 // shaders.c
00002 
00003 #include <windows.h>
00004 
00005 #include <commctrl.h>
00006 #include <gl\gl.h>
00007 #include <gl\glu.h>
00008 #include <math.h>
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <io.h>
00012 #include <fcntl.h>
00013 #include <string.h>
00014 
00015 #define __gl2_main_
00016 #include "..\gl2.h"
00017 
00018 #include "shaders.h"
00019 
00020 #define N_PROGRAMS_MAX 30
00021 
00022 static GLuint vertList[N_PROGRAMS_MAX], fragList[N_PROGRAMS_MAX], progList[N_PROGRAMS_MAX];
00023 static BOOL PluginsActiveID[N_PROGRAMS_MAX];
00024 static BOOL bShadersLoaded=FALSE,bFailed=FALSE;
00025 
00026 #define nPrograms 8
00027 static char *ProgramListD[nPrograms]={"NULL","DPhong1","Dphong2","Dnoise","Dmapping","accum",NULL,NULL};
00028 static char *ProgramListR[nPrograms]={"NULL","RPhong1","Rphong2","Rnoise","Rmapping","accum","ground","sky"};
00029 static char *ProgramListT[nPrograms]={"NULL","TPhong1","Tphong2","Tnoise","Tmapping","accum","ground","sky"};
00030 
00031 static char OFXroot[512];
00032 
00033 static BOOL bTour=FALSE;
00034 
00035 void ShadersInitTour(char *root){
00036   bTour=TRUE;
00037   ShadersInit(root,FALSE);
00038 }
00039 
00040 void ShadersInit(char *root, BOOL preview){
00041  int i;
00042  char folder[512]; 
00043  GLint status;
00044  char *vs1;
00045  char *fs1;
00046  char *list;
00047  bShadersLoaded=TRUE;
00048  for(i=0;i<N_PROGRAMS_MAX;i++)PluginsActiveID[i]=FALSE;
00049  strcpy(folder,root);
00050  strcpy(OFXroot,root);
00051  strcat(folder,"shaders\\");
00052  for(i=1;i<nPrograms;i++){
00053    PluginsActiveID[i]=TRUE;
00054    vs1=NULL; fs1=NULL;
00055    vertList[i] = -1;   fragList[i] = -1;   progList[i] = -1;
00056    if(bTour)list=ProgramListT[i];
00057    else list=(preview ? ProgramListD[i]: ProgramListR[i]);
00058    if(list == NULL)continue;
00059    if(gl2ReadShaderSource(list,folder, &vs1, &fs1) == 1 &&
00060      vs1 != NULL && fs1 != NULL){
00061      vertList[i] = glCreateShader(GL_VERTEX_SHADER);
00062      fragList[i] = glCreateShader(GL_FRAGMENT_SHADER);
00063      glShaderSource(vertList[i], 1, &vs1,NULL);
00064      glShaderSource(fragList[i], 1, &fs1,NULL);
00065      if(i != 5){
00066        glCompileShader(vertList[i]);
00067        glGetShaderiv(vertList[i],GL_COMPILE_STATUS,&status);
00068        if(status == GL_FALSE){MessageBox(NULL,"Error in Vertex shader",NULL,MB_OK); bFailed=TRUE;}
00069      } 
00070      glCompileShader(fragList[i]);
00071      glGetShaderiv(fragList[i],GL_COMPILE_STATUS,&status);
00072      if(status == GL_FALSE){MessageBox(NULL,"Error in Fragment shader",NULL,MB_OK); bFailed=TRUE;}
00073      progList[i] = glCreateProgram();
00074      if(i != 5)glAttachShader(progList[i],vertList[i]);
00075      glAttachShader(progList[i],fragList[i]);
00076      glLinkProgram(progList[i]);
00077    }
00078    else bShadersLoaded=FALSE;
00079    if(vs1 != NULL)free(vs1);
00080    if(fs1 != NULL)free(fs1);
00081  }
00082  if(!bShadersLoaded){
00083   MessageBox(NULL,"Could Not Load Shader Programs",NULL,MB_OK);
00084   bFailed=TRUE;
00085  }
00086 }
00087 
00088 static int GetNextPluginProgramID(void){
00089  int i; 
00090  for(i=nPrograms;i<N_PROGRAMS_MAX;i++)if(!PluginsActiveID[i])return i;
00091  return -1;
00092 }
00093 
00094 int LoadAndCompileShader(char *name){ // for plugin shaders
00095  BOOL bSucceeded=TRUE;
00096  char *vs1=NULL,*fs1=NULL;
00097  char folder[512];
00098  int id;
00099  GLint status;
00100  if(!bShadersLoaded)return -1;  // Only proceed if using OpenGL renderer and internal shaders loaded
00101  strcpy(folder,OFXroot);
00102  strcat(folder,"textures\\");
00103  if((id=GetNextPluginProgramID()) < 0)return -1;
00104  if(gl2ReadShaderSourceFunction(name,folder, "lighting", &vs1, &fs1) == 1 &&
00105     vs1 != NULL && fs1 != NULL){
00106    vertList[id] = glCreateShader(GL_VERTEX_SHADER);
00107    fragList[id] = glCreateShader(GL_FRAGMENT_SHADER);
00108    glShaderSource(vertList[id], 1, &vs1,NULL);
00109    glShaderSource(fragList[id], 1, &fs1,NULL);
00110    glCompileShader(vertList[id]);
00111    glGetShaderiv(vertList[id],GL_COMPILE_STATUS,&status);
00112    if(status == GL_FALSE){MessageBox(NULL,"Error in Plugin Vertex shader",name,MB_OK); bSucceeded=FALSE;}
00113    glCompileShader(fragList[id]);
00114    glGetShaderiv(fragList[id],GL_COMPILE_STATUS,&status);
00115    if(status == GL_FALSE){MessageBox(NULL,"Error in Plugin Fragment shader",name,MB_OK); bSucceeded=FALSE;}
00116    progList[id] = glCreateProgram();
00117    glAttachShader(progList[id],vertList[id]);
00118    glAttachShader(progList[id],fragList[id]);
00119    glLinkProgram(progList[id]);
00120  }
00121  else bSucceeded=FALSE;
00122  if(vs1 != NULL)free(vs1);
00123  if(fs1 != NULL)free(fs1);
00124  if(!bSucceeded)return -1;
00125  PluginsActiveID[id]=TRUE;   
00126  return id;
00127 }
00128 
00129 void UnloadCompiledShader(int id){
00130  if(!PluginsActiveID[id])return;
00131  if(glIsShader(vertList[id]) == GL_TRUE)glDeleteShader(vertList[id]);
00132  if(glIsShader(fragList[id]) == GL_TRUE)glDeleteShader(fragList[id]);
00133  if(glIsProgram(progList[id]) == GL_TRUE)glDeleteProgram(progList[id]);
00134  PluginsActiveID[id]=FALSE; 
00135 }
00136 
00137 void UseSingleShaderProgram(long id){
00138  if(id < 0)return;
00139  else if(id == 0)glUseProgram(0);
00140  else glUseProgram(progList[id]);
00141 }
00142 
00143 void UseShaderProgram(long id){
00144  if(!bShadersLoaded)return;
00145  if(id == 0)glUseProgram(0);
00146  else  glUseProgram(progList[id]);
00147 }
00148 
00149 void UnloadShaders(void){
00150  int i;
00151  if(!bShadersLoaded)return;
00152  for(i=1;i<nPrograms;i++){
00153    if(glIsShader(vertList[i]) == GL_TRUE)glDeleteShader(vertList[i]);
00154    if(glIsShader(fragList[i]) == GL_TRUE)glDeleteShader(fragList[i]);
00155    if(glIsProgram(progList[i]) == GL_TRUE)glDeleteProgram(progList[i]);
00156  }
00157  bShadersLoaded=FALSE;
00158 }
00159 
00160 void SetUniformInteger(int id, char *name, GLint val){
00161  GLint loc;
00162  if(bFailed)return;
00163  if((loc=glGetUniformLocation(progList[id],name)) == -1){;}
00164  //      MessageBox(NULL,"This variable is not in the Shader",name,MB_OK);
00165  else  glUniform1i(loc,val);
00166  
00167 }
00168 
00169 void SetUniformVariable(int id, char *name, GLfloat val){
00170  GLint loc;
00171  if(bFailed)return;
00172  if((loc=glGetUniformLocation(progList[id],name)) == -1){;}
00173  //       MessageBox(NULL,"This variable is not in the Shader",name,MB_OK);
00174  else  glUniform1f(loc,val);
00175 }
00176 
00177 void SetUniformVector(int id, char *name, GLfloat a, GLfloat b, GLfloat c){
00178  GLint loc;
00179  if(bFailed)return;
00180  if((loc=glGetUniformLocation(progList[id],name)) == -1){;}
00181  //      MessageBox(NULL,"This variable is not in the Shader",name,MB_OK);
00182  else glUniform3f(loc,a,b,c);
00183 }
00184 
00185 void SetUniformVector2(int id, char *name, GLfloat a, GLfloat b){
00186  GLint loc;
00187  if(bFailed)return;
00188  if((loc=glGetUniformLocation(progList[id],name)) == -1){;}
00189  //     MessageBox(NULL,"This variable is not in the Shader",name,MB_OK);
00190  else glUniform2f(loc,a,b);
00191 }
00192 
00193 void SetUniformMatrix3(int id, char *name, GLfloat m[3][3]){ // DOES NOT APPEAR TO send data to shader programs
00194  GLint loc;
00195  GLfloat mm[9];
00196  int i,j,k; for(i=0,k=0;i<3;i++)for(j=0;j<3;j++){mm[k]=m[i][j]; k++;}
00197  if(bFailed)return;
00198  if((loc=glGetUniformLocation(progList[id],name)) == -1)
00199       MessageBox(NULL,"This uniform matrix is not in the Shader",name,MB_OK);
00200  else  glUniformMatrix3fv(loc, 9, TRUE, mm);
00201 }
00202 
00203 void SetUniformMatrix4(int id, char *name, GLfloat m[4][4]){ // same as MAT3
00204  GLint loc;
00205  if(bFailed)return;
00206  if((loc=glGetUniformLocation(progList[id],name)) == -1)
00207       MessageBox(NULL,"This uniform 4 matrix is not in the Shader",name,MB_OK);
00208  else  glUniformMatrix3fv(loc, 16, TRUE, (GLfloat *)m);
00209 }
00210 
00211 void SetAttributeInteger(int id, char *name, GLshort val){
00212   GLint loc;
00213   if(bFailed)return;
00214   loc=glGetAttribLocation(progList[id],name);
00215   if(loc == -1){/*MessageBox(NULL,"This attribute is not in the Shader",name,MB_OK);*/ return;}
00216   glVertexAttrib1s(loc,val);
00217 }
00218 
00219 void SetAttributeVariable(int id, char *name, GLfloat val){
00220   GLint loc;
00221   if(bFailed)return;
00222   loc=glGetAttribLocation(progList[id],name);
00223   if(loc == -1){/*MessageBox(NULL,"This attribute is not in the Shader",name,MB_OK);*/ return;}
00224   glVertexAttrib1f(loc,val);
00225 }
00226 
00227 void SetAttributeVector(int id, char *name, GLfloat a, GLfloat b, GLfloat c){
00228  GLint loc;
00229  if(bFailed)return;
00230  loc=glGetAttribLocation(progList[id],name);
00231  if(loc == -1){/*MessageBox(NULL,"This attribute is not in the Shader",name,MB_OK);*/ return;}
00232  glVertexAttrib3f(loc,a,b,c);
00233 }
00234 
00235 void BindAttributeVector(int id, char *name, int loc){
00236  if(bFailed)return;
00237  glBindAttribLocation(progList[id],loc,name);
00238 }
00239 
00240 void SetAttributeVectorID(int id, int loc, GLfloat a, GLfloat b, GLfloat c){
00241  if(bFailed)return;
00242  glVertexAttrib3f(loc,a,b,c);
00243 }
00244 
00245 GLint GetAttibuteLocation(int id, char *name){
00246  GLint loc;
00247  if(bFailed)return -1;
00248  loc=glGetAttribLocation(progList[id],name);
00249  if(loc == -1){/*MessageBox(NULL,"This attribute is not in the Shader",name,MB_OK);*/ return -1;}
00250  return loc;
00251 }

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