Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting
Direct3D render disappearing after presenting Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
Go Back  Xtreme Visual Basic Talk > > > Direct3D render disappearing after presenting


Reply
 
Thread Tools Display Modes
  #1  
Old 10-19-2012, 11:10 AM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 102
Default Direct3D render disappearing after presenting


I've got two projects that are supposed to be 3D renderings. One of them renders just fine, the other does not.

The one that isn't working I copied the code from the working one expecting it to work. The major difference is that the one that doesn't work renders in a double-buffered Panel and the working one uses a non-toplevel form inherited control (yes, I tried switching back and forth with these controls in the bad proj, but it didn't do any good). The problem I'm having is that the Panel will render some polys, but it immediately disappears. However, if I drag the panel off screen, it redraws fine. I'm not sure what's different. Here's the relevant code:

Code:
Public Class DoubleBuffPanel
    Inherits Panel

    Public Sub New()
        'Me.DoubleBuffered = True
        MyBase.New()
        SetStyle(ControlStyles.AllPaintingInWmPaint Or _
                 ControlStyles.OptimizedDoubleBuffer Or _
                 ControlStyles.ResizeRedraw, _
                 True)
        UpdateStyles()
    End Sub

End Class

Public Class SubRender
    'dbpRender is a DoubleBuffPanel control on the main Form
    Private device As Microsoft.DirectX.Direct3D.Device
    Private cameraxpos, cameraypos, camerazpos As Single
    Private cameraxang, camerayang, camerazang As Single
    Private ground(64, 64)() As CustomVertex.PositionColored

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For i As Byte = 0 To 64
            For j As Byte = 0 To 64
                ReDim Preserve ground(i, j)(3)
                For k = 0 To 3
                    ground(i, j)(k).Position = New Vector3((i - 32) * 16, 0, (i - 32) * 16)
                    ground(i, j)(k).Color = 'a map of colors
                Next
            Next
        Next
        cameraxpos = 1
        cameraypos = 80
        camerazpos = 0
        cameraxang = 0
        camerayang = 0
        camerazang = 0

        Dim present As PresentParameters = New PresentParameters
        present.Windowed = True
        present.SwapEffect = SwapEffect.Discard
        present.AutoDepthStencilFormat = DepthFormat.D24S8
        present.EnableAutoDepthStencil = True
        device = New Direct3D.Device(0, DeviceType.Hardware, dbpRender, CreateFlags.SoftwareVertexProcessing, present)
        With device
            .Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI / 4), dbpRender.Width / dbpRender.Height, 1, 500) 'sets field of view, aspect ratio, etc
            .Transform.View = Matrix.LookAtLH(New Vector3(cameraxpos, cameraypos, camerazpos), New Vector3(cameraxang, camerayang, camerazang), New Vector3(0, 1, 0)) 'position and direction
            .RenderState.SourceBlend = Blend.SourceAlpha
            .RenderState.DestinationBlend = Blend.InvSourceAlpha
            .RenderState.AlphaBlendEnable = True
        End With
        device.PresentationParameters.BackBufferWidth = dbpRender.Width
        device.PresentationParameters.BackBufferHeight = dbpRender.Height
    End Sub

    Private Sub dbpRender_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles dbpRender.Disposed
        device.Dispose()
    End Sub

    Private Sub dbpRender_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dbpRender.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            device.RenderState.FillMode = device.RenderState.FillMode Xor 1 'switch back and forth between wireframe and solid
            dbpRender.Invalidate()
        End If
    End Sub

    Private Sub dbpRender_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles dbpRender.Paint

        device.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI / (36 / 5)), dbpRender.Width / dbpRender.Height, 1, 50000) 'sets field of view, aspect ratio, etc
        device.Transform.View = Matrix.LookAtLH(New Vector3(cameraxpos, cameraypos, camerazpos), New Vector3(cameraxang, camerayang, camerazang), New Vector3(0, 1, 0)) 'position and direction

        device.RenderState.Lighting = False
        device.RenderState.CullMode = Cull.None  'no triangle is culled
        device.RenderState.Clipping = True

        device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, dbpRender.BackColor, 1.0, 0)

        device.BeginScene()
        device.VertexFormat = CustomVertex.PositionColored.Format
        device.Transform.World = Matrix.Translation(0, 0, 0) * Matrix.RotationAxis(New Vector3(0, 0, 0), 0) 'angle * 0.2, angle * 1.5, angle * 3), angle)
        device.RenderState.ZBufferEnable = True

        For i As Byte = 0 To 64
            For j As Byte = 0 To 64
                device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, ground(i, j))
            Next
        Next

        device.EndScene() 'all drawings before this line

        device.Present()
    End Sub
