fm85's picture

3D object in front of a 2D background texture

Hello,

I have a problem showing a 3D model in front of a texture. The model shall rotate while the background should stay stable. I found a solution that does the job but unfortunatelly it only works once correctly. On the second call of the function the background become darker than on the first time and I can figure out why! Here's my code:

        void Render()
        {
            GL.ClearColor(Color.Black);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.Enable(EnableCap.Texture2D);
            GL.Viewport(0, 0, glControl1.Width, glControl1.Height);
 
            // 2D
            GL.LoadIdentity();
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.BindTexture(TextureTarget.Texture2D, texture);
 
            GL.Begin(BeginMode.Quads);
                GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-1f, 1f);
                GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(1f, 1f);
                GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(1f, -1f);
                GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-1f, -1f);
            GL.End();
 
            // 3D
            // Camera
            GL.MatrixMode(MatrixMode.Modelview);
            Matrix4 lookat = Matrix4.LookAt(0, 2, 5, 0, 0, 0, 0, 1, 0);
            GL.LoadMatrix(ref lookat);
            GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
            GL.MatrixMode(MatrixMode.Projection);
            float aspect_ratio = Width / (float)Height;
            Matrix4 perpective = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 64);
            GL.LoadMatrix(ref perpective);
            // Draw 3d Object
            DrawCube(-2, -1, 0, 0.1f, 2, 1);
 
            glControl1.SwapBuffers();
        }

Can anybody help me out and tell me whats the reason for this behaviour and how to solve it?

Thanks a lot in advance!


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jrwatts's picture

Is your DrawCube function changing the drawing color? If so, you need to reset it to white afterwards, as the draw color is applied to everything drawn from that point on (including the texture on your quad in the next pass).