OpenGL Objekt laden verändert Lichtposition

üäpöol

Erfahrenes Mitglied
Hi,

ich hab ein ziemlich seltsames Problem mit OpenGL.
Ich habe eine Objektladefunktion geschrieben, die aber bei manchen Dateien dazu führt, dass die Position des Lichts, das an ganz anderer Stelle definiert ist verändert.

Hier mein Code:
C++:
void DrawScene(){
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glClearDepth(1.0);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);		
	glClearColor(0.75, 0.75, 0.75, 1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(0.0, 0.0, 0.0);

	// selection
	glInitNames();
	glPushName(0);
	glLoadName(0);

	// texture
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_TEXTURE);

	// light
	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION); 
	glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR); 
	glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT); 
	glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
	glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0);
	glEnable(GL_LIGHTING);

	SetLight(0, "ambient", 1.0, 1.0, 1.0, 1.0);
	SetLight(0, "diffuse", 1.0, 1.0, 1.0, 1.0);
	SetLight(0, "specular", 0.0, 0.0, 0.0, 0.0);
	SetLight(0, "position", 0.0, 6.0, 0.0, 1.0);

	glLightfv(GL_LIGHT0, GL_AMBIENT, light[0].ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light[0].diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, light[0].specular);
	glLightfv(GL_LIGHT0, GL_POSITION, light[0].position);
	glEnable(GL_LIGHT0);

	// light 1
	SetLight(1, "ambient", 1.0, 1.0, 1.0, 1.0);
	SetLight(1, "diffuse", 1.0, 1.0, 1.0, 1.0);
	SetLight(1, "specular", 0.0, 0.0, 0.0, 0.0);
	SetLight(1, "position", 0.0, 1000.0, -100000.0, 1.0);

	glLightfv(GL_LIGHT1, GL_AMBIENT, light[1].ambient);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, light[1].diffuse);
	glLightfv(GL_LIGHT1, GL_SPECULAR, light[1].specular);
	glLightfv(GL_LIGHT1, GL_POSITION, light[1].position);
	glEnable(GL_LIGHT1);

	// fog
	glEnable(GL_FOG);
	glFogi(GL_FOG_MODE, GL_LINEAR);
	glFogf(GL_FOG_START, fog_start);
	glFogf(GL_FOG_END, fog_end);
	glFogfv(GL_FOG_COLOR, fog_color);

	// point
	glColor3f(point_color[0], point_color[1], point_color[2]);
	glPointSize(8);
	glBegin(GL_POINTS);
		glVertex3f(0.0, 0.0, -0.2);
	glEnd();

	// position
	glColor3f(1.0, 1.0, 1.0);
	glRotatef(GLPDY, 0.0, 1.0, 0.0);
	glRotatef(GLPDZ, 1.0, 0.0, 0.0);
	glTranslatef(GLPX, -2.0, GLPZ-1.8);

	// drawing
	DrawGrass();
	DrawHut(); // <----------------------------------------------------- Fehler******

	

	// cleaning up
	glPopName();

	glFlush();
	SwapBuffers(hDC);

	glDisable(GL_TEXTURE);
	glDisable(GL_LIGHTING);
	glDisable(GL_FOG);
	glColor3f(1.0, 1.0, 1.0);


	texture_number = 0;
	GLPX_ = GLPX;
	GLPZ_ = GLPZ;

	ValidateRect(hWnd, NULL);
}

