00001
00032
00033
00034
00035 #include "linden_common.h"
00036
00037 #include "llvertexprogramgl.h"
00038
00039 #include "llglheaders.h"
00040
00041 LLVertexProgramGL::LLVertexProgramGL()
00042 {
00043 mVertexProgramID = 0;
00044 }
00045
00046 LLVertexProgramGL::~LLVertexProgramGL()
00047 {
00048 glDeleteProgramsARB(1, (GLuint*) &mVertexProgramID);
00049 }
00050
00051 BOOL LLVertexProgramGL::load(const char * filename)
00052 {
00053 FILE *fp = fopen(filename, "r");
00054 if (!fp)
00055 {
00056 llwarns << "Unable to open vertex program " << filename << llendl;
00057 return FALSE;
00058 }
00059
00060 fseek(fp,0,SEEK_END);
00061 S32 file_size = ftell(fp);
00062 fseek(fp,0,SEEK_SET);
00063
00064 char *text_buffer = new char[file_size + 1];
00065
00066 S32 num_read = (S32)fread(text_buffer, sizeof(char), file_size, fp);
00067
00068 if (ferror(fp) || num_read == 0)
00069 {
00070 llwarns << "Unable to read vertex program " << filename << llendl;
00071 fclose(fp);
00072 delete[] text_buffer;
00073 return FALSE;
00074 }
00075 text_buffer[num_read] = '\0';
00076 fclose(fp);
00077
00078
00079 glGenProgramsARB(1, (GLuint*) &mVertexProgramID);
00080 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, mVertexProgramID);
00081 glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, num_read, text_buffer);
00082
00083 const GLubyte * program_error_string = glGetString(GL_PROGRAM_ERROR_STRING_ARB);
00084
00085 GLint error_pos;
00086 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);
00087
00088 if(error_pos!=-1)
00089 {
00090 glGetError();
00091 S32 line_num = 1;
00092 char *next_token = strchr(text_buffer, '\n');
00093 while(NULL != next_token && next_token < (text_buffer + error_pos))
00094 {
00095 next_token++;
00096 line_num++;
00097 next_token = strchr(next_token, '\n');
00098 }
00099 char bad_code[11];
00100 strncpy(bad_code, text_buffer + error_pos, 10);
00101 bad_code[10] = '\0';
00102
00103 llerrs << filename << "(" << line_num << "): Vertex Program Error: "
00104 << program_error_string << " at (" << bad_code<< ")"
00105 << llendl;
00106
00107 delete[] text_buffer;
00108 return FALSE;
00109 }
00110
00111
00112 delete[] text_buffer;
00113 return TRUE;
00114 }
00115
00116 void LLVertexProgramGL::bind()
00117 {
00118 glEnable(GL_VERTEX_PROGRAM_ARB);
00119 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, mVertexProgramID);
00120 }
00121
00122 void LLVertexProgramGL::unbind()
00123 {
00124 glDisable(GL_VERTEX_PROGRAM_ARB);
00125 }