| 1 | #include "nvparse_errors.h" |
|---|
| 2 | #include "nvparse_externs.h" |
|---|
| 3 | #include <string.h> |
|---|
| 4 | #include <string> |
|---|
| 5 | #include <ctype.h> |
|---|
| 6 | |
|---|
| 7 | #include <OgreGLPrerequisites.h> |
|---|
| 8 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
|---|
| 9 | # include <OpenGL/glu.h> |
|---|
| 10 | #else |
|---|
| 11 | # include <GL/glu.h> |
|---|
| 12 | #endif |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | using namespace std; |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | namespace |
|---|
| 19 | { |
|---|
| 20 | void LoadProgram( GLenum target, GLuint id, char *instring ); |
|---|
| 21 | void StrToUpper(char * string); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | bool is_vsp10(const char * s) |
|---|
| 26 | { |
|---|
| 27 | return ! strncmp(s, "!!VSP1.0", 8); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | bool vsp10_init(char * s) |
|---|
| 31 | { |
|---|
| 32 | static bool vpinit = false; |
|---|
| 33 | if (vpinit == false ) |
|---|
| 34 | { |
|---|
| 35 | /* |
|---|
| 36 | if(! glh_init_extensions("GL_NV_vertex_program")) |
|---|
| 37 | { |
|---|
| 38 | errors.set("unable to initialize GL_NV_vertex_program"); |
|---|
| 39 | return false; |
|---|
| 40 | } |
|---|
| 41 | else |
|---|
| 42 | { |
|---|
| 43 | */ |
|---|
| 44 | vpinit = true; |
|---|
| 45 | /* |
|---|
| 46 | } |
|---|
| 47 | */ |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | errors.reset(); |
|---|
| 51 | line_number = 1; |
|---|
| 52 | myin = s; |
|---|
| 53 | |
|---|
| 54 | return true; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | int vsp10_parse(int vpsid) |
|---|
| 58 | { |
|---|
| 59 | LoadProgram( GL_VERTEX_STATE_PROGRAM_NV, vpsid, myin ); |
|---|
| 60 | return 0; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | namespace |
|---|
| 64 | { |
|---|
| 65 | //.----------------------------------------------------------------------------. |
|---|
| 66 | //| Function : LoadProgram | |
|---|
| 67 | //| Description: Load a program into GL, and report any errors encountered. | |
|---|
| 68 | //.----------------------------------------------------------------------------. |
|---|
| 69 | void LoadProgram( GLenum target, GLuint id, char *instring ) |
|---|
| 70 | { |
|---|
| 71 | GLint errPos; |
|---|
| 72 | GLenum errCode; |
|---|
| 73 | const GLubyte *errString; |
|---|
| 74 | |
|---|
| 75 | int len = strlen(instring); |
|---|
| 76 | glLoadProgramNV( target, id, len, (const GLubyte *) instring ); |
|---|
| 77 | if ( (errCode = glGetError()) != GL_NO_ERROR ) |
|---|
| 78 | { |
|---|
| 79 | errString = gluErrorString( errCode ); |
|---|
| 80 | |
|---|
| 81 | glGetIntegerv( GL_PROGRAM_ERROR_POSITION_NV, &errPos ); |
|---|
| 82 | |
|---|
| 83 | int nlines = 1; |
|---|
| 84 | int nchar = 1; |
|---|
| 85 | int i; |
|---|
| 86 | for ( i = 0; i < errPos; i++ ) |
|---|
| 87 | { |
|---|
| 88 | if ( instring[i] == '\n' ) |
|---|
| 89 | { |
|---|
| 90 | nlines++; |
|---|
| 91 | nchar = 1; |
|---|
| 92 | } |
|---|
| 93 | else |
|---|
| 94 | { |
|---|
| 95 | nchar++; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | int start; |
|---|
| 99 | int end; |
|---|
| 100 | int flag = ((instring[errPos]==';') | (instring[errPos-1]==';')) ? 1 : 0; |
|---|
| 101 | for ( i = errPos; i >= 0; i-- ) |
|---|
| 102 | { |
|---|
| 103 | start = i; |
|---|
| 104 | if ( flag && (start >= errPos-1) ) |
|---|
| 105 | continue; |
|---|
| 106 | if ( instring[i] == ';' ) |
|---|
| 107 | { |
|---|
| 108 | if ( !flag ) |
|---|
| 109 | { |
|---|
| 110 | start = i+1; |
|---|
| 111 | if ( instring[start] == '\n' ) |
|---|
| 112 | start++; |
|---|
| 113 | } |
|---|
| 114 | break; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | for ( i = errPos; i < len; i++ ) |
|---|
| 118 | { |
|---|
| 119 | end = i; |
|---|
| 120 | if ( instring[i] == ';' && end > start) |
|---|
| 121 | { |
|---|
| 122 | break; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | if ( errPos - start > 30 ) |
|---|
| 126 | { |
|---|
| 127 | start = errPos - 30; |
|---|
| 128 | } |
|---|
| 129 | if ( end - errPos > 30 ) |
|---|
| 130 | { |
|---|
| 131 | end = errPos + 30; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | char substring[96]; |
|---|
| 135 | memset( substring, 0, 96 ); |
|---|
| 136 | strncpy( substring, &(instring[start]), end-start+1 ); |
|---|
| 137 | char str[256]; |
|---|
| 138 | //sprintf( str, "error at line %d character %d\n \"%s\"\n", nlines, nchar, substring ); |
|---|
| 139 | sprintf( str, "error at line %d character %d\n\"%s\"\n", nlines, nchar, substring ); |
|---|
| 140 | int width = errPos-start; |
|---|
| 141 | for ( i = 0; i < width; i++ ) |
|---|
| 142 | { |
|---|
| 143 | strcat( str, " " ); |
|---|
| 144 | } |
|---|
| 145 | strcat( str, "|\n" ); |
|---|
| 146 | for ( i = 0; i < width; i++ ) |
|---|
| 147 | { |
|---|
| 148 | strcat( str, " " ); |
|---|
| 149 | } |
|---|
| 150 | strcat( str, "^\n" ); |
|---|
| 151 | |
|---|
| 152 | errors.set( str ); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | //.----------------------------------------------------------------------------. |
|---|
| 158 | //| Function : StrToUpper | |
|---|
| 159 | //| Description: Converts all lowercase chars in a string to uppercase. | |
|---|
| 160 | //.----------------------------------------------------------------------------. |
|---|
| 161 | void StrToUpper(char *string) |
|---|
| 162 | { |
|---|
| 163 | for (unsigned int i = 0; i < strlen(string); i++) |
|---|
| 164 | string[i] = toupper(string[i]); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | } |
|---|
| 168 | /* |
|---|
| 169 | else if(!strncmp(instring, "!!VSP1.0", 8)) |
|---|
| 170 | { |
|---|
| 171 | if (vpinit == 0 ) |
|---|
| 172 | { |
|---|
| 173 | if(! glh_init_extensions("GL_NV_vertex_program")) |
|---|
| 174 | { |
|---|
| 175 | errors.set("unable to initialize GL_NV_vertex_program"); |
|---|
| 176 | free(instring); |
|---|
| 177 | return; |
|---|
| 178 | } |
|---|
| 179 | else |
|---|
| 180 | { |
|---|
| 181 | vpinit = 1; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | errors.reset(); |
|---|
| 186 | line_number = 1; |
|---|
| 187 | |
|---|
| 188 | va_list ap; |
|---|
| 189 | va_start(ap, input_string); |
|---|
| 190 | int vpsid = va_arg(ap,int); |
|---|
| 191 | va_end(ap); |
|---|
| 192 | |
|---|
| 193 | if ( glGetError() != GL_NO_ERROR ) |
|---|
| 194 | { |
|---|
| 195 | errors.set( "Previous GL_ERROR prior to vertex state program parsing." ); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | LoadProgram( GL_VERTEX_STATE_PROGRAM_NV, vpsid, instring ); |
|---|
| 199 | } |
|---|
| 200 | */ |
|---|