00001 #include "Shader.h"
00002
00003 Shader::Shader(void)
00004 {
00005
00006 }
00007
00008 Shader::~Shader(void)
00009 {
00010 glDeleteShader(m_fragment);
00011 glDeleteShader(m_vertex);
00012 glDeleteShader(m_program);
00013 }
00014
00015 Shader::Shader(const char* in_vert, const char* in_frag)
00016 {
00017 Load(in_vert, in_frag);
00018 }
00019
00020 bool Shader::Load(const char* in_vert, const char* in_frag)
00021 {
00022 char* vert = ReadFromFile(in_vert);
00023 char* frag = ReadFromFile(in_frag);
00024
00025 if(vert == NULL || frag == NULL)
00026 {
00027 printf("Failed to load Shader\n");
00028 exit(0);
00029 }
00030 else
00031 {
00032 const char* v = vert;
00033 const char* f = frag;
00034
00035 m_vertex = glCreateShader(GL_VERTEX_SHADER);
00036 m_fragment = glCreateShader(GL_FRAGMENT_SHADER);
00037
00038 glShaderSource(m_vertex, 1, &v, NULL);
00039 glShaderSource(m_fragment, 1, &f, NULL);
00040
00041 free(vert);
00042 free(frag);
00043
00044
00045 glCompileShader(m_vertex);
00046 glCompileShader(m_fragment);
00047
00048
00049 ShaderLog(m_vertex);
00050 ShaderLog(m_fragment);
00051
00052
00053 m_program = glCreateProgram();
00054
00055
00056 glAttachShader(m_program, m_vertex);
00057 glAttachShader(m_program, m_fragment);
00058
00059 glLinkProgram(m_program);
00060
00061
00062 ProgramLog(m_program);
00063
00064 glUseProgram(0);
00065 }
00066 return true;
00067 }
00068
00069 char* Shader::ReadFromFile( const char* shaderFile )
00070 {
00071 FILE *file;
00072 char *content = NULL;
00073 int count = 0;
00074
00075 if ( shaderFile != NULL ) {
00076 fopen_s( &file, shaderFile, "rt" );
00077
00078 if (file != NULL) {
00079 fseek( file, 0, SEEK_END );
00080 count = ftell( file );
00081 rewind( file );
00082
00083 if ( count > 0 ) {
00084 content = (char*) malloc( sizeof(char) * (count + 1) );
00085 count = (int) fread( content, sizeof(char), count, file );
00086 content[count] = '\0';
00087 }
00088 fclose( file );
00089 }
00090 }
00091 return content;
00092 }
00093
00094 void Shader::ShaderLog(GLuint obj)
00095 {
00096
00097 GLint compileStatus;
00098 GLint shaderType;
00099
00100 glGetShaderiv(obj, GL_COMPILE_STATUS, &compileStatus);
00101 glGetShaderiv(obj, GL_SHADER_TYPE, &shaderType);
00102
00103 printOglError("ShaderLog: \n");
00104
00105
00106 if(shaderType == GL_VERTEX_SHADER && compileStatus == GL_FALSE)
00107 {
00108 printf("Error: Compiling Vertex Shader\n");
00109 }
00110
00111 if(shaderType == GL_FRAGMENT_SHADER && compileStatus == GL_FALSE)
00112 {
00113 printf("Error: Compiling Fragment Shader\n");
00114 }
00115
00117 int infologLength = 0;
00118 int charsWritten = 0;
00119 char *infoLog;
00120
00121 glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
00122
00123 if (infologLength > 0)
00124 {
00125 infoLog = (char *)malloc(infologLength);
00126 glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
00127 printf("%s\n",infoLog);
00128 free(infoLog);
00129 }
00130
00131 printOglError("ShaderLog\n");
00132 }
00133
00134 void Shader::ProgramLog(GLuint obj)
00135 {
00136
00137 int linkStatus;
00138
00139 glGetProgramiv(m_program, GL_LINK_STATUS, &linkStatus);
00140
00141 printOglError("ProgramLog");
00142
00143 if(linkStatus == GL_FALSE)
00144 {
00145 printf("Error: Linking program\n");
00146 }
00147
00148
00149 int infologLength = 0;
00150 int charsWritten = 0;
00151 char *infoLog;
00152
00153 glGetProgramiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
00154
00155 if (infologLength > 0)
00156 {
00157 infoLog = (char *)malloc(infologLength);
00158 glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
00159 printf("%s\n",infoLog);
00160 free(infoLog);
00161 }
00162 }
00163
00164 void Shader::Bind()
00165 {
00166 glUseProgram(m_program);
00167 }
00168
00169 void Shader::Unbind()
00170 {
00171 glUseProgram(0);
00172 }
00173
00174 GLuint Shader::GetProgram()
00175 {
00176 return m_program;
00177 }