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