CAST II Game Engine Community

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: LoadObj/LoadMesh?  (Read 1583 times)

0 Members and 1 Guest are viewing this topic.

gogetax1

  • Newbie
  • *
  • Posts: 12
Re: LoadObj/LoadMesh?
« Reply #11 on: December 25, 2009, 01:38:23 PM »

i see, so i will be waiting for the sample >_<
Logged

admin

  • Administrator
  • Full Member
  • *****
  • Posts: 192
    • CAST II Engine
Re: LoadObj/LoadMesh?
« Reply #10 on: December 25, 2009, 12:13:45 PM »

No this for .obj. .X loader is based on DXSDK example and incomplete yet.
Logged

gogetax1

  • Newbie
  • *
  • Posts: 12
Re: LoadObj/LoadMesh?
« Reply #9 on: December 25, 2009, 09:39:22 AM »

thanks for the code. is it can load directx (.x) format too? or is it should be some other way?

Quote
Thank you for pointing on a problem with .bpl. I'll update the files to fix it now.

no problem. i love reporting bugs >_<
Logged

admin

  • Administrator
  • Full Member
  • *****
  • Posts: 192
    • CAST II Engine
Re: LoadObj/LoadMesh?
« Reply #8 on: December 25, 2009, 09:03:19 AM »

CAST II is a so called "data driven" engine. The only right way to use it is to prepare your data (scenes in case of an engine) and pass it to the engine. The engine should convert the data to optimal format and give an interface for in-game logic to make manipulations on the scene without loss of optimizations.
If you will load models, call API functions to change textures and other states and so on, an engine will be unable to optimize anything.
If you have two models and a landscape it's not a problem (and may be you do not need an engine in that case), but if you making a serious game with thousands of objects and materials, it is. And it's almost impossible to load and setup all these objects in code. Therefore any good engine should have an editor to do that work.

Of course, there is a way to load assets properly.
Here is a code from CASTEd which loads .obj model:
Code: [Select]
// Loads one or more .obj files making them a child noted to ParentNode.
// If number of files is more than one them can be used as frames for animation (vertex morphing).
procedure TFResTools.LoadObjFiles(ParentNode: TItem; AFiles: TStrings);
var
  i: Integer;
  VRes: array of TVerticesResource;   // Vertex resources for models
  IRes: array of TIndicesResource;    // Index resources for models
  TRes: TImageResource;               // Texture resource
  Actor: TMesh;                       // Scene item in case of non-animated model
  AnimActor: TMorphedItem;            // Scene item in case of vertex morphing animated model
  Props: TProperties;                 // Properties
  Mat: TMaterial;                     // Material item
  Garbage: IRefcountedContainer;
  Animated: Boolean;
  SnapIndex: Integer;                 // Index of vertex to snap vertex morphing animated model to

  // Creates and sets up a default material with texture in TRes
  procedure AddMaterial;
  var Tech: TTechnique; Pass: TRenderPass;
  begin
    Pass := TRenderPass.Create(Core);             // First and only pass
    Tech := TTechnique.Create(Core);              // Default technique
    Mat  := TMaterial.Create(Core);               // Material itself

    Mat.Name := GetFileName(AFiles[0]);

    ParentNode.AddChild(Mat);                     // Add material to scene
    Mat.AddChild(Tech);                           // Also technique
    Tech.AddChild(Pass);                          // And pass

    // Set up technique properties
    Props.Clear;
    Props.Add('Total passes', vtInt,        [], '1',              '');
    Props.Add('Pass #0',      vtObjectLink, [], Pass.GetFullName, '');       // Add link to the pass
    Tech.SetProperties(Props);                                               // Apply properties

    // Set up material properties
    Props.Clear;
    Props.Add('Total techniques', vtInt,        [], '1',              '');
    Props.Add('Technique #0',     vtObjectLink, [], Tech.GetFullName, '');   // Add link to the technique
    Mat.SetProperties(Props);                                                // Apply properties

    // Set up pass properties
    Props.Add('Total stages',     vtNat,        [], '1',              '');
    if Assigned(TRes) then begin
      ParentNode.AddChild(TRes);
      Props.Add('Stage #0\Texture', vtObjectLink, [], TRes.GetFullName, ''); // Add link to texture
    end;
    Pass.SetProperties(Props);                                               // Apply properties
    Pass.State := Pass.State + [isVisible];                                  // Make the pass visible
  end;

