+ Reply to Thread
Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26

Thread: G3P - The GPU General Graphing Project

  1. #11
    Master of Mars Magos Mechanicus's Avatar
    Join Date
    May 2008
    Posts
    852

    Default Re: G3P - The GPU General Graphing Project

    Yeah, I guess there are multiple ways to go about it. Much like HP, though, part of my reason to do this was that I wanted to write a compiler...

    I tried returning through eax, and after a bit more finagling it started working and returning appropriate output for the current input. Apparently, fsin expects degrees rather than radians, which is why it was 8 rather than -18. Time to get to writing the GPU.
    I can wire anything directly into anything! I'm the Professor!
    -Professor Hubert Farnsworth

  2. #12
    That furred thing Black Phoenix's Avatar
    Join Date
    Feb 2007
    Location
    Kyiv, Ukraine
    Posts
    3,565

    Default Re: G3P - The GPU General Graphing Project

    If you have the function, or the data ready for rendering, I hacked together this rendering code:
    Code:
    //==============================================================================
    // 3D graph drawer
    //==============================================================================
    
    //Must divide by each other, or else it'll fuck up big time
    define WIDTH,50;
    define TRIS_IN_FRAME,10;
    define HEIGHT,50;
    
    //
    
    mov #regHWClear,0;
    mov EDX,HEIGHT; mul EDX,WIDTH; div EDX,TRIS_IN_FRAME;
    cmp #Frame,EDX; jge @DrawExit; jmp Render.Start;
    @DrawExit: dexit;
    
    Render.Start:
    cmp #Frame,0; jne Render.SkipInitialization;
      dclrscr cBackgroundColor;
      //Apply aspect ratio correction
      mov #vPerspective.Y,1.6;
      //Initialize perspective transform matrix
      mperspective mProjectionMatrix,vPerspective;
    
    Render.SkipInitialization:
      dcvxpipe 3; //Set -1..1 coordinate system
      dvxpipe  5; //Use matrix projection routine
      mloadproj mProjectionMatrix;
      dsetlight 0,lSun;//vPerspective
    
      denable 0; //Vertex buffer
      denable 2; //Lighting
    
      call ApplyTransform;
      dcolor cDefaultColor;
    
      mov EDX,WIDTH;
      div EDX,TRIS_IN_FRAME;
    
      //set coordinates
      mov EBX,#Frame; div EBX,EDX; fint EBX;
      mov EAX,#Frame; mod EAX,EDX; fint EAX; mul EAX,TRIS_IN_FRAME;
    
      //draw triangle stripe
      mov EDX,TRIS_IN_FRAME;
      @DrawLoop:
        call DrawTriangle;
        inc EAX;
        loopd @DrawLoop;
      dvxflush;
    
      inc #Frame;
    dexit;
    
    
      
    
    
    //= Apply transformations ======================================================
    ApplyTransform:
      //Compute look-at matrix (for camera)
      mlookat mViewMatrix,vLookAt;
    
      //Rotate & translate target model
      mrotate mRotateMatrix,vRotate;
      mtranslate mTranslateMatrix,vTranslate;
      mscale mScaleMatrix,vScale;
    
      //Create Model matrix
      mmov mModelMatrix,mRotateMatrix;
      mmul mModelMatrix,mTranslateMatrix;
      mmul mModelMatrix,mScaleMatrix;
    
      //Compute and load ModelView matrix
      mmov mModelViewMatrix,mViewMatrix;
      mmul mModelViewMatrix,mModelMatrix;
      mload mModelViewMatrix;
    ret
    
    //EAX: X, EBX: Y, Z -> ECX
    GenerateHeight:
      push eax; push ebx;
        add EAX,#vTranslate.X;
        add EBX,#vTranslate.Y;
    
        mul EAX,EAX;
        mul EBX,EBX;
        add EAX,EBX; mul EAX,0.5;
        fpwr EAX,0.5;
        add EAX,0.1;
    
        fsin ECX,EAX;
        div ECX,EAX;
        mul ECX,10;
    //    fsin EAX,EAX;
    //    fcos ECX,EBX;
    //    mul ECX,EAX;
    
        //Z = Sin(R) / R
      pop ebx; pop eax;
    ret
    
    DrawTriangle:
      mov EBP,VertexData;
    
      //Face 1
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
    
      inc EAX;
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
    
      inc EBX;
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
    
      //Face 2
      dec EBX; dec EAX;
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
    
      inc EBX; inc EAX;
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
    
      dec EAX;
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
      dec EBX;
    
      dvxdata_3f VertexData,2;
    ret
    
    
    
    
    
      
    //= Rendering settings and parameters ==========================================
    matrix mRotateMatrix;       matrix mTranslateMatrix;    matrix mScaleMatrix;
    matrix mProjectionMatrix;   matrix mViewMatrix;         matrix mModelMatrix;
    matrix mPRotateMatrix;      matrix mPTranslateMatrix;   matrix mModelViewMatrix;
    //------------------------------------------------------------------------------
    
    vector4f vRotate,         0,    0,    1,    0; //<AXIS X Y Z> <ANGLE W>
    vector4f vTranslate,     -25,  -25,    0,    0; //<TRANLSATION X Y Z> <0>
    vector4f vScale,          1,    1,    1,    0; //<SCALE X Y Z> <0>
    vector4f vPerspective,   30,  1.6,    1,   20; //<FOV> <ASPECT RATIO> <ZNEAR> <ZFAR>
    
    vLookAt: vector3f vLookAt_Eye,     20,  20,  30; //Where our camera is
             vector3f vLookAt_Center,   0,   0,   0; //What we look at
             vector3f vLookAt_Up,       0,   0,  -1; //Where our head points
    
    color cBackgroundColor,     0,   0,   0;
    color cDefaultColor,      255, 255, 255;
    
    alloc Frame;
    
    lSun: vector4f Sun.Pos, 100, 100, 100,   0; //x y z <unused, will be falloff>
          color    Sun.Col, 255, 255, 255, 0.5; //R G B Brightness
    //==============================================================================
    
    VertexData:
      db 0,0,0; //Face1
      db 0,0,0;
      db 0,0,0;
      db 0,0,0; //Face2
      db 0,0,0;
      db 0,0,0;
    It draws 3D graphs like this:



    You can adjust width/height for quality, and probably you also might want to move camera around in case it's not fitting on the screen.
    I'm a wire-crazy person with a tail.

    Take a daily journey into my brain

    D2K5

  3. #13
    Master of Mars Magos Mechanicus's Avatar
    Join Date
    May 2008
    Posts
    852

    Default Re: G3P - The GPU General Graphing Project

    Wow, very fancy. Knowing absolutely zilch about how to do GPU 3d drawing, though, I'm kind of lost looking at it.
    I've been kind of busy and have yet to manage to draw anything 2D yet, but I'll definitely experiment with this. Thanks a lot for the example!
    I can wire anything directly into anything! I'm the Professor!
    -Professor Hubert Farnsworth

  4. #14
    Ursus maritimus Drunkie's Avatar
    Join Date
    Feb 2009
    Location
    Canada
    Posts
    5,662
    Blog Entries
    1

    Default Re: G3P - The GPU General Graphing Project

    Quote Originally Posted by Black Phoenix View Post
    If you have the function, or the data ready for rendering, I hacked together this rendering code:
    Code:
    //==============================================================================
    // 3D graph drawer
    //==============================================================================
    
    //Must divide by each other, or else it'll fuck up big time
    define WIDTH,50;
    define TRIS_IN_FRAME,10;
    define HEIGHT,50;
    
    //
    
    mov #regHWClear,0;
    mov EDX,HEIGHT; mul EDX,WIDTH; div EDX,TRIS_IN_FRAME;
    cmp #Frame,EDX; jge @DrawExit; jmp Render.Start;
    @DrawExit: dexit;
    
    Render.Start:
    cmp #Frame,0; jne Render.SkipInitialization;
      dclrscr cBackgroundColor;
      //Apply aspect ratio correction
      mov #vPerspective.Y,1.6;
      //Initialize perspective transform matrix
      mperspective mProjectionMatrix,vPerspective;
    
    Render.SkipInitialization:
      dcvxpipe 3; //Set -1..1 coordinate system
      dvxpipe  5; //Use matrix projection routine
      mloadproj mProjectionMatrix;
      dsetlight 0,lSun;//vPerspective
    
      denable 0; //Vertex buffer
      denable 2; //Lighting
    
      call ApplyTransform;
      dcolor cDefaultColor;
    
      mov EDX,WIDTH;
      div EDX,TRIS_IN_FRAME;
    
      //set coordinates
      mov EBX,#Frame; div EBX,EDX; fint EBX;
      mov EAX,#Frame; mod EAX,EDX; fint EAX; mul EAX,TRIS_IN_FRAME;
    
      //draw triangle stripe
      mov EDX,TRIS_IN_FRAME;
      @DrawLoop:
        call DrawTriangle;
        inc EAX;
        loopd @DrawLoop;
      dvxflush;
    
      inc #Frame;
    dexit;
    
    
      
    
    
    //= Apply transformations ======================================================
    ApplyTransform:
      //Compute look-at matrix (for camera)
      mlookat mViewMatrix,vLookAt;
    
      //Rotate & translate target model
      mrotate mRotateMatrix,vRotate;
      mtranslate mTranslateMatrix,vTranslate;
      mscale mScaleMatrix,vScale;
    
      //Create Model matrix
      mmov mModelMatrix,mRotateMatrix;
      mmul mModelMatrix,mTranslateMatrix;
      mmul mModelMatrix,mScaleMatrix;
    
      //Compute and load ModelView matrix
      mmov mModelViewMatrix,mViewMatrix;
      mmul mModelViewMatrix,mModelMatrix;
      mload mModelViewMatrix;
    ret
    
    //EAX: X, EBX: Y, Z -> ECX
    GenerateHeight:
      push eax; push ebx;
        add EAX,#vTranslate.X;
        add EBX,#vTranslate.Y;
    
        mul EAX,EAX;
        mul EBX,EBX;
        add EAX,EBX; mul EAX,0.5;
        fpwr EAX,0.5;
        add EAX,0.1;
    
        fsin ECX,EAX;
        div ECX,EAX;
        mul ECX,10;
    //    fsin EAX,EAX;
    //    fcos ECX,EBX;
    //    mul ECX,EAX;
    
        //Z = Sin(R) / R
      pop ebx; pop eax;
    ret
    
    DrawTriangle:
      mov EBP,VertexData;
    
      //Face 1
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
    
      inc EAX;
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
    
      inc EBX;
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
    
      //Face 2
      dec EBX; dec EAX;
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
    
      inc EBX; inc EAX;
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
    
      dec EAX;
      call GenerateHeight;
      mov #EBP,EAX; inc EBP;
      mov #EBP,EBX; inc EBP;
      mov #EBP,ECX; inc EBP;
      dec EBX;
    
      dvxdata_3f VertexData,2;
    ret
    
    
    
    
    
      
    //= Rendering settings and parameters ==========================================
    matrix mRotateMatrix;       matrix mTranslateMatrix;    matrix mScaleMatrix;
    matrix mProjectionMatrix;   matrix mViewMatrix;         matrix mModelMatrix;
    matrix mPRotateMatrix;      matrix mPTranslateMatrix;   matrix mModelViewMatrix;
    //------------------------------------------------------------------------------
    
    vector4f vRotate,         0,    0,    1,    0; //<AXIS X Y Z> <ANGLE W>
    vector4f vTranslate,     -25,  -25,    0,    0; //<TRANLSATION X Y Z> <0>
    vector4f vScale,          1,    1,    1,    0; //<SCALE X Y Z> <0>
    vector4f vPerspective,   30,  1.6,    1,   20; //<FOV> <ASPECT RATIO> <ZNEAR> <ZFAR>
    
    vLookAt: vector3f vLookAt_Eye,     20,  20,  30; //Where our camera is
             vector3f vLookAt_Center,   0,   0,   0; //What we look at
             vector3f vLookAt_Up,       0,   0,  -1; //Where our head points
    
    color cBackgroundColor,     0,   0,   0;
    color cDefaultColor,      255, 255, 255;
    
    alloc Frame;
    
    lSun: vector4f Sun.Pos, 100, 100, 100,   0; //x y z <unused, will be falloff>
          color    Sun.Col, 255, 255, 255, 0.5; //R G B Brightness
    //==============================================================================
    
    VertexData:
      db 0,0,0; //Face1
      db 0,0,0;
      db 0,0,0;
      db 0,0,0; //Face2
      db 0,0,0;
      db 0,0,0;
    It draws 3D graphs like this:



    You can adjust width/height for quality, and probably you also might want to move camera around in case it's not fitting on the screen.
    Looks great in EmuFox but,
    Would a 3D terrain like that be possible to draw with a decent FPS inside GMOD?

  5. #15
    That furred thing Black Phoenix's Avatar
    Join Date
    Feb 2007
    Location
    Kyiv, Ukraine
    Posts
    3,565

    Default Re: G3P - The GPU General Graphing Project

    Quote Originally Posted by Drunkie View Post
    Looks great in EmuFox but,
    Would a 3D terrain like that be possible to draw with a decent FPS inside GMOD?
    Look at how much instructions it takes. It just draws everything in batches over timespan of many frames, you can adjust how many polygons should it output per frame.
    I'm a wire-crazy person with a tail.

    Take a daily journey into my brain

    D2K5

  6. #16
    That furred thing Black Phoenix's Avatar
    Join Date
    Feb 2007
    Location
    Kyiv, Ukraine
    Posts
    3,565

    Default Re: G3P - The GPU General Graphing Project

    Picture proof:
    I'm a wire-crazy person with a tail.

    Take a daily journey into my brain

    D2K5

  7. #17
    Ursus maritimus Drunkie's Avatar
    Join Date
    Feb 2009
    Location
    Canada
    Posts
    5,662
    Blog Entries
    1

    Default Re: G3P - The GPU General Graphing Project

    Quote Originally Posted by Black Phoenix View Post
    Picture proof:
    Please do tell.
    How do you slow down how many polygons it outputs per frame?

  8. #18
    Ursus maritimus Drunkie's Avatar
    Join Date
    Feb 2009
    Location
    Canada
    Posts
    5,662
    Blog Entries
    1

    Default Re: G3P - The GPU General Graphing Project

    Black phoenix, how do you set how fast it outputs the polygons per frame?

    Also when I copy your code into a GPU, the terrain shows all pink.
    Do I have to enable something using denable?

  9. #19
    That furred thing Black Phoenix's Avatar
    Join Date
    Feb 2007
    Location
    Kyiv, Ukraine
    Posts
    3,565

    Default Re: G3P - The GPU General Graphing Project

    Quote Originally Posted by Drunkie View Post
    Black phoenix, how do you set how fast it outputs the polygons per frame?

    Also when I copy your code into a GPU, the terrain shows all pink.
    Do I have to enable something using denable?
    "define TRIS_IN_FRAME,10;" sets how many triangles per frame it should output (actually this is batches, not triangles. 10 means 20 polygons will be drawn).

    Yes, terrain all-pink is an internal GPU bug, luckily it's easy to fix. Just add "drectwh 0,0" after these lines:
    Code:
      call ApplyTransform;
      dcolor cDefaultColor;
    so it becomes

    Code:
      call ApplyTransform;
      dcolor cDefaultColor;
      drectwh 0,0;
    That'll fix it.
    I'm a wire-crazy person with a tail.

    Take a daily journey into my brain

    D2K5

  10. #20
    Ursus maritimus Drunkie's Avatar
    Join Date
    Feb 2009
    Location
    Canada
    Posts
    5,662
    Blog Entries
    1

    Default Re: G3P - The GPU General Graphing Project

    Alright, gonna try that out now. Thanks!

+ Reply to Thread
Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots