Render-Funktion optimiert; Fehler in der Beleuchtungsfunktion behoben

This commit is contained in:
neoraider 2006-11-22 22:33:04 +00:00
parent e05e8eb046
commit 671a686311
2 changed files with 34 additions and 13 deletions

2
init.c
View file

@ -15,7 +15,7 @@ GLuint lightmap;
extern TEXLIST *texlist; extern TEXLIST *texlist;
int InitGame() { int InitGame() {
LIGHT light = {LIGHT_POINT, {30.0, 30.0, 30.0}, {0.0, 0.0, 0.0}}; LIGHT light = {LIGHT_POINT, {15.0, 15.0, 15.0}, {0.0, 0.0, 0.0}};
COLOR ambient = {0.1, 0.1, 0.1}; COLOR ambient = {0.1, 0.1, 0.1};

View file

@ -18,6 +18,10 @@ extern COLOR ambient;
extern GLuint lightmap; extern GLuint lightmap;
static int SortByTex(const void *p1, const void *p2) {
return ((POLYGON*)p1)->texture - ((POLYGON*)p2)->texture;
}
void Render() { void Render() {
GLfloat std_emission[] = {0.0, 0.0, 0.0, 1.0}; GLfloat std_emission[] = {0.0, 0.0, 0.0, 1.0};
MATRIX rotate = { MATRIX rotate = {
@ -32,6 +36,7 @@ void Render() {
VECTOR v, v1, v2; VECTOR v, v1, v2;
VERTEX p; VERTEX p;
COLOR c; COLOR c;
GLint last_tex;
glLoadIdentity(); glLoadIdentity();
@ -43,6 +48,7 @@ void Render() {
glTranslatef(-player.pos.x, -player.pos.y, -player.pos.z); glTranslatef(-player.pos.x, -player.pos.y, -player.pos.z);
room = DrawRoom(&level, player.room); room = DrawRoom(&level, player.room);
qsort(room->polygons, room->nPolygons, sizeof(POLYGON), SortByTex);
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
@ -57,10 +63,17 @@ void Render() {
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0); 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++) { for(i = 0; i < room->nPolygons; i++) {
glBindTexture(GL_TEXTURE_2D, room->polygons[i].texture); if(last_tex != room->polygons[i].texture) {
glEnd();
glBegin(GL_TRIANGLES); 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]); glMultiTexCoord2fv(0, (GLfloat*)&room->polygons[i].texcoords[0]);
glVertex3fv((GLfloat*)&room->polygons[i].vertices[0]); glVertex3fv((GLfloat*)&room->polygons[i].vertices[0]);
@ -69,9 +82,11 @@ void Render() {
glMultiTexCoord2fv(0, (GLfloat*)&room->polygons[i].texcoords[2]); glMultiTexCoord2fv(0, (GLfloat*)&room->polygons[i].texcoords[2]);
glVertex3fv((GLfloat*)&room->polygons[i].vertices[2]); glVertex3fv((GLfloat*)&room->polygons[i].vertices[2]);
glEnd();
} }
glEnd();
glDepthFunc(GL_EQUAL); glDepthFunc(GL_EQUAL);
glDepthMask(GL_FALSE); glDepthMask(GL_FALSE);
@ -84,15 +99,23 @@ void Render() {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
for(i = 0; i < nLights; i++) { for(i = 0; i < nLights; i++) {
glBindTexture(GL_TEXTURE_2D, room->polygons[0].texture);
last_tex = room->polygons[0].texture;
glBegin(GL_TRIANGLES);
for(j = 0; j < room->nPolygons; j++) { for(j = 0; j < room->nPolygons; j++) {
glBindTexture(GL_TEXTURE_2D, room->polygons[j].texture); if(last_tex != room->polygons[j].texture) {
glEnd();
glBegin(GL_TRIANGLES); 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)); d = VectorDot(room->polygons[j].normal, VectorSub(room->polygons[j].vertices[0], lights[i].pos));
if(d < 0) { if(d < 0) {
c = VectorMul(lights[i].diffuse, 1.0/((nLights+1)*d*d)); c = VectorMul(lights[i].diffuse, 1.0/(d*d));
glColor3fv((GLfloat*)&c); glColor3fv((GLfloat*)&c);
p = VectorAdd(lights[i].pos, VectorMul(room->polygons[j].normal, d)); p = VectorAdd(lights[i].pos, VectorMul(room->polygons[j].normal, d));
@ -108,15 +131,13 @@ void Render() {
glVertex3fv((GLfloat*)&room->polygons[j].vertices[k]); glVertex3fv((GLfloat*)&room->polygons[j].vertices[k]);
} }
} }
glEnd();
} }
glEnd();
} }
free(room); free(room);
//ApplyLightScale();
/*glPushMatrix(); /*glPushMatrix();
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 1.0); glColor3f(1.0, 1.0, 1.0);