const GeomPrefix = 'Geometry\Frame #%D ';       // Use in Format()

begin
  Mat := nil;
  Props := TProperties.Create;

  // setup garbage collector
  Garbage := CreateRefcountedContainer;
  Garbage.AddObject(Props);

  Animated := False;
  case AFiles.Count of
    0: Exit;
    1: ;
    else if ModelLoadF.ShowModal = mrOK then begin                        // Let a user to choose if the model has animation
      Animated := ModelLoadF.PageControl1.ActivePageIndex = 1;
      if ModelLoadF.SnapCBox.Checked then
        SnapIndex := StrToIntDef(ModelLoadF.SnapIndexEdit.Text, -1) else
          SnapIndex := -1;
    end else Exit;
  end;

  // Create a scene item for a model with animation
  if Animated then begin
    AnimActor := TMorphedItem.Create(Core);
    AnimActor.Name := GetFileName(AFiles[0]);
    ParentNode.AddChild(AnimActor);
    AnimActor.TotalFrames := AFiles.Count;
  end;

  SetLength(VRes, AFiles.Count);
  SetLength(IRes, AFiles.Count);
  // For each file
  for i := 0 to AFiles.Count-1 do begin
    // Try to load model
    if not LoadObj(AFiles[i], VRes[i], IRes[i], TRes) then begin
      {$IFDEF LOGGING} Log.Log('Failed to load file "' + AFiles[i] + '"', lkError); {$ENDIF}
      Continue;
    end;

    // Setup material
    if not Animated then
      AddMaterial
    else if not Assigned(Mat) then begin
      AddMaterial;
      if Assigned(Mat) then Props.Add('Material', vtObjectLink, [], Mat.GetFullName,  '');
    end;

    Actor := nil;
    if Assigned(VRes) then begin
      ParentNode.AddChild(VRes[i]);                       // Add resource containing vertices to scene

      if Animated then begin                              // Setup vertices and indices for animated models
        Props.Add(Format(GeomPrefix + 'vertices', [i]), vtObjectLink, [], VRes[i].GetFullName, '');
        if Assigned(IRes) then begin
          ParentNode.AddChild(IRes[i]);
          Props.Add(Format(GeomPrefix + 'indices', [i]),  vtObjectLink, [], IRes[i].GetFullName, '');
        end;
        if (SnapIndex >= 0) and (i > 0) then              // Snap model to the specified vertex
          ScaleForm.MoveVertices(VRes[i], SubVector3s(VRes[0].VertexCoords[SnapIndex], VRes[i].VertexCoords[SnapIndex]));
      end else begin                                      // Setup vertices and indices for non-animated models
        Props.Clear;
        Actor := TMesh.Create(Core);                      // Create a new scene item for model
        Actor.Name := GetFileName(AFiles[0]);
        ParentNode.AddChild(Actor);                       // Add to scene

        // Setup material property
        if Assigned(Mat) then Props.Add('Material', vtObjectLink, [], Mat.GetFullName,  '');
      end;

      // Setup vertex and index resources for model
      if not Animated or (i = 0) then begin
        Props.Add('Geometry\Vertices', vtObjectLink, [], VRes[i].GetFullName, '');
        if Assigned(IRes) then begin
          ParentNode.AddChild(IRes[i]);
          Props.Add('Geometry\Indices', vtObjectLink, [], IRes[i].GetFullName, '');
        end;
      end;

    end;
  end;

  // Apply properties to item
  if Animated then begin
    AnimActor.SetProperties(Props);
    AnimActor.SetFrames(0, 0, 0);
  end else
    Actor.SetProperties(Props);

  // Refresh items tree of CASTEd
  MainF.ItemsFrame1.RefreshTree;
  if Assigned(Actor) then MainF.ItemsFrame1.SelectItem(Actor, False);

  // Show up model manipulation tools
  ScaleForm.Show;
