Sign in to follow this  
Direct

Help with simple skybox

Recommended Posts

I previously had some questions regarding this in the .net forum, but I think my problem at this point is now DirectX, so I'm continuing my questions here instead. I am using C#, though, as you'll see in the code following. Problem is I've hashed together a simple sky box from what I found as examples (mostly c++ examples) and other bits and pieces here and there. But, when I move around, the sky has a big black square or triangle (depending on which way is forward for me exactly) cut out of it...and it is always directly in front of the camera, havent' seen any anomalies elsewhere. Anybody have any suggestions on how to fix? Here's the current little render function that is pumping out the skybox...it isn't elegant but I'm just trying to do the job, if you will:
public bool render(Vector3 pos) {
	float wid = 2000.0f, hgt = 2000.0f, depth = 2000.0f;

	// Calculate half the width, height, and depth	
	float w = wid * 0.5f;
	float h = hgt * 0.5f;
	float d = depth * 0.5f;

	VertexBuffer vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), kMaxVerts, device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Managed);
	CustomVertex.PositionTextured[] v = new CustomVertex.PositionTextured[kMaxVerts];
	GraphicsStream gsv = vb.Lock(0, 0, 0);

	// Fill back
	v[0].X = pos.X + w; v[0].Y = pos.Y - h; v[0].Z = pos.Z - d; v[0].Tu = 1.0f; v[0].Tv = 1.0f;
	v[1].X = pos.X + w; v[1].Y = pos.Y + h; v[1].Z = pos.Z - d; v[1].Tu = 1.0f; v[1].Tv = 0.0f;
	v[2].X = pos.X - w; v[2].Y = pos.Y + h; v[2].Z = pos.Z - d; v[2].Tu = 0.0f; v[2].Tv = 0.0f;
	v[3].X = pos.X - w; v[3].Y = pos.Y - h; v[3].Z = pos.Z - d; v[3].Tu = 0.0f; v[3].Tv = 1.0f;

	// Fill front
	v[4].X = pos.X - w; v[4].Y = pos.Y - h; v[4].Z = pos.Z + d; v[4].Tu = 1.0f; v[4].Tv = 1.0f;
	v[5].X = pos.X - w; v[5].Y = pos.Y + h; v[5].Z = pos.Z + d; v[5].Tu = 1.0f; v[5].Tv = 0.0f;
	v[6].X = pos.X + w; v[6].Y = pos.Y + h; v[6].Z = pos.Z + d; v[6].Tu = 0.0f; v[6].Tv = 0.0f;
	v[7].X = pos.X + w; v[7].Y = pos.Y - h; v[7].Z = pos.Z + d; v[7].Tu = 0.0f; v[7].Tv = 1.0f;

	// Fill bottom
	v[8].X = pos.X - w; v[8].Y = pos.Y - h; v[8].Z = pos.Z - d; v[8].Tu = 1.0f; v[8].Tv = 1.0f;
	v[9].X = pos.X - w; v[9].Y = pos.Y - h; v[9].Z = pos.Z + d; v[9].Tu = 1.0f; v[9].Tv = 0.0f;
	v[10].X = pos.X + w; v[10].Y = pos.Y - h; v[10].Z = pos.Z + d; v[10].Tu = 0.0f; v[10].Tv = 0.0f;
	v[11].X = pos.X + w; v[11].Y = pos.Y - h; v[11].Z = pos.Z - d; v[11].Tu = 0.0f; v[11].Tv = 1.0f;

	// Fill top
	v[12].X = pos.X + w; v[12].Y = pos.Y + h; v[12].Z = pos.Z - d; v[12].Tu = 0.0f; v[12].Tv = 0.0f;
	v[13].X = pos.X + w; v[13].Y = pos.Y + h; v[13].Z = pos.Z + d; v[13].Tu = 0.0f; v[13].Tv = 1.0f;
	v[14].X = pos.X - w; v[14].Y = pos.Y + h; v[14].Z = pos.Z + d; v[14].Tu = 1.0f; v[14].Tv = 1.0f;
	v[15].X = pos.X - w; v[15].Y = pos.Y + h; v[15].Z = pos.Z - d; v[15].Tu = 1.0f; v[15].Tv = 0.0f;
	
	// Fill the left side
	v[16].X = pos.X - w; v[16].Y = pos.Y + h; v[16].Z = pos.Z - d; v[16].Tu = 1.0f; v[16].Tv = 0.0f;
	v[17].X = pos.X - w; v[17].Y = pos.Y + h; v[17].Z = pos.Z + d; v[17].Tu = 0.0f; v[17].Tv = 0.0f;
	v[18].X = pos.X - w; v[18].Y = pos.Y - h; v[18].Z = pos.Z + d; v[18].Tu = 0.0f; v[18].Tv = 1.0f;
	v[19].X = pos.X - w; v[19].Y = pos.Y - h; v[19].Z = pos.Z - d; v[19].Tu = 1.0f; v[19].Tv = 1.0f;
	
	// Fill the right side
	v[20].X = pos.X + w; v[20].Y = pos.Y - h; v[20].Z = pos.Z - d; v[20].Tu = 0.0f; v[20].Tv = 1.0f;
	v[21].X = pos.X + w; v[21].Y = pos.Y - h; v[21].Z = pos.Z + d; v[21].Tu = 1.0f; v[21].Tv = 1.0f;
	v[22].X = pos.X + w; v[22].Y = pos.Y + h; v[22].Z = pos.Z + d; v[22].Tu = 1.0f; v[22].Tv = 0.0f;
	v[23].X = pos.X + w; v[23].Y = pos.Y + h; v[23].Z = pos.Z - d; v[23].Tu = 0.0f; v[23].Tv = 0.0f;

	gsv.Write(v);
	vb.Unlock();

	const int kMaxIndices = 12 * 3;

	int[] indexList = new int[kMaxIndices] {0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 8, 9, 10, 10, 11, 8, 12, 13, 14, 14, 15, 12, 16, 17, 18, 18, 19, 16, 20, 21, 22, 22, 23, 20};
			
	IndexBuffer ib = new IndexBuffer(typeof(int), kMaxIndices, device, Usage.WriteOnly, Pool.Managed);
	GraphicsStream gsi = ib.Lock(0, 0, 0);
	gsi.Write(indexList);
	ib.Unlock();

	for(int i = 0, j = 0; i < kMaxTextures; ++i, j += 6) {
		// Select the texture for the current side
		device.SetTexture(0, tex[i]); 
		// Render it...
		device.SetStreamSource(0,vb,0);
		device.VertexFormat = CustomVertex.PositionTextured.Format;
		device.Indices = ib;
		device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, kMaxVerts, j, 2);

	}

	return true;
}






Share this post


Link to post
Share on other sites
two things,

First off, regarding your problem, you might have one primitive flipped (with back to you).

Try setting the CullMode to CullNone, and see if that makes it show. If so, look for the reversed order one.

Secondly, I see you're creating your VB every frame. Also, I don't see any release code (I think it's not needed in C# but I'm not sure).

At any rate, creating and locking a VB every frame would be quite costly, performance wise, in your app. As far as I can see, the data remains the same every frame, so you really should only create the VB and load the data once, and then just render it every frame.

If for some reason you do need to lock and modify the data every frame, I'd recommend you at least not create the VB every frame, but rather create it once and use it over and over.

Good luck :).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this