C Sphere-Bug - Unvollständige Darstellung

Somtaaw

Lieutenant
Registriert
Jan. 2008
Beiträge
658
Hab leider einen kleinen Bug bei der Darstellung einer Sphäre, aber dazu am besten das Bild im Anhang betrachten... leider komme ich nicht drauf wo der Fehler steckt, da ich das Tutorial ned ganz verstanden haben. Ich hänge mal alle wichtigen Code-Elemente an... vielleicht weiß ja einer von euch wo sich der Teufel im Detail versteckt.

Sphäre erzeugen:
Code:
void CreateSphere (double R, double H, double K, double Z) 
{
	int n;
	double a;
	double b;

	n = 0;

	for( b = 0; b <= 90 - space; b+=space)
	{
		for( a = 0; a <= 360 - space; a+=space)
		{
			VERTEX[n].X = R * sin((a) / 180 * PI) * sin((b) / 180 * PI) - H;    
			VERTEX[n].Y = R * cos((a) / 180 * PI) * sin((b) / 180 * PI) + K;       
			VERTEX[n].Z = R * cos((b) / 180 * PI) - Z;     
			VERTEX[n].V = (2 * b) / 360;   
			VERTEX[n].U = (a) / 360;   
			n++;

			VERTEX[n].X = R * sin((a) / 180 * PI) * sin((b + space) / 180 * PI) - H;    
			VERTEX[n].Y = R * cos((a) / 180 * PI) * sin((b + space) / 180 * PI) + K;      
			VERTEX[n].Z = R * cos((b + space) / 180 * PI) - Z;      
			VERTEX[n].V = (2 * (b + space)) / 360;       
			VERTEX[n].U = (a) / 360;        
			n++;
            
			VERTEX[n].X = R * sin((a + space) / 180 * PI) * sin((b) / 180 * PI) - H;
			VERTEX[n].Y = R * cos((a + space) / 180 * PI) * sin((b) / 180 * PI) + K;
			VERTEX[n].Z = R * cos((b) / 180 * PI) - Z;  
			VERTEX[n].V = (2 * b) / 360;
			VERTEX[n].U = (a + space) / 360;   
			n++;
            
			VERTEX[n].X = R * sin((a + space) / 180 * PI) * sin((b + space) / 180 * PI) - H;
			VERTEX[n].Y = R * cos((a + space) / 180 * PI) * sin((b + space) / 180 * PI) + K;
			VERTEX[n].Z = R * cos((b + space) / 180 * PI) - Z;
			VERTEX[n].V = (2 * (b + space)) / 360;
			VERTEX[n].U = (a + space) / 360;
			n++;
		}
	}
}

Textur laden:
Code:
GLuint LoadTextureRAW( const char * filename )
{
	GLuint texture;
	int width, height;
	unsigned char * data;
	FILE * file;

	file = fopen( filename, "rb" );
	if ( file == NULL ) 
		return 0;
    
	width = 1024;
	height = 512;
	data = (unsigned char *)malloc( width * height * 3 );
	fread( data, width * height * 3, 1, file );
	fclose( file );

	glGenTextures( 1, &texture ); 
	glBindTexture( GL_TEXTURE_2D, texture );
	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); 
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
	gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );

	free( data );

	return texture;
}

Sphäre anzeigen:
Code:
void DisplaySphere (double R, GLuint texture)
{
	int b;
	glScalef (0.0125 * R, 0.0125 * R, 0.0125 * R);
	glRotatef (90, 1, 0, 0);
	glBindTexture (GL_TEXTURE_2D, texture);
	glBegin (GL_TRIANGLE_STRIP);

	for ( b = 0; b <= VertexCount; b++)
	{      
		glTexCoord2f (VERTEX[b].U, VERTEX[b].V);  
		glVertex3f (VERTEX[b].X, VERTEX[b].Y, -VERTEX[b].Z);  
	}
      
	for ( b = 0; b <= VertexCount; b++)
	{
		glTexCoord2f (VERTEX[b].U, -VERTEX[b].V);
		glVertex3f (VERTEX[b].X, VERTEX[b].Y, VERTEX[b].Z);
	}
	glEnd();
}
 

Anhänge

  • error.png
    error.png
    32,2 KB · Aufrufe: 170
Hmm, sieht so aus als wenn mindestens die Weltkoordinate des Südpols falsch gesetzt wird. Kann es sein, dass der Mittelpunkt der Kugel die Weltkoordinaten [-H,+K,-Z] hat und die falschen Vertices auf [0,0,0] gesetzt werden? Überprüf mal die for-Schleifen auf korrekte Abbruchbedingung.

/edit
Was ich vergaß: was macht dieses space? Und warum wird die v-Coord der Texture durch 360 geteilt. 2*(b + space) geht bis 180°
 
Zuletzt bearbeitet:
Danke für deine Hilfe... Das Problem wurde zum Glück mittlerweile gelöst (H,K,Z sind übrigends die Variablen für eine Translation gewesen, aber wurden jetzt gestrichten da sinnlos ;) )

Sollte es wen interessieren, die Lösung:

Sphere generieren:
Code:
void CreateSphere (double R) 
{
	int n;
	double a;
	double b;

	n = 0;

	for( b = 0; b <= 180; b+=space)
	{
		for( a = 0; a <= 360 - space; a+=space)
		{
			VERTEX[n].X = R * sin((a) / 180 * PI) * sin((b) / 180 * PI);    
			VERTEX[n].Y = R * cos((a) / 180 * PI) * sin((b) / 180 * PI);       
			VERTEX[n].Z = R * cos((b) / 180 * PI);     
			VERTEX[n].V = (2 * b) / 360;   
			VERTEX[n].U = (a) / 360;   
			n++;

			VERTEX[n].X = R * sin((a) / 180 * PI) * sin((b + space) / 180 * PI);    
			VERTEX[n].Y = R * cos((a) / 180 * PI) * sin((b + space) / 180 * PI);      
			VERTEX[n].Z = R * cos((b + space) / 180 * PI);      
			VERTEX[n].V = (2 * (b + space)) / 360;       
			VERTEX[n].U = (a) / 360;        
			n++;
            
			VERTEX[n].X = R * sin((a + space) / 180 * PI) * sin((b) / 180 * PI);
			VERTEX[n].Y = R * cos((a + space) / 180 * PI) * sin((b) / 180 * PI);
			VERTEX[n].Z = R * cos((b) / 180 * PI);  
			VERTEX[n].V = (2 * b) / 360;
			VERTEX[n].U = (a + space) / 360;   
			n++;
            
			VERTEX[n].X = R * sin((a + space) / 180 * PI) * sin((b + space) / 180 * PI);
			VERTEX[n].Y = R * cos((a + space) / 180 * PI) * sin((b + space) / 180 * PI);
			VERTEX[n].Z = R * cos((b + space) / 180 * PI);
			VERTEX[n].V = (2 * (b + space)) / 360;
			VERTEX[n].U = (a + space) / 360;
			n++;
		}
	}
}

Sphäre zeichnen:
Code:
void DisplaySphere (double R, GLuint texture)
{
	int b;
	glScalef (0.0125 * R, 0.0125 * R, 0.0125 * R);
	glRotatef (90.0f, 1.0f, 0.0f, 0.0f);
	glBindTexture (GL_TEXTURE_2D, texture);
	glBegin (GL_TRIANGLE_STRIP);

	for ( b = 0; b < VertexCount; b++)
	{      
		glTexCoord2f (VERTEX[b].U, VERTEX[b].V);  
		glVertex3f (VERTEX[b].X, VERTEX[b].Y, -VERTEX[b].Z);  
	}
	glEnd();
}

Und um vielleicht etwas Verwirrung zu entwirren:
Vorher wurde nur die halbe spähre kreiert und im draw verdoppelt und texturiert... um fehler auszuschließen erstell ich jetzt gleich die ganze späre (verbraucht zwar mehr speicher aber egal), und texuriere dann.. und beim Dwar gabs ein Problem mit dem b<=VertexCount.. wie man sieht ist das jetzt nur noch <, dadurch gibts keine Darstellungsporbleme ^^
 
Zurück
Oben