Jump to content

Simple 3D Graphics with GDI+


Starg
 Share

Recommended Posts

Another example:

post-81173-0-24169600-1375449836_thumb.j

#include <GUIConstantsEx.au3>
#include <S3d.au3>

Global Const $PI = 3.1416

Main()

Func Main()
    ; Create a window and initialize GDI+
    Local $hGUI = GUICreate("S3d Test", 650, 550)
    GUISetState()
    _GDIPlus_Startup()
    
    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    Local $hBitmap = _GDIPlus_BitmapCreateFromGraphics(650, 550, $hGraphics)
    Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    
    Local $hBrushB = _GDIPlus_BrushCreateSolid(0x996699FF)
    Local $hBrushG = _GDIPlus_BrushCreateSolid(0x9966FF33)
    
    Local $hPen = _GDIPlus_PenCreate(0xCCFF6633, 3)
    
    ; Select a Graphic object
    ; width = 650, height = 550
    _S3d_SelectGraphic($hGraphic, 650, 550)
    
    _S3d_SelectPen($hPen)
    
    Local $i = 0
    ; Loop until user exits
    Do
        ; Clear the Graphics object
        _S3d_Clear(0xFFCCCCCC)
        
        ; Set camera
        _S3d_SetCamera(500 * Cos($i), 500 * Sin($i), 1000, 0, 0, 600)
        
        ; Draw an arrow
        _S3d_Arrow(0, 0, -300, 0, 0, 900)
        
        _S3d_SelectBrush($hBrushG)
        
        _S3d_MoveTo2(100, 0, 0, 130, 0, 0)
        For $j = 0 To $PI * 2 * 3 Step ($PI * 2) / 20
            _S3d_RibbonTo(100 * Cos($j), 100 * Sin($j), $j * 50, 130 * Cos($j), 130 * Sin($j), $j * 50)
        Next
        
        _S3d_SelectBrush($hBrushB)
        
        _S3d_MoveTo2(-100, 0, 0, -130, 0, 0)
        For $j = 0 To $PI * 2 * 3 Step ($PI * 2) / 20
            _S3d_RibbonTo(-100 * Cos($j), -100 * Sin($j), $j * 50, -130 * Cos($j), -130 * Sin($j), $j * 50)
        Next
        
        ; Copy to the window
        _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)
        
        $i += 0.3
        Sleep(30)
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    
    ; Clean up resources
    
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrushB)
    _GDIPlus_BrushDispose($hBrushG)
    
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    
    GUIDelete($hGUI)
EndFunc ;==>Main
Link to comment
Share on other sites

UPDATE

New version (v1.2.0) released! Please see the first post.

  • Added: _S3d_SetCameraEx
  • Added: _S3d_SetLocalMatrixEx
  • Added: Optional parameter $iSmooth for _S3d_SelectGraphic to set the graphics object rendering quality
  • Changed: Internal function __S3d_Clip modified to avoid long loop
  • Removed: Unused internal function __S3d_Rotate
If you have written some example scripts that use S3d.au3, feel free to post them here.
Link to comment
Share on other sites

How to make the camera to follow the mouse

Something like this?

 

Edit:

Run 11-Mouse.au3 in the zip instead of this. 

#include <GUIConstantsEx.au3>
#include <S3d.au3>

Opt("MouseCoordMode", 0)    ; relative coords to the active window

Global Const $PI = 3.1416

Main()

Func Main()
    ; Create a window and initialize GDI+
    Local $hGUI = GUICreate("Mouse Test", 650, 550)
    GUISetState()
    _GDIPlus_Startup()
    
    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    Local $hBitmap = _GDIPlus_BitmapCreateFromGraphics(650, 550, $hGraphics)
    Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    
    Local $hBrush = _GDIPlus_BrushCreateSolid(0x996699FF)
    _S3d_SelectBrush($hBrush)
    
    ; Select a Graphic object
    ; width = 650, height = 550
    _S3d_SelectGraphic($hGraphic, 650, 550)
    
    Local $iX = 0, $iY = 0
    ; Loop until user exits
    Do
        ; Clear the Graphics object
        _S3d_Clear(0xFF333333)
        
        ; Set camera
        ; Camera : (0, 0, 150)
        ; $nXYAngle = $i (rad)
        ; $nXZAngle = 0.5 * Sin($i) (rad)
        _S3d_SetCameraEx(0, 0, 150, $iX, $iY)
        
        Local $k
        For $j = 0 To $PI * 2 Step $PI * 2 / 6
            $k = $j + $PI / 6   ; $j + $PI * 2 / 12
            _S3d_Square(1000 * Cos($j), 1000 * Sin($j), 0, _
                1000 * Cos($j), 1000 * Sin($j), 300, _
                1000 * Cos($k), 1000 * Sin($k), 300, _
                1000 * Cos($k), 1000 * Sin($k), 0)
        Next
        
        ; Copy to the window
        _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)
        
        $iX += (MouseGetPos(0) - 275) * 0.00005
        $iY += (MouseGetPos(1) - 325) * 0.00005
        ; Limit value of $iY
        If $iY < -0.5 Then
            $iY = -0.5
        ElseIf $iY > 0.5 Then
            $iY = 0.5
        EndIf
        
        Sleep(30)
        
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    
    ; Clean up resources
    
    _GDIPlus_BrushDispose($hBrush)
    
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    
    GUIDelete($hGUI)
EndFunc ;==>Main
Edited by Starg
Link to comment
Share on other sites

UPDATE

New version (v1.2.1) released! Please see the first post.

  • Added: Function Reference (not yet completed)
  • Changed: _S3d_Square calls _S3dLine when $fFill = False
  • Fixed: Strange behavior of _S3d_LineTo

Enjoy!

Link to comment
Share on other sites

UPDATE

New version (v1.2.2) released! Please see the first post.

  • Fixed: _S3d_RibbonTo didn't return 1 on success
  • Fixed: _S3d_SetCameraEx accidentally reversed $nXYAngle and $nXZAngle
  • Changed: _S3d_Square performance improved

Enjoy!

Link to comment
Share on other sites

UPDATE

New version (v1.3.0) released! Please see the first post.

  • Added: _S3d_InitCurrentPos().
  • Added: New optional parameter $fRefresh for _S3d_MultiplyLocalMatrix() and _S3d_Local****()'s to determine whether to update the local matrix.
  • Changed: Better algorithm for handling the current position.
  • Changed: Internal function __S3d_SetCamera() performance improved.
  • Changed: _S3d_MoveTo(), _S3d_LineTo(), _S3d_MoveTo2(), and _S3d_RibbonTo() no longer return values or set @error.
  • Changed: Internal function __S3d_CreateMatrix() now accepts initial values.
Enjoy!
Link to comment
Share on other sites

  • 2 weeks later...

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
 Share

×
×
  • Create New...