End Class
It's trying to render a topographic "map" where the ground is changing in elevation. I've also got some mouse controls that move the camera around so the user can view it from different angles.

Any thoughts?
Reply With Quote
  #2  
Old 10-19-2012, 01:01 PM
passel's Avatar
passelDirect3D render disappearing after presenting passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Well, I haven't worked with DirectX so I don't know, but I'm thinking that perhaps the panel shouldn't be double-buffered.
I would assume, not knowing DirectX, that when you set up your Direct3D.Device and pass it your panel, that is setting up DirectX to render to the screen area associated with that panel.
But since you set the panel to be double buffered, in the Paint event, the panel has set up a graphics object pointing to a memory buffer for you to draw on using the graphics object passed to you in the paint event arguments.
I further assume, that DirectX is rendering wherever it renders and when done, updates the screen area you initialized it to (the panel screen area). Now, as you leave the paint event, the Back buffer that the panel set up to double buffer for the Graphics object to use is refreshed to the same screen area, wiping out the drawing done by DirectX.
So your would see a brief flash of some of the DirectX output before it was replaced by the panel's back buffer.
Try it without doublebuffering the panel to see how it acts.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 10-19-2012, 01:55 PM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 102
Default

I've tried that. Turning it off and on and even switching the base object to a form and back. None of that makes a difference.

I'll counter your assumptions with some of my own.

I assume the double-buffer works the opposite way as you suggest. The buffer is the previous image that is the background until the pending rendering/drawing is completed and displayed over the buffer. The buffer then becomes the completed drawn render and the cycle starts anew.
Panels don't have double buffering by default so there's no back buffer to draw after the rendering. It's almost like it's re-drawing the form behind the panel after the device has "presented" its render. As long as I'm continuously calling the paint event through dbpRender.invalidate() it displays the terrain just fine.
Reply With Quote
  #4  
Old 10-19-2012, 02:39 PM
passel's Avatar
passelDirect3D render disappearing after presenting passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

What if you didn't use the paint event?
Since the Paint event is for WinForms drawing, and you're not doing WinForms drawing, it seems like you're asking for trouble mixing those.

Also, not knowing DirectX, I'm a little surpised you could pass a .net object like dbpRender to it. I would have assumed you would have to pass dbpRender.Handle, but perhaps it it the same thing.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 10-19-2012 at 02:52 PM.
Reply With Quote
  #5  
Old 10-19-2012, 03:17 PM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 102
Default

Quote:
Originally Posted by passel View Post
What if you didn't use the paint event?
...I'm a little surpised you could pass a .net object like dbpRender to it. I would have assumed you would have to pass dbpRender.Handle, but perhaps it it the same thing.
It works either way, actually. Ironically, specifying "Me" instead of dbpRender in the device's constructor makes it render in the form just fine. It must be a problem with my control.

I got it working now...somehow. The principle difference is I'm adding it to the form at runtime instead of design time (using Me.Controls.Add(dbpRender) ). I think the designer adds some hidden attribute that doesn't make it work right. The designer was forcing visibility to false when the custom control was inheriting Form. Weird. I'll call this solved even though I'm not sure what I did.
Reply With Quote
  #6  
Old 10-20-2012, 09:54 AM
AtmaWeapon's Avatar
AtmaWeaponDirect3D render disappearing after presenting AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Honestly that's disappointingly common with Managed DirectX. It's been unsupported by MS since like 2008, due to /terrible/ architectural flaws. There were literally crashing bugs for which the solution was "run the application again and pray". I'd strongly recommend looking into alternative project like SharpDX, though I don't use any so I can't say what the best is.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
Direct3D render disappearing after presenting Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
 
Direct3D render disappearing after presenting
Direct3D render disappearing after presenting
 
-->