void DrawHut(){
	if(PositionCheck(-8.0, -112.0, 8.0, -130.0) == true){
		glDisable(GL_FOG);
		glDisable(GL_LIGHT0);
		glDisable(GL_LIGHT1);
		glDisable(GL_LIGHTING);
		glDisable(GL_COLOR_MATERIAL);

		if(allow_out == false){
			SetInfoText(3);
			if(call_time == 0) call_time = game_seconds;
			if(game_seconds-5 > call_time) SetInfoText(4);
		}
		else{

		}
	}

	// ---------------------------------------------------------------------------------------------------------------------------------
	LoadObject("objects/walls.obj", 0.0, 0.0, -120.0, 1.0, 1.0, 1.0, true, "textures/wood.bmp", 5, 5); // SORGT FÜR EINE DREHUNG DES LICHTS UM 90°!
	
	LoadObject("objects/ground.obj", 0.0, 0.0, -120.0, 1.0, 1.0, 1.0, true, "textures/ground.bmp", 10, 10); // funktioniert
	LoadObject("objects/roof.obj", 0.0, 0.0, -120.0, 1.0, 1.0, 1.0, true, "textures/wood.bmp", 10, 10); // funktioniert
	// LoadObject("objects/door.obj", -4.0, 0.1, -120+8.2+0.1, 1.0, 1.0, 1.0, true, "textures/door.bmp", 2, 3); // dasselbe Problem
	// LoadObject("objects/door.obj", -4.0, 0.1, -120+7.8-0.1, 1.0, 1.0, 1.0, true, "textures/door.bmp", 2, 3); // dasselbe Problem
}
void SetLight(int id, char* param, float val1, float val2, float val3, float val4){
	if(strcmp(param, "ambient") == 0){
		light[id].ambient[0] = val1;
		light[id].ambient[1] = val2;
		light[id].ambient[2] = val3;
		light[id].ambient[3] = val4;
	}
	else if(strcmp(param, "diffuse") == 0){
		light[id].diffuse[0] = val1;
		light[id].diffuse[1] = val2;
		light[id].diffuse[2] = val3;
		light[id].diffuse[3] = val4;
	}
	else if(strcmp(param, "specular") == 0){
		light[id].specular[0] = val1;
		light[id].specular[1] = val2;
		light[id].specular[2] = val3;
		light[id].specular[3] = val4;
	}
	else if(strcmp(param, "position") == 0){
		light[id].position[0] = val1;
		light[id].position[1] = val2;
		light[id].position[2] = val3;
		light[id].position[3] = val4;
	}
}

struct vertex_struct{
	GLfloat x;
	GLfloat y;
	GLfloat z;
};

struct normals_struct{
	GLfloat x;
	GLfloat y;
	GLfloat z;
};

struct faces_struct{
	int a, b, c, d, e;
};

int LoadObject(const char* filename, GLfloat X, GLfloat Y, GLfloat Z, GLfloat red, GLfloat green, GLfloat blue, bool texture, char* texture_filename, GLfloat texture_repeat_x, GLfloat texture_repeat_y){
	GLuint texture_buffer;
	AUX_RGBImageRec *texture_addr;

	int vertex_counter = 0;
	int normals_counter = 0;
	int face_counter = 0;

	vector<vertex_struct> vertex;
	vector<normals_struct> normals;
	vector<faces_struct> faces;

	vertex_struct vertex_buffer;
	normals_struct normals_buffer;
	faces_struct faces_buffer;

	fstream obj;
	obj.open(filename, ios::in);
	if(!obj){
		MessageBox(NULL, filename, "Object can't be loaded!", MB_OK | MB_ICONERROR);
	}

	vertex_buffer.x = 0;
	vertex_buffer.y = 0;
	vertex_buffer.z = 0;
	vertex.push_back(vertex_buffer);

	normals_buffer.x = 0;
	normals_buffer.y = 0;
	normals_buffer.z = 0;
	normals.push_back(normals_buffer);

	faces_buffer.a = 0;
	faces_buffer.b = 0;
	faces_buffer.c = 0;
	faces_buffer.d = 0;
	faces_buffer.e = 0;
	faces.push_back(faces_buffer);

	string line = " ";
	while(line != "\0"){
		getline(obj, line);
		if(line.c_str()[0] == 'v' && line.c_str()[1] == ' '){
			vertex_counter++;
			sscanf(line.c_str(), "v %f %f %f", &vertex_buffer.x, &vertex_buffer.y, &vertex_buffer.z);
			vertex.push_back(vertex_buffer);
		}
		else if(line.c_str()[0] == 'v' && line.c_str()[1] == 'n'){
			normals_counter++;
			sscanf(line.c_str(), "vn %f %f %f", &normals_buffer.x, &normals_buffer.y, &normals_buffer.z);
			normals.push_back(normals_buffer);
		}
		else if(line.c_str()[0] == 'f'){
			face_counter++;
			sscanf(line.c_str(), "f %i//%i %i//%i %i//%i %i//%i", &faces_buffer.a, &faces_buffer.e, &faces_buffer.b, &faces_buffer.e, &faces_buffer.c, &faces_buffer.e, &faces_buffer.d, &faces_buffer.e);
			faces.push_back(faces_buffer);
		}
	}

	if(texture == true){
		glEnable(GL_TEXTURE);
		LoadTexture(texture_filename, texture_addr, texture_buffer);
	}

	for(int i = 1; i <= face_counter; i++){
		glBegin(GL_QUADS);
			glColor3f(red, green, blue);
			if(faces[i].d <= normals_counter) glNormal3f(normals[faces[i].d].x, normals[faces[i].d].y, normals[faces[i].d].z);
			if(texture == true){
				glBindTexture(GL_TEXTURE_2D, texture_buffer);
			}

			if(texture == true) glTexCoord2f(0.0, 0.0);			
			glVertex3f(vertex[faces[i].a].x+X, vertex[faces[i].a].y+Y, vertex[faces[i].a].z+Z);

			if(texture == true) glTexCoord2f(texture_repeat_x, 0.0);
			glVertex3f(vertex[faces[i].b].x+X, vertex[faces[i].b].y+Y, vertex[faces[i].b].z+Z);

			if(texture == true) glTexCoord2f(texture_repeat_x, texture_repeat_y);
			glVertex3f(vertex[faces[i].c].x+X, vertex[faces[i].c].y+Y, vertex[faces[i].c].z+Z);

			if(texture == true) glTexCoord2f(0.0, texture_repeat_y);
			glVertex3f(vertex[faces[i].d].x+X, vertex[faces[i].d].y+Y, vertex[faces[i].d].z+Z);
		glEnd();
	}

	glDisable(GL_TEXTURE);

	obj.close();
	return 0;
}

Ich bin leider nicht wirklich ein OpenGL Profi, deshalb weiß ich nicht mal wo ich nach dem Problem suchen soll, weil alles für sich genommen funktioniert.

Hier noch die Objektdateien:
Code:
# Blender v2.58 (sub 1) OBJ File: 'walls.obj'
# www.blender.org
v -8.199989 0.000000 8.105000
v -8.200010 0.000000 -8.195000
v -7.800010 0.000000 -8.194999
v -7.799990 0.000000 8.105002
v -8.199989 5.000000 8.104996
v -8.200010 5.000000 -8.195004
v -7.800010 5.000000 -8.194997
v -7.799990 5.000000 8.105000
vn -0.000000 -1.000000 0.000000
vn 0.000000 1.000000 -0.000000
vn -1.000000 0.000000 0.000001
vn 0.000011 -0.000000 -1.000000
vn 1.000000 0.000000 -0.000001
vn -0.000008 0.000001 1.000000
usemtl Material
s off
f 1//1 2//1 3//1 4//1
f 5//2 8//2 7//2 6//2
f 1//3 5//3 6//3 2//3
f 2//4 6//4 7//4 3//4
f 3//5 7//5 8//5 4//5
f 5//6 1//6 4//6 8//6
v 7.800010 0.000000 8.000000
v 7.799990 0.000000 -8.000000
v 8.199989 0.000000 -7.999999
v 8.200010 0.000000 8.000003
v 7.800010 5.000000 7.999996
v 7.799990 5.000000 -8.000005
v 8.199990 5.000000 -7.999998
v 8.200010 5.000000 8.000000
vn 0.000010 -0.000000 -1.000000
usemtl Material
s off
f 9//1 10//1 11//1 12//1
f 13//2 16//2 15//2 14//2
f 9//3 13//3 14//3 10//3
f 10//7 14//7 15//7 11//7
f 11//5 15//5 16//5 12//5
f 13//6 9//6 12//6 16//6
v -7.800000 0.000000 -8.199995
v 8.200000 0.000000 -8.200006
v 8.199999 0.000000 -7.800005
v -7.800003 0.000000 -7.799995
v -7.799996 5.000000 -8.199995
v 8.200005 5.000000 -8.200005
v 8.199997 5.000000 -7.800005
v -7.800000 5.000000 -7.799995
vn -0.000001 0.000000 -1.000000
vn 1.000000 -0.000000 0.000011
vn 0.000001 -0.000000 1.000000
vn -1.000000 0.000001 -0.000008
usemtl Material
s off
f 17//1 18//1 19//1 20//1
f 21//2 24//2 23//2 22//2
f 17//8 21//8 22//8 18//8
f 18//9 22//9 23//9 19//9
f 19//10 23//10 24//10 20//10
f 21//11 17//11 20//11 24//11
v -7.800000 0.000000 7.900005
v 8.200000 0.000000 7.899995
v 8.199999 0.000000 8.099995
v -7.800003 0.000000 8.100005
v -7.799996 5.000000 7.900005
v 8.200005 5.000000 7.899995
v 8.199997 5.000000 8.099995
v -7.800000 5.000000 8.100005
vn 1.000000 -0.000000 0.000021
vn -1.000000 0.000001 -0.000017
usemtl Material
s off
f 25//1 26//1 27//1 28//1
f 29//2 32//2 31//2 30//2
f 25//8 29//8 30//8 26//8
f 26//12 30//12 31//12 27//12
f 27//10 31//10 32//10 28//10
f 29//13 25//13 28//13 32//13

Code:
# Blender v2.58 (sub 1) OBJ File: 'door.obj'
# www.blender.org
v -0.000000 0.000000 -0.089999
v 1.600000 0.000000 -0.090001
v 1.600000 0.000000 0.090000
v -0.000000 0.000000 0.090000
v 0.000000 3.000000 -0.090000
v 1.600001 3.000000 -0.090000
v 1.600000 3.000000 0.090000
v 0.000000 3.000000 0.090001
vn 0.000000 -1.000000 -0.000000
vn 0.000000 1.000000 0.000000
vn -0.000001 0.000000 -1.000000
vn 1.000000 -0.000000 0.000002
vn 0.000001 -0.000000 1.000000
vn -1.000000 0.000000 -0.000001
usemtl Material
s off
f 1//1 2//1 3//1 4//1
f 5//2 8//2 7//2 6//2
f 1//3 5//3 6//3 2//3
f 2//4 6//4 7//4 3//4
f 3//5 7//5 8//5 4//5
f 5//6 1//6 4//6 8//6

Code:
# Blender v2.58 (sub 1) OBJ File: 'ground.blend'
# www.blender.org
v 7.790000 0.010000 -7.789999
v 7.790000 0.010000 7.790000
v -7.790001 0.010000 7.789999
v -7.789997 0.010000 -7.790003
v 7.790004 0.410000 -7.789996
v 7.789995 0.410000 7.790005
v -7.790003 0.410000 7.789997
v -7.789999 0.410000 -7.790000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 0.000002 0.000000
vn -0.000000 -0.000004 1.000000
vn -1.000000 -0.000005 -0.000000
vn 0.000000 0.000008 -1.000000
usemtl Material
s off
f 1//1 2//1 3//1 4//1
f 5//2 8//2 7//2 6//2
f 1//3 5//3 6//3 2//3
f 2//4 6//4 7//4 3//4
f 3//5 7//5 8//5 4//5
f 5//6 1//6 4//6 8//6

Code:
# Blender v2.58 (sub 1) OBJ File: 'roof.blend'
# www.blender.org
v 8.500000 5.110000 -8.499999
v 8.500000 5.110000 8.500000
v -8.500001 5.110000 8.499998
v -8.499997 5.110000 -8.500003
v 8.500004 5.310000 -8.499995
v 8.499994 5.310000 8.500005
v -8.500003 5.310000 8.499997
v -8.499999 5.310000 -8.500000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 0.000005 0.000000
vn -0.000000 -0.000010 1.000000
vn -1.000000 -0.000010 -0.000000
vn 0.000000 0.000017 -1.000000
usemtl Material
s off
f 1//1 2//1 3//1 4//1
f 5//2 8//2 7//2 6//2
f 1//3 5//3 6//3 2//3
f 2//4 6//4 7//4 3//4
f 3//5 7//5 8//5 4//5
f 5//6 1//6 4//6 8//6

Danke im Voraus.
 
Zuletzt bearbeitet von einem Moderator:
Meinst du mit walls.obj und ohne? Ich glaube, dass man da kaum einen Unterschied erkennen wird. Ich habe zwei Lichter: Das erste ist die Grundhelligkeit und das zweite soll Licht (die Sonne) aus einer bestimmten Himmelsrichtung zeigen. Da in beiden Fällen die Objekte walls.obj nicht direkt beschienen werden kann man das so nicht sehen. Man merkt es nur daran, dass man sich mit walls.obj um 90° nach links drehen muss, um direkt das Licht zu bekommen und man ohne in der Startposition direkt beschienen wird.
 
Dazu zwei Punkte:
1. Sonnenlicht solltest du nicht durch ein Pointlight machen sondern durch ein Directional light. Die Sonne ist sozusagen unendlich weit weg und bescheint entsprechend alle Objekte aus der gleichen Richtung.

2. Man sollte eigentlich auf den anderen Objekten einen deutlichen Unterschied erkennen, wenn sich die Position des Lichts wirklich ändert. Hier ein Beispiel (beim Hügel im Hintergrund siehst dus am besten):

Licht kommt aus meiner Richtung:
50e9aca7802530_l1.jpg


Licht 90° verschoben:
50e9acb9053801_l2.jpg


Soweit sehe ich bei deinem Code nichts problematisches, daher vermute ich dass das Problem wo anders liegt. Einzige subtoptimalen Dinge sind wie gesagt dass du das Sonnenlicht nicht korrekt machst und dass du die Fixed function pipeline verwendest.
 

Neue Beiträge

Zurück