-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpolyrobo.c
More file actions
147 lines (120 loc) · 4.86 KB
/
Copy pathpolyrobo.c
File metadata and controls
147 lines (120 loc) · 4.86 KB
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
// polyrobo.c: big fancy robot example
// owo
// TODO: make this load polyrobo.mdl directly!
#include <float.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <plush/plush.h>
#include "ex.h"
pl_Light *light;
pl_Obj *polyrobo;
pl_Mat *materials[7];
pl_Cam *camera;
uint8_t framebuffer[W * H];
float zbuffer[W * H];
uint8_t palette[768];
size_t num_materials = 0;
int main(int argc, char **argv)
{
size_t i, n;
/* setup graphics mode */
exSetGraphics();
/* fallback material */
materials[0] = plMatCreate();
/* load polyrobo model */
polyrobo = plObjCreate(NULL);
polyrobo->Model = plReadWavefrontMdlEx("polyrobo.obj", &materials[1], 6, &n, materials[0]);
/* override material setups */
materials[1]->Ambient[0] = materials[1]->Ambient[1] = materials[1]->Ambient[2] = -128;
materials[1]->Specular[0] = materials[1]->Specular[1] = materials[1]->Specular[2] = 64;
materials[1]->Diffuse[0] = materials[1]->Diffuse[1] = materials[1]->Diffuse[2] = 128;
materials[1]->NumGradients = 2500;
materials[1]->ShadeType = PL_SHADE_GOURAUD;
materials[1]->Texture = plReadPCXTex("black.pcx", true, true);
materials[1]->PerspectiveCorrect = 8;
materials[2]->Ambient[0] = materials[2]->Ambient[1] = materials[2]->Ambient[2] = -128;
materials[2]->Specular[0] = materials[2]->Specular[1] = materials[2]->Specular[2] = 64;
materials[2]->Diffuse[0] = materials[2]->Diffuse[1] = materials[2]->Diffuse[2] = 128;
materials[2]->NumGradients = 2500;
materials[2]->ShadeType = PL_SHADE_GOURAUD;
materials[2]->Texture = plReadPCXTex("black.pcx", true, true);
materials[2]->Environment = plReadPCXTex("chrome_grayr.pcx", true, true);
materials[2]->TexEnvMode = PL_TEXENV_MAX;
materials[3]->Ambient[0] = materials[3]->Ambient[3] = materials[3]->Ambient[2] = -128;
materials[3]->Specular[0] = materials[3]->Specular[3] = materials[3]->Specular[2] = 64;
materials[3]->Diffuse[0] = materials[3]->Diffuse[3] = materials[3]->Diffuse[2] = 128;
materials[3]->NumGradients = 2500;
materials[3]->ShadeType = PL_SHADE_GOURAUD;
materials[3]->Texture = plReadPCXTex("black.pcx", true, true);
materials[3]->Environment = plReadPCXTex("chrome_steel1.pcx", true, true);
materials[3]->TexEnvMode = PL_TEXENV_MAX;
materials[4]->Ambient[0] = materials[4]->Ambient[1] = materials[4]->Ambient[2] = -128;
materials[4]->Specular[0] = materials[4]->Specular[1] = materials[4]->Specular[2] = 64;
materials[4]->Diffuse[0] = materials[4]->Diffuse[1] = materials[4]->Diffuse[2] = 128;
materials[4]->NumGradients = 2500;
materials[4]->ShadeType = PL_SHADE_GOURAUD;
materials[4]->Texture = plReadPCXTex("droid_legmap.pcx", true, true);
materials[4]->PerspectiveCorrect = 8;
materials[5]->Ambient[0] = materials[5]->Ambient[1] = materials[5]->Ambient[2] = -128;
materials[5]->Specular[0] = materials[5]->Specular[1] = materials[5]->Specular[2] = 64;
materials[5]->Diffuse[0] = materials[5]->Diffuse[1] = materials[5]->Diffuse[2] = 128;
materials[5]->NumGradients = 2500;
materials[5]->ShadeType = PL_SHADE_GOURAUD;
materials[5]->Texture = plReadPCXTex("ears.pcx", true, true);
materials[5]->PerspectiveCorrect = 8;
materials[6]->Ambient[0] = materials[6]->Ambient[1] = materials[6]->Ambient[2] = 0;
materials[6]->Specular[0] = materials[6]->Specular[1] = materials[6]->Specular[2] = 64;
materials[6]->Diffuse[0] = materials[6]->Diffuse[1] = materials[6]->Diffuse[2] = 128;
materials[6]->NumGradients = 2500;
materials[6]->ShadeType = PL_SHADE_NONE;
materials[6]->Texture = plReadPCXTex("eye.pcx", true, true);
materials[6]->PerspectiveCorrect = 8;
/* initialize materials */
num_materials = n + 1;
for (i = 0; i < num_materials; i++)
plMatInit(materials[i]);
/* create palette */
plMatMakeOptPal(palette, 1, 255, materials, num_materials);
palette[0] = palette[1] = palette[2] = 0;
for (i = 0; i < num_materials; i++)
plMatMapToPal(materials[i], palette, 0, 255);
exSetPalette(palette);
/* create camera */
camera = plCamCreate(W, H, W * 3.0 / (H * 4.0), 90.0, framebuffer, zbuffer);
camera->Y += 96;
camera->Z -= 512;
/* create light */
light = plLightSet(plLightCreate(), PL_LIGHT_VECTOR, 0.0, 0.0, 0.0, 1.0, 1.0);
/* main loop */
while (!exGetKey())
{
/* rotate model */
polyrobo->Ya += 1.0;
/* clear back buffer */
memset(zbuffer, 0, sizeof(zbuffer));
memset(framebuffer, 0, sizeof(framebuffer));
/* render frame */
plRenderBegin(camera);
plRenderLight(light);
plRenderObj(polyrobo);
plRenderEnd();
/* wait for vsync, then copy to screen */
exWaitVSync();
memcpy(exGraphMem, framebuffer, sizeof(framebuffer));
}
/* clean up */
plCamDelete(camera);
plLightDelete(light);
plMdlDelete(polyrobo->Model);
plObjDelete(polyrobo);
for (i = 0; i < num_materials; i++)
{
if (materials[i]->Texture) plTexDelete(materials[i]->Texture);
if (materials[i]->Environment) plTexDelete(materials[i]->Environment);
plMatDelete(materials[i]);
}
/* shut down video */
exSetText();
return 0;
}