Results 1 to 23 of 23

Thread: 3d Engine

  1. #1
    Join Date
    Nov 2006
    Posts
    1,103
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default 3d Engine

    Well, maybe not quite an 3D engine, but it draws a cube and knows what sides not to draw, enough of this talking, here is the script:
    SCAR Code:
    type
      MyPoint = record
        x,y,z : Integer;
      end;

    Type
      TPoly = Array[1..8] of MyPoint;
    Type
      T_2D = Array[1..8] of TPoint;

    var
      YRot, XRot : Extended;
      DebugCanv, CubeCanv : TCanvas;
      TempBmp : integer;

    Function SetupBox(Size : integer) : TPoly;
    Var
      Poly : TPoly;
    begin
      Poly[1].x := 1 * size;
      Poly[1].y := 1 * size;
      Poly[1].z := 1 * size;
      Poly[2].x := -1 * size;
      Poly[2].y := 1 * size;
      Poly[2].z := 1 * size;
      Poly[3].x := -1 * size;
      Poly[3].y := 1 * size;
      Poly[3].z := -1 * size;
      Poly[4].x := 1 * size;
      Poly[4].y := 1 * size;
      Poly[4].z := -1 * size;
      Poly[5].x := 1 * size;
      Poly[5].y := -1 * size;
      Poly[5].z := 1 * size;
      Poly[6].x := -1 * size;
      Poly[6].y := -1 * size;
      Poly[6].z := 1 * size;
      Poly[7].x := -1 * size;
      Poly[7].y := -1 * size;
      Poly[7].z := -1 * size;
      Poly[8].x := 1 * size;
      Poly[8].y := -1 * size;
      Poly[8].z := -1 * size;
      Result := Poly;
    end;

    Function _3DTo2D(X,Y,Z: integer; RotX, RotY, RotZ : extended) : TPoint;
    begin
      Result.x := Round((X*Cos(RotY)-Z*Sin(RotY)));
      Result.y := -Round((Y*Cos(RotX)+(-X*Sin(RotY)-Z*Cos(RotY))*Sin(RotX)));
    end;



    Function CheckSide(Vertices : array of integer; _2D : T_2D) : boolean;
    var
      I : integer;
      Middle : TPoint;
      PreviousAngle : Extended;
      CurrentAngle : Extended;
      BiggerCount : integer;
    begin
      Middle.x := 0;
      Middle.y := 0;
      BiggerCount := 0;
      For I := 0 to 3 do
        begin
          Middle.X := Middle.X + _2D[Vertices[I]].x;
          Middle.Y := Middle.Y + _2D[Vertices[I]].y;
        end;
      Middle.x := Round(Middle.x/4);
      Middle.y := Round(Middle.y/4);
      PreviousAngle := ArcTan2(_2D[Vertices[3]].x - Middle.x, _2d[Vertices[3]].y - Middle.y);
      For I := 0 to 3 do
        begin
          CurrentAngle := ArcTan2(_2D[Vertices[I]].x - Middle.x, _2d[Vertices[I]].y - Middle.y);
          If CurrentAngle > PreviousAngle then Inc(BiggerCount);
          PreviousAngle := CurrentAngle;
        end;
      If (BiggerCount >= 3) then Result := True;
    end;

    Procedure RenderSide(Vertices : Array of Integer; Dots : integer; Canvas : TCanvas; _2D : T_2D);
    var
      I : integer;
    begin
      Canvas.MoveTo(_2D[Vertices[0]].x + 300,_2D[Vertices[0]].y + 200);
      For I := 0 to 3 do
        Canvas.LineTo(_2D[Vertices[I]].x + 300,_2D[Vertices[I]].y + 200);
      Canvas.LineTo(_2D[Vertices[0]].x + 300,_2D[Vertices[0]].y + 200);
    end;

    Procedure DrawSide(Side : integer; Canvas : TCanvas; _2D : T_2D);
    begin
      Case Side of
        1 : begin
              if CheckSide([4,3,2,1],_2D) then
                RenderSide([4,3,2,1],1,Canvas,_2D);
            end;
        2 : begin
              if CheckSide([5,6,7,8],_2D) then
                RenderSide([5,6,7,8],6,Canvas,_2D);
            end;
        3 : begin
              if CheckSide([2,6,5,1],_2D) then
                RenderSide([2,6,5,1],3,Canvas,_2D);
            end;
        4 : begin
              if CheckSide([4,8,7,3],_2D) then
                RenderSide([4,8,7,3],4,Canvas,_2D);
            end;
        5 : begin
              if CheckSide([5,8,4,1],_2D) then
                RenderSide([5,8,4,1],2,Canvas,_2D);
            end;
        6 : begin
              if CheckSide([2,3,7,6],_2D) then
                RenderSide([2,3,7,6],5,Canvas,_2D);
            end;
      end;
    end;

    Procedure DrawCube(RotX, RotY, RotZ : extended; Size : integer; var C : TCanvas);
    var
      I : integer;
      Poly : TPoly;
      _2D : T_2D;
    begin
      C.Pen.Color := 0;
      C.Brush.Color := 0;
      C.Rectangle(0,0,600,400);
      Poly := SetupBox(Size);
      For I := 1 to 8 do
        _2D[I] := _3DTo2D(Poly[I].x,Poly[I].y, Poly[I].z, RotX, RotY, RotZ);
      C.Pen.Color := 16777215;
      For I := 1 to 6 do
        DrawSide(I, C, _2D);

    end;

    begin
      DisplayDebugImgWindow(600,400);
      DebugCanv :=GetDebugCanvas;
      TempBmp := BitmapFromString(600, 400, '');
      CubeCanv := GetBitmapCanvas(TempBMP);
      Repeat
        DrawCube(XRot, YRot , 0.0, 100,CubeCanv);
        CopyCanvas(CubeCanv, DebugCanv, 0, 0, 600, 400, 0, 0, 600, 400);
        YRot := YRot - 0.01*Pi;
        XRot := XRot - 0.01*Pi;
        wait(30);
      Until(False);
    end.

    Have fun
    Ow and credits to Raymond for helping me with a huge memory leak and some other bugs
    Infractions, reputation, reflection, the dark side of scripting, they are.

  2. #2
    Join Date
    Feb 2007
    Location
    Het ademt zwaar en moedeloos vannacht.
    Posts
    7,211
    Mentioned
    26 Post(s)
    Quoted
    72 Post(s)

    Default

    This is simply amazing! May there come more stuff like this from you
    I made a new script, check it out!.

  3. #3
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Lol, nice!

    I give you rep++ when you make the pen color dark, and paint the whole cube, then by the angle adjust the darkness O.o

  4. #4
    Join Date
    Nov 2006
    Posts
    1,103
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    i dont really know a lot about how lightning works... but if you want that, try the directx api
    Infractions, reputation, reflection, the dark side of scripting, they are.

  5. #5
    Join Date
    Apr 2007
    Posts
    3,152
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    itd be cool if you could drag the shape yourself to rotate it
    other than that amazing. (before n3ss3s' idea maybe add more shapes )
    SCAR Tutorials: The Form Tutorial | Types, Arrays, and Classes
    Programming Projects: NotePad | Tetris | Chess


  6. #6
    Join Date
    Nov 2006
    Posts
    1,103
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    well, if you change to loops to use array length, it should be possible to make any shape... feel free to edit this code as you want
    Infractions, reputation, reflection, the dark side of scripting, they are.

  7. #7
    Join Date
    Mar 2007
    Location
    Netherlands->Amersfoort.
    Posts
    1,615
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Wow thats cool!
    poeple can do more with scar then i ever thought.

  8. #8
    Join Date
    Jul 2007
    Posts
    1,055
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    This is really cool. Only bad thing is when you try and make the cube * 2 it is too big for the debug image and then if you try and make the debug window bigger the cube disappears.

  9. #9
    Join Date
    Nov 2006
    Posts
    1,103
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    well, the cube is already 2*2 XD 1--1 = 2^^ anyways, whats the point of just increasing its size, it will still be the same shape, but to do this, change all the sizes of the canvases/bitmaps
    Infractions, reputation, reflection, the dark side of scripting, they are.

  10. #10
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Its truly amazing!
    Verrekte Koekwous

  11. #11
    Join Date
    Dec 2006
    Location
    Third rock from the sun.
    Posts
    2,510
    Mentioned
    4 Post(s)
    Quoted
    1 Post(s)

    Default

    Wow, fun ^_^

    And in less than 200 lines. Nice.

  12. #12
    Join Date
    Jul 2007
    Posts
    1,431
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by mastaraymondAndNegaal View Post
    Its truly amazing!
    .
    [CENTER][SIZE="4"]Inactive[/SIZE]I forgot my password[/CENTER]

  13. #13
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Sweet dude gw!
    I do visit every 2-6 months

  14. #14
    Join Date
    Mar 2007
    Posts
    1,700
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    That's rather sexy. GJ

  15. #15
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    i made it so that you can steer the rotation:

    SCAR Code:
    program New;
    var
      Mx, My : Integer;

    type
      MyPoint = record
        x,y,z : Integer;
      end;

    Type
      TPoly = Array[1..8] of MyPoint;
    Type
      T_2D = Array[1..8] of TPoint;

    var
      YRot, XRot : Extended;
      DebugCanv, CubeCanv : TCanvas;
      TempBmp : integer;

    Function SetupBox(Size : integer) : TPoly;
    Var
      Poly : TPoly;
    begin
      Poly[1].x := 1 * size;
      Poly[1].y := 1 * size;
      Poly[1].z := 1 * size;
      Poly[2].x := -1 * size;
      Poly[2].y := 1 * size;
      Poly[2].z := 1 * size;
      Poly[3].x := -1 * size;
      Poly[3].y := 1 * size;
      Poly[3].z := -1 * size;
      Poly[4].x := 1 * size;
      Poly[4].y := 1 * size;
      Poly[4].z := -1 * size;
      Poly[5].x := 1 * size;
      Poly[5].y := -1 * size;
      Poly[5].z := 1 * size;
      Poly[6].x := -1 * size;
      Poly[6].y := -1 * size;
      Poly[6].z := 1 * size;
      Poly[7].x := -1 * size;
      Poly[7].y := -1 * size;
      Poly[7].z := -1 * size;
      Poly[8].x := 1 * size;
      Poly[8].y := -1 * size;
      Poly[8].z := -1 * size;
      Result := Poly;
    end;

    Function _3DTo2D(X,Y,Z: integer; RotX, RotY, RotZ : extended) : TPoint;
    begin
      Result.x := Round((X*Cos(RotY)-Z*Sin(RotY)));
      Result.y := -Round((Y*Cos(RotX)+(-X*Sin(RotY)-Z*Cos(RotY))*Sin(RotX)));
    end;



    Function CheckSide(Vertices : array of integer; _2D : T_2D) : boolean;
    var
      I : integer;
      Middle : TPoint;
      PreviousAngle : Extended;
      CurrentAngle : Extended;
      BiggerCount : integer;
    begin
      Middle.x := 0;
      Middle.y := 0;
      BiggerCount := 0;
      For I := 0 to 3 do
        begin
          Middle.X := Middle.X + _2D[Vertices[i]].x;
          Middle.Y := Middle.Y + _2D[Vertices[i]].y;
        end;
      Middle.x := Round(Middle.x/4);
      Middle.y := Round(Middle.y/4);
      PreviousAngle := ArcTan2(_2D[Vertices[3]].x - Middle.x, _2d[Vertices[3]].y - Middle.y);
      For I := 0 to 3 do
        begin
          CurrentAngle := ArcTan2(_2D[Vertices[i]].x - Middle.x, _2d[Vertices[i]].y - Middle.y);
          If CurrentAngle > PreviousAngle then Inc(BiggerCount);
          PreviousAngle := CurrentAngle;
        end;
      If (BiggerCount >= 3) then Result := True;
    end;

    Procedure RenderSide(Vertices : Array of Integer; Dots : integer; Canvas : TCanvas; _2D : T_2D);
    var
      I : integer;
    begin
      Canvas.MoveTo(_2D[Vertices[0]].x + 300,_2D[Vertices[0]].y + 200);
      For I := 0 to 3 do
        Canvas.LineTo(_2D[Vertices[i]].x + 300,_2D[Vertices[i]].y + 200);
      Canvas.LineTo(_2D[Vertices[0]].x + 300,_2D[Vertices[0]].y + 200);
    end;

    Procedure DrawSide(Side : integer; Canvas : TCanvas; _2D : T_2D);
    begin
      Case Side of
        1 : begin
              if CheckSide([4,3,2,1],_2D) then
                RenderSide([4,3,2,1],1,Canvas,_2D);
            end;
        2 : begin
              if CheckSide([5,6,7,8],_2D) then
                RenderSide([5,6,7,8],6,Canvas,_2D);
            end;
        3 : begin
              if CheckSide([2,6,5,1],_2D) then
                RenderSide([2,6,5,1],3,Canvas,_2D);
            end;
        4 : begin
              if CheckSide([4,8,7,3],_2D) then
                RenderSide([4,8,7,3],4,Canvas,_2D);
            end;
        5 : begin
              if CheckSide([5,8,4,1],_2D) then
                RenderSide([5,8,4,1],2,Canvas,_2D);
            end;
        6 : begin
              if CheckSide([2,3,7,6],_2D) then
                RenderSide([2,3,7,6],5,Canvas,_2D);
            end;
      end;
    end;

    Procedure DrawCube(RotX, RotY, RotZ : extended; Size : integer; var C : TCanvas);
    var
      I : integer;
      Poly : TPoly;
      _2D : T_2D;
    begin
      C.Pen.Color := 0;
      C.Brush.Color := 0;
      C.Rectangle(0,0,600,400);
      Poly := SetupBox(Size);
      For I := 1 to 8 do
        _2D[i] := _3DTo2D(Poly[i].x,Poly[i].y, Poly[i].z, RotX, RotY, RotZ);
      C.Pen.Color := 16777215;
      For I := 1 to 6 do
        DrawSide(I, C, _2D);

    end;

    begin
      DisplayDebugImgWindow(600,400);
      DebugCanv :=GetDebugCanvas;
      TempBmp := BitmapFromString(600, 400, '');
      CubeCanv := GetBitmapCanvas(TempBMP);
      Repeat
        DrawCube(XRot, YRot , 0.0, 100,CubeCanv);
        CopyCanvas(CubeCanv, DebugCanv, 0, 0, 600, 400, 0, 0, 600, 400);
        getmousepos(MX, MY);
        if ((Mx < 200) and (My < 768)) then
        begin
          YRot := YRot - 0.02*Pi;
          XRot := XRot - 0.02*Pi;
        end;
        if (Mx > 824) and (My > 0) then
        begin
          YRot := YRot + 0.02*Pi;
          XRot := XRot + 0.02*Pi;
        end;
        wait(30);
      Until(False);
    end.

    Just move the mouse to left or right of your screen and it will move that direction!

  16. #16
    Join Date
    Nov 2006
    Posts
    1,103
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    nice, i did it in delphi, put it will rotate only if you hold the mouse button, anyways, does anyone know how to do z-axis rotation?
    Infractions, reputation, reflection, the dark side of scripting, they are.

  17. #17
    Join Date
    Mar 2007
    Posts
    3,681
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Killerdou View Post
    nice, i did it in delphi, put it will rotate only if you hold the mouse button, anyways, does anyone know how to do z-axis rotation?
    woah I love it and Zytex I get an error on your version =S

  18. #18
    Join Date
    Oct 2007
    Location
    Anime land. Woot!
    Posts
    552
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    total 'oonage for get ownage this is oonage! =p
    keep devoping it untill it's good as blender? k? lol jk very cool, fun to watch aswell.

  19. #19
    Join Date
    Jan 2007
    Location
    Tennessee
    Posts
    642
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Wow this is amazing!
    Completely different than the way i do mine.
    Yours uses ArcTan2, i was thinking of doing that.
    You can check mine out in the other scripts section.

  20. #20
    Join Date
    May 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Wow, simply awesome. Make it a dice

  21. #21
    Join Date
    Mar 2008
    Location
    The Netherlands
    Posts
    1,395
    Mentioned
    1 Post(s)
    Quoted
    1 Post(s)

    Default

    Make it a cone


  22. #22
    Join Date
    Feb 2009
    Location
    Irvine, CA
    Posts
    2,873
    Mentioned
    8 Post(s)
    Quoted
    138 Post(s)

    Default

    yo this looks like the objects that spin in some random events :O pretty soon someone will utilize this to make sides of the objects and find which object to click/identify.... lol i could see it now...

    PWN PWN SPAM

  23. #23
    Join Date
    Jul 2008
    Location
    Canada
    Posts
    1,612
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. New RS-Engine?
    By Junior in forum RS has been updated.
    Replies: 2
    Last Post: 07-03-2007, 09:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •