Skip navigation

Rendering a skybox in xna

apparently you CANNOT change textures while in between BasicEffect.Begin() and BasicEffect.End().

What SEEMS to happen is you actually end up “queuing” a change to another texture, but the change doesn’t happen until you call .End()

Example: I was trying to render a skybox. The position of the textures kept coming out ALL wrong. After a long time spent looking for a bug in the texture load or vertices code, I found out that it was just the call to change the texture was in the wrong place.

Wrong way:

    for( int i = 0; i < 6 ; i++ )
    {

      basicEffect.Begin();
      basicEffect.CurrentTechnique.Passes[ 0 ].Begin();
      {
        basicEffect.Texture = walls[ i ]; // apparently QUEUES until you End() / Begin() again.
        GraphicsDevice.DrawUserPrimitives( PrimitiveType.TriangleStrip, wallQuads[ i ], 0, 2 );
      }
      basicEffect.CurrentTechnique.Passes[ 0 ].End();
      basicEffect.End();
    }

Right way:

    for( int i = 0; i < 6 ; i++ )
    {
      basicEffect.Texture = walls[ i ]; // THIS will change the texture before going in to render.
      
      basicEffect.Begin();
      basicEffect.CurrentTechnique.Passes[ 0 ].Begin();
      {
        GraphicsDevice.DrawUserPrimitives( PrimitiveType.TriangleStrip, wallQuads[ i ], 0, 2 );
      }
      basicEffect.CurrentTechnique.Passes[ 0 ].End();
      basicEffect.End();
    }

Granted, this probably isn’t a very efficient way to do this at all. I’m thinking of using a cubemap to avoid having to .Begin() and .End() that basic effect 6 times to draw this thing. But I guess it makes sense, because in OpenGL you can NOT bind a texture inside glBegin() and glEnd(), you have to do it before or after glBegin(). So I guess that makes sense, but it seems a bit inefficient.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: