[692] | 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 |
|
---|
| 9 | #ifndef _WIN32
|
---|
| 10 | # define strnicmp strncasecmp
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
| 13 | using namespace std;
|
---|
| 14 |
|
---|
| 15 | namespace
|
---|
| 16 | {
|
---|
| 17 | void ParseVertexProgramConstants( GLenum target, char *instring);
|
---|
| 18 | GLuint LookupTrackMatrix(char *matrixName);
|
---|
| 19 | GLuint LookupTrackMatrixTransform(char *matrixTransformName);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | bool is_vcp10(const char * s)
|
---|
| 23 | {
|
---|
| 24 | return ! strncmp(s, "!!VCP1.0", 8);
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | bool vcp10_init(char * s)
|
---|
| 28 | {
|
---|
| 29 | static int vpinit = 0;
|
---|
| 30 |
|
---|
| 31 | if (vpinit == 0 )
|
---|
| 32 | {
|
---|
| 33 | /*
|
---|
| 34 | if(! glh_init_extensions("GL_NV_vertex_program"))
|
---|
| 35 | {
|
---|
| 36 | errors.set("unable to initialize GL_NV_vertex_program");
|
---|
| 37 | return false;
|
---|
| 38 | }
|
---|
| 39 | else
|
---|
| 40 | {
|
---|
| 41 | */
|
---|
| 42 | vpinit = 1;
|
---|
| 43 | /*
|
---|
| 44 | }
|
---|
| 45 | */
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | errors.reset();
|
---|
| 49 | line_number = 1;
|
---|
| 50 |
|
---|
| 51 | myin = s;
|
---|
| 52 |
|
---|
| 53 | return true;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | int vcp10_parse()
|
---|
| 57 | {
|
---|
| 58 | // parse the constant declarations, setting their values in the GL.
|
---|
| 59 | ParseVertexProgramConstants( GL_VERTEX_PROGRAM_NV, myin);
|
---|
| 60 | return 0;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | namespace
|
---|
| 65 | {
|
---|
| 66 |
|
---|
| 67 | //.----------------------------------------------------------------------------.
|
---|
| 68 | //| Function : ParseVertexProgramConstants |
|
---|
| 69 | //| Description: Parse and set VP1.0 constant memory based on const |
|
---|
| 70 | //| directives. |
|
---|
| 71 | //| |
|
---|
| 72 | //| Format : c[XX] = (x, y, z, w); # where XXX is an integer 0-95. |
|
---|
| 73 | //| : c[XX] = TRACK(matrix, transform); # track a matrix |
|
---|
| 74 | //.----------------------------------------------------------------------------.
|
---|
| 75 | void ParseVertexProgramConstants(GLenum target, char *instring)
|
---|
| 76 | {
|
---|
| 77 | // don't overwrite the original string.
|
---|
| 78 | char *tmpstring = new char[strlen(instring)+1];
|
---|
| 79 | strcpy(tmpstring, instring);
|
---|
| 80 |
|
---|
| 81 | char lineSeparator[] = "\n";
|
---|
| 82 | //char wordSeparator[] = " \t";
|
---|
| 83 | char error[256];
|
---|
| 84 | char dummy[256];
|
---|
| 85 | char *token;
|
---|
| 86 |
|
---|
| 87 | //iterate over the lines in the string
|
---|
| 88 | token = strtok(tmpstring, lineSeparator);
|
---|
| 89 |
|
---|
| 90 | // we assume the first line is the "!!VCP1.0 line".
|
---|
| 91 | if (token != NULL)
|
---|
| 92 | {
|
---|
| 93 | token = strtok(NULL, lineSeparator);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | int iLineCount = 1; // skip first line
|
---|
| 97 |
|
---|
| 98 | while (token != NULL)
|
---|
| 99 | {
|
---|
| 100 | iLineCount++;
|
---|
| 101 |
|
---|
| 102 | // if the first non-whitespace character is a #, this is a comment. Skip.
|
---|
| 103 | if (!sscanf(token, " #%s", dummy))
|
---|
| 104 | { // not a comment. Is it a constant?
|
---|
| 105 |
|
---|
| 106 | // strip whitespace from the beginning of the string
|
---|
| 107 | int i;
|
---|
| 108 | for (i = 0; i < (int)strlen(token) && isspace(token[i]); i++);
|
---|
| 109 | token += i;
|
---|
| 110 |
|
---|
| 111 | if (strlen(token) > 0 && // this is not a blank line and
|
---|
| 112 | !strnicmp(token, "c[", 2)) // the first word is of the form "c[xx]", so its a constant.
|
---|
| 113 | {
|
---|
| 114 | int iConstID;
|
---|
| 115 | int iNumValuesAssigned;
|
---|
| 116 | char c[6];
|
---|
| 117 |
|
---|
| 118 | iNumValuesAssigned = sscanf(token, " %c [ %d ] = %s ", &c[0], &iConstID, dummy);
|
---|
| 119 |
|
---|
| 120 | if (3 != iNumValuesAssigned || toupper(c[0]) != 'C')
|
---|
| 121 | { // error in constant directive.
|
---|
| 122 | sprintf(error, "error at line %d \n\"%s\"\n", iLineCount, token);
|
---|
| 123 | errors.set(error);
|
---|
| 124 | }
|
---|
| 125 | else if (!strnicmp(dummy, "track", 5))
|
---|
| 126 | { // this is a TrackMatrix directive
|
---|
| 127 | char matrixName[256], matrixTransformName[256];
|
---|
| 128 |
|
---|
| 129 | // the series of %c's are to make sure "track(" doesn't get glommed onto the matrixName
|
---|
| 130 | iNumValuesAssigned = sscanf(token,
|
---|
| 131 | " %c [ %d ] = %c%c%c%c%c ( %s %s ) ;",
|
---|
| 132 | &c[0], &iConstID, &c[1], &c[2], &c[3], &c[4], &c[5],
|
---|
| 133 | matrixName, matrixTransformName);
|
---|
| 134 |
|
---|
| 135 | if (iNumValuesAssigned < 8)
|
---|
| 136 | {
|
---|
| 137 | sprintf(error, "error at line %d \n\"%s\"\n", iLineCount, token);
|
---|
| 138 | errors.set(error);
|
---|
| 139 | }
|
---|
| 140 | else
|
---|
| 141 | {
|
---|
| 142 | char *buffer;
|
---|
| 143 | if (9 == iNumValuesAssigned)
|
---|
| 144 | {
|
---|
| 145 | // just need to remove any junk from the matrix names and IDs.
|
---|
| 146 | buffer = strstr(matrixName, ",");
|
---|
| 147 | if (buffer)
|
---|
| 148 | *buffer = 0;
|
---|
| 149 |
|
---|
| 150 | buffer = strstr(matrixTransformName, ")");
|
---|
| 151 | if (buffer)
|
---|
| 152 | *buffer = 0;
|
---|
| 153 | }
|
---|
| 154 | else // 8 == iNumValuesAssigned
|
---|
| 155 | {
|
---|
| 156 | // have to split the two names, since they both were put into the matrixName
|
---|
| 157 | buffer = strstr(matrixName, ",");
|
---|
| 158 | if (buffer)
|
---|
| 159 | {
|
---|
| 160 | strcpy(matrixTransformName, buffer + 1);
|
---|
| 161 | *buffer = 0;
|
---|
| 162 | // get rid of paren at end of transform name, if it is there
|
---|
| 163 | buffer = strstr(matrixTransformName, ")");
|
---|
| 164 | if (buffer)
|
---|
| 165 | *buffer = 0;
|
---|
| 166 |
|
---|
| 167 | }
|
---|
| 168 | else
|
---|
| 169 | {
|
---|
| 170 | sprintf(error, "error at line %d \n\"%s\"\n", iLineCount, token);
|
---|
| 171 | errors.set(error);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | // constant ID must be modulo 4.
|
---|
| 176 | if (0 != (iConstID % 4))
|
---|
| 177 | {
|
---|
| 178 | sprintf(error,
|
---|
| 179 | "error at line %d \n\"%s\"\n\tglTrackMatrixNV address must be modulo 4\n",
|
---|
| 180 | iLineCount, token);
|
---|
| 181 | errors.set(error);
|
---|
| 182 | }
|
---|
| 183 | else if (iConstID < 0 || iConstID > 95)
|
---|
| 184 | {
|
---|
| 185 | sprintf(error,
|
---|
| 186 | "error at line %d \n\"%s\"\n\tConstant address out of range\n",
|
---|
| 187 | iLineCount, token);
|
---|
| 188 | errors.set(error);
|
---|
| 189 | }
|
---|
| 190 | else
|
---|
| 191 | {
|
---|
| 192 | // get the enum values for the specified matrices
|
---|
| 193 | GLuint iMatrixID = LookupTrackMatrix(matrixName);
|
---|
| 194 | GLuint iTransformID = LookupTrackMatrixTransform(matrixTransformName);
|
---|
| 195 |
|
---|
| 196 | if (0 == iMatrixID)
|
---|
| 197 | {
|
---|
| 198 | sprintf(error,
|
---|
| 199 | "error at line %d \n\"%s\"\n\tInvalid Matrix parameter in glTrackMatrixNV.\n",
|
---|
| 200 | iLineCount, token);
|
---|
| 201 | errors.set(error);
|
---|
| 202 | }
|
---|
| 203 | else if (0 == iTransformID)
|
---|
| 204 | {
|
---|
| 205 | sprintf(error,
|
---|
| 206 | "error at line %d \n\"%s\"\n\tInvalid Transform parameter in glTrackMatrixNV\n",
|
---|
| 207 | iLineCount, token);
|
---|
| 208 | errors.set(error);
|
---|
| 209 | }
|
---|
| 210 | else
|
---|
| 211 | {
|
---|
| 212 | // untrack any currently tracked matrix
|
---|
| 213 | glTrackMatrixNV(target, iConstID, GL_NONE, GL_IDENTITY_NV);
|
---|
| 214 |
|
---|
| 215 | // tell GL the matrix to track
|
---|
| 216 | glTrackMatrixNV(target, iConstID, iMatrixID, iTransformID);
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 | else // this is a constant directive
|
---|
| 222 | {
|
---|
| 223 | float xyzw[4] = {0, 0, 0, 0};
|
---|
| 224 | iNumValuesAssigned = sscanf(token,
|
---|
| 225 | " %c [ %d ] = ( %f , %f , %f , %f ) ; ",
|
---|
| 226 | &c[0], &iConstID, xyzw, xyzw + 1, xyzw + 2, xyzw + 3);
|
---|
| 227 |
|
---|
| 228 | if (6 != iNumValuesAssigned)
|
---|
| 229 | { // error in constant directive.
|
---|
| 230 | sprintf(error, "error at line %d \n\"%s\"\n", iLineCount, token);
|
---|
| 231 | errors.set(error);
|
---|
| 232 | }
|
---|
| 233 | else if (iConstID < 0 || iConstID > 95)
|
---|
| 234 | {
|
---|
| 235 | sprintf(error,
|
---|
| 236 | "error at line %d \n\"%s\"\n\tConstant address out of range\n",
|
---|
| 237 | iLineCount, token);
|
---|
| 238 | errors.set(error);
|
---|
| 239 | }
|
---|
| 240 | else
|
---|
| 241 | {
|
---|
| 242 | // Always set the closest matrix location to tracking NONE to avoid errors!
|
---|
| 243 | glTrackMatrixNV(target, iConstID - (iConstID % 4), GL_NONE, GL_IDENTITY_NV);
|
---|
| 244 |
|
---|
| 245 | // tell GL the constant values
|
---|
| 246 | glProgramParameter4fvNV(target, iConstID, xyzw);
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | // get the next line
|
---|
| 253 | token = strtok(NULL, lineSeparator);
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 |
|
---|
| 258 | struct MatrixLookupEntry
|
---|
| 259 | {
|
---|
| 260 | string name;
|
---|
| 261 | GLuint ID;
|
---|
| 262 | };
|
---|
| 263 |
|
---|
| 264 |
|
---|
| 265 | //.----------------------------------------------------------------------------.
|
---|
| 266 | //| Function : LookupTrackMatrix |
|
---|
| 267 | //| Description: Returns the enumerated matrix name given a valid string |
|
---|
| 268 | //| or 0 if an unknown matrix is requested. |
|
---|
| 269 | //.----------------------------------------------------------------------------.
|
---|
| 270 | GLuint LookupTrackMatrix(char *matrixName)
|
---|
| 271 | {
|
---|
| 272 | static bool bFirstTime = true;
|
---|
| 273 | static int iNumEntries = 14;
|
---|
| 274 | static MatrixLookupEntry* matrixLookupTable = new MatrixLookupEntry[iNumEntries];
|
---|
| 275 |
|
---|
| 276 | if (bFirstTime) // build the lookup table
|
---|
| 277 | {
|
---|
| 278 | matrixLookupTable[0].name = "GL_NONE";
|
---|
| 279 | matrixLookupTable[0].ID = GL_NONE;
|
---|
| 280 | matrixLookupTable[1].name = "GL_MODELVIEW";
|
---|
| 281 | matrixLookupTable[1].ID = GL_MODELVIEW;
|
---|
| 282 | matrixLookupTable[2].name = "GL_PROJECTION";
|
---|
| 283 | matrixLookupTable[2].ID = GL_PROJECTION;
|
---|
| 284 | matrixLookupTable[3].name = "GL_TEXTURE";
|
---|
| 285 | matrixLookupTable[3].ID = GL_TEXTURE;
|
---|
| 286 | matrixLookupTable[4].name = "GL_COLOR";
|
---|
| 287 | matrixLookupTable[4].ID = GL_COLOR;
|
---|
| 288 | matrixLookupTable[5].name = "GL_MODELVIEW_PROJECTION_NV";
|
---|
| 289 | matrixLookupTable[5].ID = GL_MODELVIEW_PROJECTION_NV;
|
---|
| 290 | matrixLookupTable[6].name = "GL_MATRIX0_NV";
|
---|
| 291 | matrixLookupTable[6].ID = GL_MATRIX0_NV;
|
---|
| 292 | matrixLookupTable[7].name = "GL_MATRIX1_NV";
|
---|
| 293 | matrixLookupTable[7].ID = GL_MATRIX1_NV;
|
---|
| 294 | matrixLookupTable[8].name = "GL_MATRIX2_NV";
|
---|
| 295 | matrixLookupTable[8].ID = GL_MATRIX2_NV;
|
---|
| 296 | matrixLookupTable[9].name = "GL_MATRIX3_NV";
|
---|
| 297 | matrixLookupTable[9].ID = GL_MATRIX3_NV;
|
---|
| 298 | matrixLookupTable[10].name = "GL_MATRIX4_NV";
|
---|
| 299 | matrixLookupTable[10].ID = GL_MATRIX4_NV;
|
---|
| 300 | matrixLookupTable[11].name = "GL_MATRIX5_NV";
|
---|
| 301 | matrixLookupTable[11].ID = GL_MATRIX5_NV;
|
---|
| 302 | matrixLookupTable[12].name = "GL_MATRIX6_NV";
|
---|
| 303 | matrixLookupTable[12].ID = GL_MATRIX6_NV;
|
---|
| 304 | matrixLookupTable[13].name = "GL_MATRIX7_NV";
|
---|
| 305 | matrixLookupTable[13].ID = GL_MATRIX7_NV;
|
---|
| 306 | bFirstTime = false;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | for (int i = 0; i < iNumEntries; i++)
|
---|
| 310 | {
|
---|
| 311 | if (!strcmp(matrixName, matrixLookupTable[i].name.c_str()))
|
---|
| 312 | {
|
---|
| 313 |
|
---|
| 314 | return matrixLookupTable[i].ID;
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | return 0;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | //.----------------------------------------------------------------------------.
|
---|
| 322 | //| Function : LookupTrackMatrixTransform |
|
---|
| 323 | //| Description: Returns the enumerated matrix transform name given a valid |
|
---|
| 324 | //| string name or 0 if an unknown transform is requested. |
|
---|
| 325 | //.----------------------------------------------------------------------------.
|
---|
| 326 | GLuint LookupTrackMatrixTransform(char *matrixTransformName)
|
---|
| 327 | {
|
---|
| 328 | static bool bFirstTime = true;
|
---|
| 329 | static int iNumEntries = 4;
|
---|
| 330 | static MatrixLookupEntry* transformLookupTable = new MatrixLookupEntry[iNumEntries];
|
---|
| 331 |
|
---|
| 332 | if (bFirstTime)
|
---|
| 333 | {
|
---|
| 334 | transformLookupTable[0].name = "GL_IDENTITY_NV";
|
---|
| 335 | transformLookupTable[0].ID = GL_IDENTITY_NV;
|
---|
| 336 | transformLookupTable[1].name = "GL_INVERSE_NV";
|
---|
| 337 | transformLookupTable[1].ID = GL_INVERSE_NV;
|
---|
| 338 | transformLookupTable[2].name = "GL_TRANSPOSE_NV";
|
---|
| 339 | transformLookupTable[2].ID = GL_TRANSPOSE_NV;
|
---|
| 340 | transformLookupTable[3].name = "GL_INVERSE_TRANSPOSE_NV";
|
---|
| 341 | transformLookupTable[3].ID = GL_INVERSE_TRANSPOSE_NV;
|
---|
| 342 | bFirstTime = false;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | for (int i = 0; i < iNumEntries; i++)
|
---|
| 346 | {
|
---|
| 347 | if (!strcmp( matrixTransformName, transformLookupTable[i].name.c_str()))
|
---|
| 348 | {
|
---|
| 349 | return transformLookupTable[i].ID;
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | return 0;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | }
|
---|