Skip to main content
gamedev.net
🔒 Locked

Not rendering partly visible objects

Started by DpakoH Nov 21, 2011 at 8:14 PM 9 replies 1.6k views
Original Post
DpakoH
DpakoH
Hello guys,

I need some help. I am rendering some textures in 3d and using bounding boxes to determine if they are visible for the camera.
I am attaching a pic so you can see the result
a5f95.png

the white lines are the bounding boxes, as you can see some of them are still party visible for the camera but no texture is rendered.
Any idea what might be the problem will be great, as I am getting nowhere.

The code below is part of my camera class.


public BoundingFrustum Frustrum
{
get
{
return new BoundingFrustum(ViewProjection);
}
}


public bool BoundingVolumeIsInView(BoundingBox box)
{
return (Frustrum.Contains(box) != ContainmentType.Disjoint);
}



Thank you in advance for the help
MJP
MJP
It's probably because you're using BoundingFrustum.Contains, and not BoundingFrustum.Intersects. Contains will tell you if the box is completely enclosed in the frustum, which doesn't work for boxes that are only partially enclosed.
DpakoH
DpakoH

It's probably because you're using BoundingFrustum.Contains, and not BoundingFrustum.Intersects. Contains will tell you if the box is completely enclosed in the frustum, which doesn't work for boxes that are only partially enclosed.

I also used Intersect - a little better but still not rendering everything right.
MJP
MJP
Hmm that's strange...are your bounding boxes flat (min Y == max Y), like you've depicted in your image? If so, that may be screwing up the intersection test.
DpakoH
DpakoH

Hmm that's strange...are your bounding boxes flat (min Y == max Y), like you've depicted in your image? If so, that may be screwing up the intersection test.


yes they are, i will change that to test if this is the problem and post here what happened.
Adaline
Adaline
Hello
Could you post your code about :
- the view frustum creation ?
- the frustum/AABB intersection test ?

At least your parameters if you use a library for this


(As mentionned before, assuming that your AABBs aren't defining a null volume)

wink.gif
MJP
MJP
He's using the XNA BoundingFrustum class
Adaline
Adaline
Yes I realized that a bit late smile.gif (that's why I added [color=#1C2837][size=2]At least your parameters if you use a library for this)

I suppose the error(s) is (are) at the level of the parameters that are provided to this class
Seeing how the involved objects are instanced/parametrized may help to figure out what's going on I hope

wink.gif
DpakoH
DpakoH
here is the camera class, it is from a physics engine called jiglibx (or something like that)




public class Camera : GameComponent
{
private Matrix view;
private Matrix projection;

private Vector3 position = new Vector3(0, 0, 0);
private Vector2 angles = Vector2.Zero;

private int widthOver2;
private int heightOver2;

private float fieldOfView = Microsoft.Xna.Framework.MathHelper.PiOver4;
private float aspectRatio;
private float nearPlaneDistance = 0.1f;
private float farPlaneDistance = 10000.0f;

private bool isCameraFree = true;
private KeyboardState currentKeyboardState;
private KeyboardState previousKeyboardState;

private MouseState prevMouseState = new MouseState();

/// <summary>
/// Initializes new camera component.
/// </summary>
/// <param name="game">Game to which attach this camera.</param>
public Camera(Game game)
: base(game)
{
widthOver2 = game.Window.ClientBounds.Width / 2;
heightOver2 = game.Window.ClientBounds.Height / 2;
aspectRatio = (float)game.Window.ClientBounds.Width / (float)game.Window.ClientBounds.Height;
UpdateProjection();
Mouse.SetPosition(widthOver2, heightOver2);

currentKeyboardState = Keyboard.GetState();
}

/// <summary>
/// Gets camera view matrix.
/// </summary>
public Matrix View { get { return view; } }
/// <summary>
/// Gets or sets camera projection matrix.
/// </summary>
public Matrix Projection { get { return projection; } set { projection = value; } }
/// <summary>
/// Gets camera view matrix multiplied by projection matrix.
/// </summary>
public Matrix ViewProjection { get { return view * projection; } }

/// <summary>
/// Gets or sets camera position.
/// </summary>
public Vector3 Position { get { return position; } set { position = value; } }

/// <summary>
/// Gets or sets camera field of view.
/// </summary>
public float FieldOfView { get { return fieldOfView; } set { fieldOfView = value; UpdateProjection(); } }
/// <summary>
/// Gets or sets camera aspect ratio.
/// </summary>
public float AspectRatio { get { return aspectRatio; } set { aspectRatio = value; UpdateProjection(); } }
/// <summary>
/// Gets or sets camera near plane distance.
/// </summary>
public float NearPlaneDistance { get { return nearPlaneDistance; } set { nearPlaneDistance = value; UpdateProjection(); } }
/// <summary>
/// Gets or sets camera far plane distance.
/// </summary>
public float FarPlaneDistance { get { return farPlaneDistance; } set { farPlaneDistance = value; UpdateProjection(); } }

/// <summary>
/// Gets or sets camera's target.
/// </summary>
public Vector3 Target
{
get
{
Matrix cameraRotation = Matrix.CreateRotationX(angles.X) * Matrix.CreateRotationY(angles.Y);
return position + Vector3.Transform(Vector3.Forward, cameraRotation);
}
set
{
Vector3 forward = Vector3.Normalize(position - value);
Vector3 right = Vector3.Normalize(Vector3.Cross(forward, Vector3.Up));
Vector3 up = Vector3.Normalize(Vector3.Cross(right, forward));

Matrix test = Matrix.Identity;
test.Forward = forward;
test.Right = right;
test.Up = up;
angles.X = -(float)Math.Asin(test.M32);
angles.Y = -(float)Math.Asin(test.M13);
}
}

/// <summary>
/// Updates camera with input and updates view matrix.
/// </summary>
/// <param name="gameTime"></param>
public override void Update(GameTime gameTime)
{
if (Enabled)
{
double elapsedTime = (double)gameTime.ElapsedGameTime.Ticks / (double)TimeSpan.TicksPerSecond;
ProcessInput((float)elapsedTime * 50.0f);
UpdateView();

base.Update(gameTime);
}
}

private void ProcessInput(float amountOfMovement)
{
Vector3 moveVector = new Vector3();

previousKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();

if (currentKeyboardState.IsKeyDown(Keys.D))
moveVector.X += amountOfMovement;
if (currentKeyboardState.IsKeyDown(Keys.A))
moveVector.X -= amountOfMovement;
if (currentKeyboardState.IsKeyDown(Keys.S))
moveVector.Z += amountOfMovement;
if (currentKeyboardState.IsKeyDown(Keys.W))
moveVector.Z -= amountOfMovement;

if (currentKeyboardState.IsKeyDown(Keys.F) && previousKeyboardState.IsKeyUp(Keys.F))
{
isCameraFree = !isCameraFree;
}

Matrix cameraRotation = Matrix.CreateRotationX(angles.X) * Matrix.CreateRotationY(angles.Y);
position += Vector3.Transform(moveVector, cameraRotation);

MouseState currentMouseState = Mouse.GetState();

if (currentMouseState.RightButton == ButtonState.Pressed && prevMouseState.RightButton == ButtonState.Released)
{

Mouse.SetPosition(widthOver2, heightOver2);
}
else if (currentMouseState.RightButton == ButtonState.Pressed || isCameraFree)
{
if (currentMouseState.X != widthOver2)
angles.Y -= amountOfMovement / 80.0f * (currentMouseState.X - widthOver2);
if (currentMouseState.Y != heightOver2)
angles.X -= amountOfMovement / 80.0f * (currentMouseState.Y - heightOver2);

if (angles.X > 1.4) angles.X = 1.4f;
if (angles.X < -1.4) angles.X = -1.4f;
if (angles.Y > Math.PI) angles.Y -= 2 * (float)Math.PI;
if (angles.Y < -Math.PI) angles.Y += 2 * (float)Math.PI;

Mouse.SetPosition(widthOver2, heightOver2);
}

prevMouseState = currentMouseState;
}

private void UpdateProjection()
{
projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance);
}

private void UpdateView()
{
Matrix cameraRotation = Matrix.CreateRotationX(angles.X) * Matrix.CreateRotationY(angles.Y);
Vector3 targetPos = position + Vector3.Transform(Vector3.Forward, cameraRotation);

Vector3 upVector = Vector3.Transform(Vector3.Up, cameraRotation);

view = Matrix.CreateLookAt(position, targetPos, upVector);
}
}



this is not the last version but apparently i haven't committed last changes. but the only parts the class is missing are those that i have shown in my first post. (getting [color=#660066][font=CourierNew, monospace][size=2]BoundingFrustum and checking if BoundinBox intersects with it)[/font]
DpakoH
DpakoH
sorry for wasting your time guys although boundingboxes look fine, i was constructing them in the wrong way. so problem is solved.
again sorry.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.