-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cpp
149 lines (142 loc) · 5.43 KB
/
parse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <functional>
#include <cstring>
#include "minigl.h"
#ifdef _WIN32
#include <windows.h>
#endif
#ifndef __APPLE__
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#else
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#endif
extern int width,height;
extern std::vector<std::function<void()> > commands_gl,commands_mgl;
void Parse(const char* test_file)
{
FILE* F = fopen(test_file,"r");
if(!F)
{
printf("Failed to open file '%s'\n",test_file);
exit(EXIT_FAILURE);
}
float f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15;
int i0,i1;
char buff[1000],c;
while(fgets(buff, sizeof(buff), F))
{
if(buff[0]=='#') continue;
if(buff[0]=='\n') continue;
if(sscanf(buff,"size %d %d\n",&i0,&i1)==2)
{
width=i0;
height=i1;
}
else if(sscanf(buff,"glBegin GL_TRIANGLES%c\n",&c)==1)
{
commands_gl.push_back([=](){glBegin(GL_TRIANGLES);});
commands_mgl.push_back([=](){mglBegin(MGL_TRIANGLES);});
}
else if(sscanf(buff,"glBegin GL_QUADS%c\n",&c)==1)
{
commands_gl.push_back([=](){glBegin(GL_QUADS);});
commands_mgl.push_back([=](){mglBegin(MGL_QUADS);});
}
else if(sscanf(buff,"glEnd%c\n",&c)==1)
{
commands_gl.push_back([=](){glEnd();});
commands_mgl.push_back([=](){mglEnd();});
}
else if(sscanf(buff,"glVertex2 %g %g\n",&f0,&f1)==2)
{
commands_gl.push_back([=](){glVertex2f(f0,f1);});
commands_mgl.push_back([=](){mglVertex2(f0,f1);});
}
else if(sscanf(buff,"glVertex3 %g %g %g\n",&f0,&f1,&f2)==3)
{
commands_gl.push_back([=](){glVertex3f(f0,f1,f2);});
commands_mgl.push_back([=](){mglVertex3(f0,f1,f2);});
}
else if(sscanf(buff,"glMatrixMode GL_MODELVIEW%c\n",&c)==1)
{
commands_gl.push_back([=](){glMatrixMode(GL_MODELVIEW);});
commands_mgl.push_back([=](){mglMatrixMode(MGL_MODELVIEW);});
}
else if(sscanf(buff,"glMatrixMode GL_PROJECTION%c\n",&c)==1)
{
commands_gl.push_back([=](){glMatrixMode(GL_PROJECTION);});
commands_mgl.push_back([=](){mglMatrixMode(MGL_PROJECTION);});
}
else if(sscanf(buff,"glPushMatrix%c\n",&c)==1)
{
commands_gl.push_back([=](){glPushMatrix();});
commands_mgl.push_back([=](){mglPushMatrix();});
}
else if(sscanf(buff,"glPopMatrix%c\n",&c)==1)
{
commands_gl.push_back([=](){glPopMatrix();});
commands_mgl.push_back([=](){mglPopMatrix();});
}
else if(sscanf(buff,"glLoadIdentity%c\n",&c)==1)
{
commands_gl.push_back([=](){glLoadIdentity();});
commands_mgl.push_back([=](){mglLoadIdentity();});
}
else if(sscanf(buff,"glLoadMatrix %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g\n",
&f0,&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&f11,&f12,&f13,&f14,&f15)==16)
{
commands_gl.push_back([=](){float f[16]={f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15};glLoadMatrixf(f);});
commands_mgl.push_back([=](){float f[16]={f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15};mglLoadMatrix(f);});
}
else if(sscanf(buff,"glMultMatrix %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g\n",
&f0,&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&f11,&f12,&f13,&f14,&f15)==16)
{
commands_gl.push_back([=](){float f[16]={f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15};glMultMatrixf(f);});
commands_mgl.push_back([=](){float f[16]={f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15};mglMultMatrix(f);});
}
else if(sscanf(buff,"glTranslate %g %g %g\n",&f0,&f1,&f2)==3)
{
commands_gl.push_back([=](){glTranslatef(f0,f1,f2);});
commands_mgl.push_back([=](){mglTranslate(f0,f1,f2);});
}
else if(sscanf(buff,"glRotate %g %g %g %g\n",&f0,&f1,&f2,&f3)==4)
{
commands_gl.push_back([=](){glRotatef(f0,f1,f2,f3);});
commands_mgl.push_back([=](){mglRotate(f0,f1,f2,f3);});
}
else if(sscanf(buff,"glScale %g %g %g\n",&f0,&f1,&f2)==3)
{
commands_gl.push_back([=](){glScalef(f0,f1,f2);});
commands_mgl.push_back([=](){mglScale(f0,f1,f2);});
}
else if(sscanf(buff,"glFrustum %g %g %g %g %g %g\n",&f0,&f1,&f2,&f3,&f4,&f5)==6)
{
commands_gl.push_back([=](){glFrustum(f0,f1,f2,f3,f4,f5);});
commands_mgl.push_back([=](){mglFrustum(f0,f1,f2,f3,f4,f5);});
}
else if(sscanf(buff,"glOrtho %g %g %g %g %g %g\n",&f0,&f1,&f2,&f3,&f4,&f5)==6)
{
commands_gl.push_back([=](){glOrtho(f0,f1,f2,f3,f4,f5);});
commands_mgl.push_back([=](){mglOrtho(f0,f1,f2,f3,f4,f5);});
}
else if(sscanf(buff,"glColor %g %g %g\n",&f0,&f1,&f2)==3)
{
commands_gl.push_back([=](){glColor3f(f0,f1,f2);});
commands_mgl.push_back([=](){mglColor(f0,f1,f2);});
}
else
{
int len=strlen(buff);
if(buff[len-1]=='\n') buff[len-1]=0;
printf("Unrecognized command: '%s'\n",buff);
exit(EXIT_FAILURE);
}
}
fclose(F);
}