Jump to content

Set Material and Texture


gamecreator
 Share

Recommended Posts

I'm sure I'm doing something incredibly stupid and this should be simple but...

I load a model like so:

 

TModel ball=LoadModel("data/ball.gmf");

 

I then try to put a new material on it:

 

PaintEntity(ball,LoadMaterial("data/ball2.mat"));

 

Engine.log shows that the material and the new texture (box1.dds) are both successfully loaded but the ball doesn't change. Any idea why?

 

I also tried swapping just the texture with this:

 

SetMaterialTexture(GetEntityMaterial(ball),LoadTexture("data/box1.dds"),0);

 

But that crashed at the very start of the run (Engine.log was empty).

 

Anyone know what I'm doing wrong?

Link to comment
Share on other sites

I should see what code you used to crash (maybe you tried to set texture on the model material when no material was assigned to it?), however the code to change the texture of a material at runtime:

 

// .. engine initialize functions
TModel box = LoadModel("abstract::oildrum.gmf");
TMaterial mat = LoadMaterial("abstract::cobblestones.mat");
PaintEntity(box, mat, 1);
while(!KeyHit(KEY_ESCAPE) && !AppTerminate())
{
if (KeyHit(KEY_I))
	SetMaterialTexture(mat, LoadTexture("abstract::road_dashedwhite2.dds"), 0);
else if (KeyHit(KEY_V))
	SetMaterialTexture(mat, LoadTexture("abstract::cobblestones.dds"), 0);
UpdateFramework();
RenderFramework();
Flip();
}
return Terminate();

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

The line that causes the crash is this:

 

SetMaterialTexture(GetEntityMaterial(ball),LoadTexture("data/box1.dds"),0);

 

Based on what you told me, I suspected that I need to get the material not from ball but from ball's child. So I changed it to this:

 

SetMaterialTexture(GetEntityMaterial(GetChild(ball,1)),LoadTexture("data/box1.dds"),0);

 

This doesn't crash immediately and generates a complete log (which doesn't show anything irregular) but the program does still crash when it gets to that line. Here's the complete code:

 

//    ====================================================================
#include "engine.h"

#if defined( _WINDOWS )
   void ErrOut( const char* pstrMessage ) { MessageBoxA(0, pstrMessage, "Error", 0 ); }
   int WINAPI WinMain( HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int nShowCmd ) 
#else
   #include <string>
   void ErrOut( const std::string& message ) { puts( message.c_str()); }
   int main( int argn, char* argv[] )
#endif
{
   if(!Initialize()) return 1;

   RegisterAbstractPath("D:/Programming/Leadwerks Engine SDK");

   Graphics(1024,768);
   TFramework fw = CreateFramework();
   if(fw==NULL)
   {
       MessageBoxA(0,"Failed to initialize engine.","Error",0);
       return 1;
   }
   SetGlobalObject("fw",fw);

   // Set Lua framework variable
   BP lua=GetLuaState();
   lua_pushobject(lua,fw);
   lua_setglobal(lua,"fw");
   lua_pop(lua,1);

   // Get framework main camera
   TCamera camera = GetLayerCamera(GetFrameworkLayer(0));
   PositionEntity(camera,Vec3(2,6,-5));
   RotateEntity(camera,Vec3(45,0,0));

   TModel scene=LoadScene("data/scene.sbx");
   TModel ball=LoadModel("data/ball.gmf");
   PositionEntity(ball,Vec3(0.0,2.0,0.0));

   PositionEntity(camera,Vec3(0.0,8.0,-5.0));

//    SetMaterialTexture(GetEntityMaterial(ball),LoadTexture("data/box1.dds"),0);
   SetMaterialTexture(GetEntityMaterial(GetChild(ball,1)),LoadTexture("data/box1.dds"),0);

   while(!KeyHit(KEY_ESCAPE))
   {
       UpdateFramework();
       RenderFramework();

       DrawText(0,100,"children = %d",CountChildren(ball));

       Flip(0);
   }
}

Link to comment
Share on other sites

just to ask the obvious question, but does the ball have a material file listed in its GMF? if there is no material file, i suspect this is why its failing... why not attach the ball.gmf to the post so someone can try to troubleshoot.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

this way seems to work fine... don't know why GetEntityMaterial() fails...

RegisterAbstractPath("")
Graphics(800,600)
fw=CreateFramework()
fw.main.camera:SetPositionf(0,0,-2)

ball=LoadModel("abstract::ball.gmf")
ballmaterial = GetSurfaceMaterial(GetSurface(GetChild(ball,1),1))
balltexture = GetMaterialTexture(ballmaterial,0)
boxtexture = LoadTexture("abstract::box1.dds")

ground=CreateCube()
ground:SetScalef(10.0,1.0,10.0)
ground:SetPositionf(0.0,-2.0,0.0)
ground:Paint(ballmaterial)
light=CreateDirectionalLight()
light:SetRotationf(45,45,45)
while AppTerminate()==0 do
       ball:Turnf(AppSpeed()*0.5,AppSpeed()*0.5,AppSpeed()*0.5)

if MouseHit(1)==1 then SetMaterialTexture(ballmaterial,boxtexture,0) end
if MouseHit(2)==1 then SetMaterialTexture(ballmaterial,balltexture,0) end

       fw:Update()
       fw:Render()

DrawText("Left click to change to box texture",0,50)
DrawText("Right click to change to ball texture",0,70)

       Flip()
end

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Thank you very much macklebee!! Here's how the one-time switch looks in C.

 

TMaterial ballmaterial = GetSurfaceMaterial(GetSurface(GetChild(ball,1),1));
TTexture boxtexture = LoadTexture("data/box1.dds");
SetMaterialTexture(ballmaterial,boxtexture,0);

or combined into a single line:

 

SetMaterialTexture(GetSurfaceMaterial(GetSurface(GetChild(ball,1),1)),LoadTexture("data/box1.dds"),0);

And thanks again ZioRed as well.

 

Bonus points to anyone who gets GetEntityMaterial working with this. :) I couldn't get it to do anything but return 0 or crash.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...