end;

Thank you for pointing on a problem with .bpl. I'll update the files to fix it now.
Logged

gogetax1

  • Newbie
  • *
  • Posts: 12
Re: LoadObj/LoadMesh?
« Reply #7 on: December 22, 2009, 02:43:57 PM »

so you saying the only easy way to load a model to my engine is 1st, import it to the editor, 2nd export it to scene and 3rd import the screne in your engine?

im sorry to say that but thats sux.
isnt there any examples that shows how to load the .x/obj or whatever models into the engine? or at least there will be?

[Edit]
oh, just in case, the editor not working for me properly..
it asking for delphi7 libraries that lying on the system32 folder (vcl70.bpl and so on)
so and i dont have it, and editor wont load (althought i found all of those library files, but still not running properly)
« Last Edit: December 22, 2009, 02:47:18 PM by gogetax1 »
Logged

admin

  • Administrator
  • Full Member
  • *****
  • Posts: 192
    • CAST II Engine
Re: LoadObj/LoadMesh?
« Reply #6 on: December 22, 2009, 02:09:59 PM »

.x and .obj models can be easily loaded in editor (CASTEd).
You still can load it manually but it is not as simple as a call of a single function. That's because the engine is designed for WYSIWYG scene manipulation in CASTEd.
Internal model format is .cbf. You can save a model in a file by using "File->Save node" menu item in editor and then load it with LoadItem() method of scene manager (Core).
Please note that if you loading a model from a file you should save to the file not only the model itself but also all items which it references to. Such as material, texture, vertex/index buffer resources.
When you save an item all its child items are also saved so it's usefull to make referenced items to be child items of the main one.
Logged

gogetax1

  • Newbie
  • *
  • Posts: 12
Re: LoadObj/LoadMesh?
« Reply #5 on: December 22, 2009, 01:05:13 PM »

ok, so i tryed .X and .Obj file and nothing happened (except of .obj said "out of memory")
but still. nothing happened with all of those 3 formats i used..
Logged

admin

  • Administrator
  • Full Member
  • *****
  • Posts: 192
    • CAST II Engine
Re: LoadObj/LoadMesh?
« Reply #4 on: December 22, 2009, 12:05:00 PM »

.3ds is not the internal format for CAST II so neither Itm.Load() neither Core.LoadItem() will load it. These functions can load only those files which were saved with Itm.Save or using save function in CASTEd.
Logged

gogetax1

  • Newbie
  • *
  • Posts: 12
Re: LoadObj/LoadMesh?
« Reply #3 on: December 21, 2009, 12:03:15 PM »

ok thanks..
now, i did something like:
Code: [Select]
itmManag := TItemsManager.Create;
  Stream := TFileStream.Create('Media\Map\Cube.3ds');
  Itm := TItem.Create(itmManag);
  Itm.Load(Stream);
  Stream.Free;

all loaded without errors, but i do not see anything on screen at all..
do i need to put something on my while look except of core.process; ?
Logged

admin

  • Administrator
  • Full Member
  • *****
  • Posts: 192
    • CAST II Engine
Re: LoadObj/LoadMesh?
« Reply #2 on: December 20, 2009, 09:29:58 PM »

There is no need to use LoadObj() command. Models are loaded into CASTEd and then can be saved and loaded in CAST internal format via Item.Save() and Core.LoadItem() functions respectively.
LoadObj() can be also used but then you have to create visual object and attach all the resource which are returned by LoadObj().
Logged

gogetax1

  • Newbie
  • *
  • Posts: 12
LoadObj/LoadMesh?
« Reply #1 on: December 20, 2009, 01:17:38 PM »

hey guys, i've been looking around inside the C2ResImport library, i seen that LoadMesh command was eraced and there is only LoadObj on it.
so i was wondering, what can load .x file formats and how to use the commands? cuz it looks like LoadScene and LoadObj are using different strategies..
so.. how do i use that LoadObj command?
Logged
 

+ Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:

Please enter the number 234 in the field: