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
|
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <neofx/math.h>
#include <zoom/level.h>
#include <zoom/light.h>
#include <zoom/player.h>
#include <zoom/render.h>
extern PLAYER player;
extern LEVEL *level;
extern GLuint sphere;
extern int nLights;
extern LIGHT *lights;
extern COLOR ambient;
extern GLuint lightmap;
static int SortByTex(const void *p1, const void *p2) {
return ((POLYGON*)p1)->texture - ((POLYGON*)p2)->texture;
}
void Render() {
//GLfloat std_emission[] ={{0.0, 0.0, 0.0, 1.0};
MATRIX rotate = {.f = {
player.rotycos, 0.0, -player.rotysin, 0.0,
0.0, 1.0, 0.0, 0.0,
player.rotysin, 0.0, player.rotycos, 0.0,
0.0, 0.0, 0.0, 1.0
}};
POLYGON_LIST *room;
int i, j, k;
float d;
VECTOR v, v1, v2;
VERTEX p;
COLOR c;
GLint last_tex;
glLoadIdentity();
glRotatef(player.rotx, 1.0, 0.0, 0.0);
glMultMatrixf(rotate.f);
glTranslatef(-player.pos.x, -player.pos.y, -player.pos.z);
room = DrawRoom(level, player.room);
qsort(room->polygons, room->nPolygons, sizeof(POLYGON), SortByTex);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_ONE, GL_ZERO);
glColor3fv((GLfloat*)&ambient);
glActiveTexture(GL_TEXTURE1);
glDisable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, room->polygons[0].texture);
last_tex = room->polygons[0].texture;
glBegin(GL_TRIANGLES);
for(i = 0; i < room->nPolygons; i++) {
if(last_tex != room->polygons[i].texture) {
glEnd();
glBindTexture(GL_TEXTURE_2D, room->polygons[i].texture);
last_tex = room->polygons[i].texture;
glBegin(GL_TRIANGLES);
}
glMultiTexCoord2fv(0, (GLfloat*)&room->polygons[i].texcoords[0]);
glVertex3fv((GLfloat*)&room->polygons[i].vertices[0]);
glMultiTexCoord2fv(0, (GLfloat*)&room->polygons[i].texcoords[1]);
glVertex3fv((GLfloat*)&room->polygons[i].vertices[1]);
glMultiTexCoord2fv(0, (GLfloat*)&room->polygons[i].texcoords[2]);
glVertex3fv((GLfloat*)&room->polygons[i].vertices[2]);
}
glEnd();
glDepthFunc(GL_EQUAL);
glDepthMask(GL_FALSE);
glBlendFunc(GL_ONE, GL_ONE);
glColor3f(1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, lightmap);
glActiveTexture(GL_TEXTURE1);
for(i = 0; i < nLights; i++) {
if(room->polygons[0].texture) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, room->polygons[0].texture);
}
else
glDisable(GL_TEXTURE_2D);
last_tex = room->polygons[0].texture;
glBegin(GL_TRIANGLES);
for(j = 0; j < room->nPolygons; j++) {
if(last_tex != room->polygons[j].texture) {
glEnd();
glBindTexture(GL_TEXTURE_2D, room->polygons[j].texture);
last_tex = room->polygons[j].texture;
glBegin(GL_TRIANGLES);
}
d = VectorDot(room->polygons[j].normal, VectorSub(room->polygons[j].vertices[0], lights[i].pos));
if(d < 0) {
c = VectorMul(lights[i].diffuse, 1.0/(d*d));
glColor3fv((GLfloat*)&c);
v1 = VectorNormalize(VectorSub(room->polygons[j].vertices[0], room->polygons[j].vertices[1]));
for(k = 0; k < 3; k++) {
p = VectorAdd(lights[i].pos, VectorMul(room->polygons[j].normals[k], d));
v = VectorSub(room->polygons[j].vertices[k], p);
v2 = VectorCross(v1, room->polygons[j].normals[k]);
glMultiTexCoord2f(0, VectorDot(v, v1)*0.04 / d + 0.5, VectorDot(v, v2)*0.04 / d + 0.5);
glMultiTexCoord2fv(1, (GLfloat*)&room->polygons[j].texcoords[k]);
glVertex3fv((GLfloat*)&room->polygons[j].vertices[k]);
}
}
}
glEnd();
}
free(room);
/*glPushMatrix();
glDisable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 1.0);
glTranslatef(lights[0].pos.x, lights[0].pos.y, lights[0].pos.z);
glScalef(0.1, 0.1, 0.1);
glCallList(sphere);
glEnable(GL_TEXTURE_2D);
glPopMatrix();*/
glFlush();
}
|