Jump to content

SetMaterial with Manually Created Models/Surfaces


jasonwkeith
 Share

Recommended Posts

I am working my way through some of the sample code and making slight modifications. I am stuck with a very simple problem. I cannot manage to get a model to successfully render a texture.

 

I am using slightly modified code from the leadwerks docs and a stock material.

 

Any insight to my challenged code/thought process is appreciated.

 

#include "App.h"

using namespace Leadwerks;

App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}

App::~App() { delete world; delete window; }

bool App::Start()
{
window = Window::Create();
context = Context::Create(window);
world = World::Create();
camera = Camera::Create();
camera->SetRotation(5, 0, 0);
camera->Move(0, 0, -6);
Light* light = DirectionalLight::Create();
light->SetRotation(35, 35, 0);

Model* model = NULL;

model = Model::Create();
model->SetColor(1.0, 0.0, 1.0);
model->SetPosition(0, 0, 0);

material = Material::Load("Materials/Concrete/concrete_dirty.mat");


Surface* surface = model->AddSurface();
surface->AddVertex(-0.5, -0.5, 0, 0, 0, -1);
surface->AddVertex(0.5, -0.5, 0, 0, 0, -1);
surface->AddVertex(0.5, 0.5, 0, 0, 0, -1);
surface->AddVertex(-0.5, 0.5, 0, 0, 0, -1);
surface->AddTriangle(2, 1, 0);
surface->AddTriangle(0, 3, 2);
surface->UpdateAABB();

model->SetMaterial(material,1);
model->UpdateAABB(Entity::LocalAABB | Entity::GlobalAABB);

return true;
}

bool App::Loop()
{
if (window->Closed() || window->KeyDown(Key::Escape)) return false;

Time::Update();
world->Update();
world->Render();
context->Sync();

return true;
}

Link to comment
Share on other sites

i think you need to perform an update on the surface...

 

http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/surface/surfaceupdate-r638

 

EDIT - actually that works just fine for me in lua... all except for the SetMaterial command. The second argument is bool not integer and will cause an error.

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

Add the UV map (last two)

 

   surface->AddVertex(-0.5, -0.5, 0, 0, 0, -1,  -1,-1);
   surface->AddVertex(0.5, -0.5, 0, 0, 0, -1,  1,-1);
   surface->AddVertex(0.5, 0.5, 0, 0, 0, -1,   1,1);
   surface->AddVertex(-0.5, 0.5, 0, 0, 0, -1,  -1,1);

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

  • 8 years later...
On 5/20/2014 at 7:04 PM, shadmar said:

Add the UV map (last two)


   surface->AddVertex(-0.5, -0.5, 0, 0, 0, -1,  -1,-1);
   surface->AddVertex(0.5, -0.5, 0, 0, 0, -1,  1,-1);
   surface->AddVertex(0.5, 0.5, 0, 0, 0, -1,   1,1);
   surface->AddVertex(-0.5, 0.5, 0, 0, 0, -1,  -1,1);
 

Hi @shadmar

How do you add the UV map in independent Vertex index? I didn't see any documentation on this topic.

Best Regards,

Ed

Link to comment
Share on other sites

This code below creates a plane model facing forward along the Z axis with a squared texture uniformly mapped.

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
Leadwerks::Model* CreatePlaneModel()
{
	auto mdl = Leadwerks::Model::Create();
	auto surf = mdl->AddSurface();

	surf->AddVertex(-0.5, -0.5, 0, 0, 0, -1);
	surf->AddVertex(0.5, -0.5, 0, 0, 0, -1);
	surf->AddVertex(0.5, 0.5, 0, 0, 0, -1);
	surf->AddVertex(-0.5, 0.5, 0, 0, 0, -1);
	surf->AddTriangle(2, 1, 0);
	surf->AddTriangle(0, 3, 2);
	surf->FlipNormals();
	surf->Update();

	surf->SetVertexTexCoords(1, Leadwerks::Vec2(0, 1), 0); // Bottom Left
	surf->SetVertexTexCoords(2, Leadwerks::Vec2(0, 0), 0); // Top Left
	surf->SetVertexTexCoords(3, Leadwerks::Vec2(1, 0), 0); // Top Right
	surf->SetVertexTexCoords(0, Leadwerks::Vec2(1, 1), 0); // Bottom Right

	mdl->UpdateAABB(Leadwerks::Entity::LocalAABB);
	mdl->UpdateAABB(Leadwerks::Entity::GlobalAABB);

	return mdl;
}

Hope this helps. 

  • Thanks 1

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

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...