-
Notifications
You must be signed in to change notification settings - Fork 2
/
RayTracer.g
executable file
·198 lines (184 loc) · 5.51 KB
/
RayTracer.g
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
grammar RayTracer;
@members {
public Vector <double[]> spheres = new Vector <double []>();
public double [] ambientLight = new double [3];
public Vector <double []> ligths = new Vector <double[]>();
public String camera="";
public String num1="", num2="", num3="", num0="";
public String plane= "";
public String color= "";
public int typeObject = 0;
public boolean flag;
public static final int typePlane = 1;
public static final int typeSphere = 2;
public static final int typeCamera = 3;
public static final int typeLigth = 4;
public static final int typeAmbientalL=5;
}
scene : ambient_light object+;
object : light | camera | (sphere|plane);
light : {typeObject=typeLigth;}
LIGHTSOURCE OPEN_BRACKET coordenate3D COLOR color CLOSE_BRACKET
/*{ligth+= "luz = " + $coordenate3D.text + ", " + $color.text + ";";
System.out.println (ligth);}*/;
camera : {camera += "camara = " + "position ";}
CAMERA OPEN_BRACKET LOCATION coordenate=coordenate3D
{camera += $coordenate.text + ", lookat ";
}
LOOK_AT coordenate2=coordenate3D {
camera +=$coordenate2.text + ";";
System.out.println(camera);} CLOSE_BRACKET;
sphere : {typeObject=typeSphere;
flag=false;}
SPHERE OPEN_BRACKET coordenate3D
/*{sphere+= $coordenate3D.text+ ", ";}*/
COMMA num0=number
pigment 'finish' OPEN_BRACKET (constantLights)? 'reflection' OPEN_BRACKET num1=number
CLOSE_BRACKET CLOSE_BRACKET CLOSE_BRACKET
{
double [] tempRadio= new double [2];
tempRadio [0] = Double.valueOf($num0.text).doubleValue();
tempRadio[1]=Double.valueOf($num1.text).doubleValue();
spheres.add (tempRadio);
if(!flag){
double [] temp = new double [4];
temp [0]=0.1;
temp [1]=0.7;
temp [2]= 0.7;
temp [3] = 20.0;
spheres.add(temp);}
};
plane : {typeObject=typePlane;
plane+= "plano = ";}
PLANE OPEN_BRACKET coordenate3D
{plane+= $coordenate3D.text+ ", ";}
COMMA number
{plane+= $number.text + ", ";}
pigment (constantLights)? CLOSE_BRACKET
{System.out.println(plane);
plane="";};
pigment : PIGMENT OPEN_BRACKET COLOR color CLOSE_BRACKET
{color+= $color.text;
double [] color1 = new double [3];
// Para determinar el color
if (color.equals("Yellow"))
{
color1 [0] = 1.0; color1 [1] =1.0; color1 [2] = 0.0;
} else if (color.equals("Blue"))
{color1 [0] = 0.0; color1 [1] =0.0; color1 [2] = 1.0;
} else if (color.equals("Red"))
{ color1 [0] = 1.0; color1 [1] =0.0; color1 [2] = 0.0;
} else if (color.equals("Green"))
{color1 [0] = 0.0; color1 [1] =1.0; color1 [2] = 0.0;
} else if (color.equals("White"))
{color1 [0] = 1.0; color1 [1] =1.0; color1 [2] = 1.0;}
color = "";
// Para saber a que vector se agrega
switch (typeObject){
case 1:
break;
case 2:
spheres.add(color1);
case 3:
break;
case 4:
break;
default:
break;} };
coordenate3D
: LESS_THAN num1=number COMMA num2=number COMMA num3=number GREATER_THAN
{ double [] temp = new double [3];
temp [0]=Double.valueOf($num1.text).doubleValue();
temp [1]= Double.valueOf($num2.text).doubleValue();
temp [2] = Double.valueOf($num3.text).doubleValue();
switch (typeObject){
case 1:
break;
case 2:
spheres.add(temp);
break;
case 3:
break;
case 4:
ligths.add(temp);
break;
case 5:
break;}
};
number : ('-')? DIGIT ('.')? DIGIT*;
color : TYPE_COLOR
{color+= $TYPE_COLOR.text;
double [] color1 = new double [3];
if (color.equals("Yellow")) {
color1 [0] = 1.0; color1 [1] =1.0; color1 [2] = 0.0;
} else if (color.equals("Blue"))
{color1 [0] = 0.0; color1 [1] =0.0; color1 [2] = 1.0;
} else if (color.equals("Red")) { color1 [0] = 1.0; color1 [1] =0.0; color1 [2] = 0.0; } else if (color.equals("Green"))
{color1 [0] = 0.0; color1 [1] =1.0; color1 [2] = 0.0;
} else if (color.equals("White"))
{color1 [0] = 1.0; color1 [1] =1.0; color1 [2] = 1.0;}
color = "";
switch (typeObject){
case 4:
ligths.add(color1);
break;
case 5:
for (int i=0; i<3; i++){
ambientLight[i]=color1[i];}
break;
default:
break;}};
ambient_light :{typeObject=typeAmbientalL;}
GLOBAL OPEN_BRACKET AMBIENTLIGHT COLOR color CLOSE_BRACKET ; // Para determinar el color
constantLights
:{flag=true;}
'ambient' num0=number 'diffuse' num1=number 'phong' num2=number 'phong_size' num3=number
{
double [] temp = new double [4];
temp [0]=Double.valueOf($num0.text).doubleValue();
temp [1]= Double.valueOf($num1.text).doubleValue();
temp [2] = Double.valueOf($num2.text).doubleValue();
temp [3]=Double.valueOf($num3.text).doubleValue();
switch (typeObject)
{
case 1:
break;
case 2:
spheres.add(temp);
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
} ;
AMBIENTLIGHT
: 'ambient_light';
GLOBAL
: 'global_settings';
LIGHTSOURCE : 'light_source';
CAMERA : 'camera';
LOCATION: 'location';
PIGMENT : 'pigment';
PLANE : 'plane';
SPHERE : 'sphere';
LOOK_AT : 'look_at';
COLOR : 'color';
TYPE_COLOR
: 'Yellow'
|'Blue'
|'Red'
|'Green'
|'White' ;
OPEN_BRACKET: '{';
CLOSE_BRACKET: '}';
LESS_THAN
: '<';
GREATER_THAN
: '>';
COMMA : ',';
DIGIT : '0'..'9'+;
NEWLINE : '\r'? '\n' {skip();};
WHITESPACE: (' '|'\t')+ {skip();} ;