Fragen bzgl. Voxel Engine

Einstiegsfragen, Mathematik, Physik, künstliche Intelligenz, Engine Design
Benutzeravatar
mike7774
Beiträge: 22
Registriert: 06.01.2014, 00:00

Re: Fragen bzgl. Voxel Engine

Beitrag von mike7774 »

Warum unübersichtlich? ...
Weil ich dann die Kamera, Octree und Frustum Klasse posten müsste, und das wäre ne schlimme codewurst (ausserdem möchte ich den Code noch niemandem zumuten :mrgreen: )

Ja, mir ist schon klar wie das Culling funktioniert, und ich habs auch schon mehrfach überprüft, aber es scheint so als würde er das Frustum nicht korrekt erstellen, hier mal ein wenig Code wie das Frustum erstellt wird

Code: Alles auswählen

void Frustum::ConstructFrustum(float screenDepth, D3DXMATRIX matProj, D3DXMATRIX matView)
{
	float zMinimum;
	float r;
	D3DXMATRIX matrix;


	//D3DXVECTOR3 vDir = camera.vLookAt - camera.vPosition;
	D3DXMatrixLookAtLH( &matView, &camera.vPosition,&camera.vLookAt,&D3DXVECTOR3(0.0f,1.0f,0.0f));


	// Calculate the minimum Z distance in the frustrum
	zMinimum = -matProj._43 / matProj._33;
	r = screenDepth / (screenDepth - zMinimum);
	matProj._33 = r;
	matProj._43 = -r*zMinimum;


	// Create the frustrum matrix from the view matrix and update projection matrix
	D3DXMatrixMultiply(&matrix, &matView, &matProj);
//	D3DXMatrixInverse(&matrix,NULL,&matrix);

	// Calculate near plane of frustrum
	this->m_planes[0].a = matrix._14 + matrix._13;
	this->m_planes[0].b = matrix._24 + matrix._23;
	this->m_planes[0].c = matrix._34 + matrix._33;
	this->m_planes[0].d = matrix._44 + matrix._43;
	D3DXPlaneNormalize(&this->m_planes[0], &this->m_planes[0]);

	// Calculate far plane of frustrum
	this->m_planes[1].a = matrix._14 - matrix._13;
	this->m_planes[1].b = matrix._24 - matrix._23;
	this->m_planes[1].c = matrix._34 - matrix._33;
	this->m_planes[1].d = matrix._44 - matrix._43;
	D3DXPlaneNormalize(&this->m_planes[1], &this->m_planes[1]);

	// Calculate left plane of frustrum
	this->m_planes[2].a = matrix._14 + matrix._11;
	this->m_planes[2].b = matrix._24 + matrix._21;
	this->m_planes[2].c = matrix._34 + matrix._31;
	this->m_planes[2].d = matrix._44 + matrix._41;
	D3DXPlaneNormalize(&this->m_planes[2], &this->m_planes[2]);

	// Calculate right plane of frustrum
	this->m_planes[3].a = matrix._14 - matrix._11;
	this->m_planes[3].b = matrix._24 - matrix._21;
	this->m_planes[3].c = matrix._34 - matrix._31;
	this->m_planes[3].d = matrix._44 - matrix._41;
	D3DXPlaneNormalize(&this->m_planes[3], &this->m_planes[3]);

	// Calculate top plane of frustrum
	this->m_planes[4].a = matrix._14 - matrix._12;
	this->m_planes[4].b = matrix._24 - matrix._22;
	this->m_planes[4].c = matrix._34 - matrix._32;
	this->m_planes[4].d = matrix._44 - matrix._42;
	D3DXPlaneNormalize(&this->m_planes[4], &this->m_planes[4]);

	// Caclualte bottom plane of frustrum
	this->m_planes[5].a = matrix._14 + matrix._12;
	this->m_planes[5].b = matrix._24 + matrix._22;
	this->m_planes[5].c = matrix._34 + matrix._32;
	this->m_planes[5].d = matrix._44 + matrix._42;
	D3DXPlaneNormalize(&this->m_planes[5], &this->m_planes[5]);


	// Calculate Frustum Poins
	D3DXVECTOR3 dir,nc,fc,X,Y,Z;
	this->ratio = 1920.0f/1080.0f;
	this->angle = D3DX_PI/4;
	this->nearDistance = 0.5f;
	this->farDistance = 200.0f;

	// compute width and height of the near and far plane sections
	tang = (float)tan(ANG2RAD * angle * 0.5) ;
	nearHeight = nearDistance * tang;
	nearWidth = nearHeight * ratio;
	farHeight = farDistance  * tang;
	farWidth = farHeight * ratio;

	D3DXVECTOR3 vLookAt;
	D3DXVec3Normalize(&vLookAt, &camera.vLookAt);

	Z = camera.vPosition + vLookAt;
	D3DXVec3Normalize(&Z, &Z);

	D3DXVec3Cross(&X, &camera.vUp, &Z);
	D3DXVec3Normalize(&X, &X);
	D3DXVec3Cross(&Y, &Z, &X);

	// compute the centers of the near and far planes
	nc = camera.vPosition - Z * nearDistance;
	fc = camera.vPosition - Z * farDistance;

	// compute the 4 corners of the frustum on the near plane
	nearTopLeft = nc + Y * nearHeight - X * nearWidth;
	nearTopRight = nc + Y * nearHeight + X * nearWidth;
	nearBottomLeft = nc - Y * nearHeight - X * nearWidth;
	nearBottomRight = nc - Y * nearHeight + X * nearWidth;

	// compute the 4 corners of the frustum on the far plane
	farTopLeft = fc + Y * farHeight - X * farWidth;
	farTopRight = fc + Y * farHeight + X * farWidth;
	farBottomLeft = fc - Y * farHeight - X * farWidth;
	farBottomRight = fc - Y * farHeight + X * farWidth;


}
Und das Culling selbst überprüft einfach ob einer der Punkte im Frustum liegt bzw. bei größeren Objekten, das Frustum im Objekt.

Edit:
Und der Octree wird folgendermaßen geprüft:

Code: Alles auswählen

void ChunkOctree::getVisibleData(Chunk **ppData, DWORD &count)
{
	if(g_Frustum.CubeInFrustum(*m_MidPt,m_Radius) || g_Frustum.FrustumInCube(*m_MidPt,m_Radius))
	{
		for(int i=0;i<8;i++)
		{
			if(this->m_pChilds[i] != NULL)
				this->m_pChilds[i]->getVisibleData(ppData, count);
		}
		
		if(this->m_pChunkPtr != NULL)
		{
			ppData[count] = this->m_pChunkPtr;
			count++;
		}
	}
}
Benutzeravatar
exploid
Establishment
Beiträge: 146
Registriert: 21.08.2005, 18:33

Re: Fragen bzgl. Voxel Engine

Beitrag von exploid »

Hallo

Deine Fragen 1-3 werden ausführlich bei den folgenden links behandelt:

https://sites.google.com/site/letsmakeavoxelengine/home
https://www.sea-of-memes.com/LetsCode1/LetsCode1.html

In der Hoffnung etwas geholfen zu haben.

mfg
All your base are belong to us! Justice
Benutzeravatar
mike7774
Beiträge: 22
Registriert: 06.01.2014, 00:00

Re: Fragen bzgl. Voxel Engine

Beitrag von mike7774 »

Hallo

Deine Fragen 1-3 werden ausführlich bei den folgenden links behandelt:

https://sites.google.com/site/letsmakeavoxelengine/home
...
Hey, ja Let's make a Voxel Engine ist ne verdammt gute Seite, hat mir beim Start sehr geholfen.
Antworten