Page 1 of 9 123 ... LastLast
Results 1 to 10 of 82

Thread: Is OBJ or 3ds export possible ?

  1. #1

    Is OBJ or 3ds export possible ?

    Have a question or two regarding the export of OBJ or 3ds files

    1. Is it possible ?

    2. I am using list and primitive objects to generate a tree using Petr's script.
    I have created a user interface to control tree parameters and then generate trees
    based on these parameters.
    Can the data generated by the script then be captured and exported to OBJ or 3DS ?

    3.Are there any examples of how to implement Exporting or Saving of OBJ or 3DS files ?

    4. I noticed that thinedge supports export to these formats and would like to know if the
    export features will be implemented in TBGL

    I know this is probably something that is relatively easy but forgive me for the lack of
    knowledge in this area as i am mainly a 3d modeller and have just got into scripting recently.

    Ask me to make a multi textured ,high resolution ,3d model of a Banshee = No Problem

    Ask me to Script a function to save my life = I am dead



  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Re: Is OBJ or 3ds export possible ?

    Hi Macros,

    TBGL does not feature this, as it is not possible to do it for geometry defined using immediate mode/display lists - this is sent directly to driver and archived nowhere.

    But there is still hope, multiple options

    OBJ format is very simple, so it is not that hard to write custom exporter.

    Take following code:
    [code=thinbasic]
    TBGL_BeginPoly %GL_TRIANGLES
    TBGL_Vertex -1, -1, 0
    TBGL_Vertex 1, -1, 0
    TBGL_Vertex 0, 1, 0
    TBGL_EndPoly
    [/code]

    It can be "translated" to OBJ as following:
    # TRIANGLE
    # List of vertices
    v -1.000000 -1.000000 0.000000
    v 1.000000 -1.000000 0.000000
    v 0.000000 1.000000 0.000000

    # List of faces pointing to vertex list above
    f 1 2 3
    You can also make list of texture coordinates and even define material (texture, color) via MTL file.

    OBJ does not support geometric primitives or transformations, so for exporting cylinder, you would have to create simple triangle mesh for it, and then postprocess vertices via matrix multiplication - you would basically duplicate push/pop matrix, translate, rotate commands.

    OBJ format specification can be found on the web, for example here:
    http://local.wasp.uwa.edu.au/~pbourke/dataformats/obj/

    VRML
    Now that you are scared from doing ton of work, there is VRML - it is very close to OpenGL representation of data, includes primitives and transformations. I guess this would be the most straightforward way, but I am not sure if your 3D modeler supports import from this.

    [code=thinbasic]
    TBGL_PushMatrix
    TBGL_Translate 2, 2, 2
    TBGL_Cylinder 2, 2, 4
    TBGL_PopMatrix
    [/code]

    would become

    Separator {
    translation {
    translation 2, 2, 2
    }
    Cylinder {
    radius 2
    height 4
    }
    }
    ... or something like that


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3

    Re: Is OBJ or 3ds export possible ?

    If have understood correctly the TBGL_cylinders that make up the tree branches cannot be exported
    and because the leaves are a display list then they cannot be exported either is that right ?

    So i would have to create the cylinder by defining its vertices?

    If i did this how would i then change the length\radius of cylinder dynamically ?

    About the code example you have written in your post

    TBGL_BeginPoly %GL_TRIANGLES
    TBGL_Vertex -1, -1, 0
    TBGL_Vertex 1, -1, 0
    TBGL_Vertex 0, 1, 0
    TBGL_EndPoly

    Isnt this a display list ?

    I have attached the Treegenerator GUI script from the other Treegenerator post.

    Would you be able to post an example of how to implement the simple triangle method you have described into the tree generator?


    As for the export problem i will probably have to have an attempt at coding my own exporter for .OBJ
    Although i am not sure about where to start.


    I dont mean to be a pain i just figure its better to ask someone with more knowledge on the subject.



    Attached Files Attached Files

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Re: Is OBJ or 3ds export possible ?

    Hi Macro,

    all you have to do is:
    - define cylinder via triangle vertices
    - cast transformations via transformation matrices

    Here is simple example of defining points for Cylinder:
    [code=thinBasic]
    '
    ' One of the possible ways to generate Cylinder, vertex by vertex
    ' Petr Schreiber, started on 03-06-2009
    '

    Uses "TBGL" , "Math"

    FUNCTION TBMAIN()
    LOCAL hWnd AS DWORD
    LOCAL FrameRate AS DOUBLE

    ' -- Create and show window
    hWnd = TBGL_CreateWindowEx("TBGL script - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED or %TBGL_WS_CLOSEBOX)
    TBGL_ShowWindow

    ' -- Initialize lighting
    TBGL_UseLighting %TRUE
    TBGL_UseLightSource %GL_LIGHT0, %TRUE

    tbgl_SetLightParameter %GL_LIGHT0, %TBGL_LIGHT_POSITION, 15, 10, 15, 1

    ' -- Resets status of all keys
    TBGL_ResetKeyState()

    ' -- Main loop
    While TBGL_IsWindow(hWnd)
    FrameRate = TBGL_GetFrameRate

    TBGL_ClearFrame
    TBGL_Camera 15, 15, 15, 0, 0, 0

    TBGL_Color 255, 128, 0
    RenderCylinder(1, 2, 3)

    TBGL_DrawFrame

    ' -- ESCAPE key to exit application
    If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

    Wend

    TBGL_DestroyWindow
    END FUNCTION

    SUB RenderCylinder( BYVAL lowR AS EXT, BYVAL hiR AS EXT, BYVAL height AS EXT )
    LOCAL i AS EXT
    LOCAL n AS LONG, px AS SINGLE, pz AS SINGLE, x AS SINGLE, y AS SINGLE, z AS SINGLE
    LOCAL j AS LONG
    LOCAL theta1, theta2, theta3 AS SINGLE

    n = 24

    j = (( n / 2 ) - 2 ) / 2

    theta1 = j * (Pi*2) / n - (Pi/2)
    theta2 = ( j + 1 ) * (Pi*2) / n - (Pi/2)
    TBGL_BeginPoly %GL_QUAD_STRIP
    FOR i = 0 TO n

    theta3 = i * (2*Pi) / n

    x = COS( theta2 ) * COS( theta3 )
    y = SIN( theta2 )
    z = COS( theta2 ) * SIN( theta3 )
    px = lowR * x
    pz = lowR * z
    TBGL_Normal x, y, z
    tbgl_TexCoord2D - theta3 * lowR, 0.0

    TBGL_Vertex px, 0.0, pz

    x = COS( theta2 ) * COS( theta3 ) ' theta1
    y = SIN( theta2 )
    z = COS( theta2 ) * SIN( theta3 ) ' theta1
    px = hiR * x
    pz = hiR * z
    TBGL_Normal x, y, z

    tbgl_TexCoord2D - theta3 * lowR, height
    TBGL_Vertex px, height, pz
    NEXT i
    TBGL_EndPoly 'GL_QUAD_STRIP

    END SUB
    [/code]

    But this is just to help you understand it "visually", for OBJ you would have to generate separate list of vertices and faces of course.

    Do you think VRML is no way? I sadly do not have time resources to help more at this time, but feel free to ask.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5

    Re: Is OBJ or 3ds export possible ?

    Hi again
    Vrml isnt really suitable as i would like to make it easy to use trees in many different 3d apps and OBJ is the most cross compatible format.

    Is it possible to perform the same tree generation using M15 models, For example if i modelled the cylinder and leaves in thin edge, and saved them as seperate M15 files e.g - Cylinder.M15 and Leaves.M15

    Could i then perform the same transforms and generation using the M15 models instead of display lists?

    Then i should be able to export out as OBJ ?




  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Re: Is OBJ or 3ds export possible ?

    Hi Macros,

    TBGL_m15SaveModel function could be done ... but I think it would be easier to do it directly to OBJ, as m15 would not help you with the most tricky parts - translations + rotations.

    Translations and rotations can be retrieved from OpenGL matrices, I can look into it, but it will take me some days as I am little bit short in time.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  7. #7

    Re: Is OBJ or 3ds export possible ?

    I am trying to understand how to do this but im getting confused.

    If i create the cylinder using Polygon modeling methods eg.
    (The code below just creates a polygon)

    TBGL_PUSHMATRIX
    TBGL_BEGINPOLY %GL_POLYGON
    TBGL_NORMAL 0, 0, 1
    TBGL_TEXCOORD2D 0, 0
    TBGL_VERTEX - 1, 0, 0
    TBGL_TEXCOORD2D 1, 0
    TBGL_VERTEX 1, 0, 0
    TBGL_TEXCOORD2D 1, 1
    TBGL_VERTEX 1, 1, 0
    TBGL_TEXCOORD2D 0, 1
    TBGL_VERTEX - 1, 1, 0
    TBGL_ENDPOLY
    TBGL_POPMATRIX

    I should be able to recall the data to export it to OBJ shouldnt i ?

    i am having a bit of trouble distinguishing between display lists and polygon methods.

    Are they the same thing?

    For example if i have created the polygon above inside a display list will the data be retrievable or not?

    If not couldnt i just create a function called BP() that builds the cylinder from polygons and then call the function instead of using TBGL_CYLINDER for example:

    FOR i = 1 TO levels

    TBGL_CYLINDER radiusS, radiusE, length CHANGE THIS TO BP() Function
    TBGL_TRANSLATE 0, length, 0
    TBGL_ROTATE openangle, 0, 0, 1
    TBGL_ROTATE twist, 0, 1, 0
    TBGL_PUSHMATRIX
    TBGL_ROTATE - openangle * 2, 0, 0, 6
    IF i = 1 THEN
    DrawBranch( radiusE, length, levels - 1, - openangle, twist )
    ELSE
    DrawBranch( radiusE, length, levels - i, openangle, twist )
    END IF
    TBGL_POPMATRIX
    radiusS = radiusE
    radiusE = radiusS - startRadius / levels
    length = length - startlength / levels
    NEXT
    TBGL_POPMATRIX

    I know the code above would need more refinement than just adding the BP() function but i am just concentrating on getting the cylinder working first and will worry about rest later.

    I had an idea
    How about changing the scaling of the BP() function cylinder to adjust radius and length

    If i adjust the scaling of the BP() function created cylinder along x axis and z axis to change the radius and scaling y axis for length it should work , and with the data generated from this i would be able to export to OBJ wouldnt i ?

    The only problem is i am not sure how to do this.
    Once again thanks for any help provided.

  8. #8
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Re: Is OBJ or 3ds export possible ?

    Macros,

    let me explain a bit how definition of TBGL geometry works.

    Immediate mode
    Geometry defined between TBGL_BeginPoly / TBGL_EndPoly.
    You define objects vertex by vertex. Definition is redirected to driver, no storage of data is performed
    Speed rating: slowest, because you use lot of function calls to define object. It has educative value

    M15 Models
    Geometry defined on the fly using TBGL_m15* commands, or loaded from file.
    Geometry is rendered in more optimized way, info about vertices is stored.
    Speed rating: fast

    Geometry primitives
    Geometry defined using single call like TBGL_Sphere, TBGL_Box, ...
    Rendering approach used differs from object to object, internal data are not accessible by TBGL programmer.
    Speed rating: varies

    And in the end we have special class, encapsulating all of the above:

    Display lists
    It allows to "cache" geometry created via any of previously described approaches using TBGL_NewList / TBGL_EndList.
    You create display list before rendering, then call group of cached commands via TBGL_CallList.
    Speed rating: fast, thanks to fact geometry is transfered to driver once, not each frame.

    As you see, all those approaches are +/- geometry-to-driver things.
    When you need to output raw data to file, you should define custom data structures and write them to file later.

    How openGL / TBGL knows how to draw?
    It internally works with matrix stack.

    TBGL_Translate shifts actual "3D cursor" position
    TBGL_Rotate turns actual "3D cursor" orientation
    TBGL_Scale scales actual coordinate system.

    So you first define transformations, and when you define geometry later, all its vertices are postprocessed by matrix which reflects current position, orientation and scaling.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  9. #9

    Re: Is OBJ or 3ds export possible ?

    So by defining a primitive in a display list the the data would be retrievable and able to be exported?

    Or would i just define the vertices to make the cylinder inside the display list and then retrieve the data when needed?

    I know im asking a lot of questions but i am still trying to understand the basics while getting in well over my head.

  10. #10
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Re: Is OBJ or 3ds export possible ?

    Hi Macro,

    it is important to ask questions, no problem.

    Answer to your question is no - driver caches it in vendor specific form, so no way to get it back safely.

    I think the key, in this case, is understanding the algorithm as a such, and reconstruct the vertex positions using own math routines, there is no need for *GL.
    You are lucky as ThinBasic supports matrix arithmetics.

    Growing the tree in this case is nothing else than set of elemental translate-rotate transformations, casted on 1 scaled object - cylinder.


    Petr

    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Page 1 of 9 123 ... LastLast

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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