Jump to content

Transform::Point


gamecreator
 Share

Go to solution Solved by Josh,

Recommended Posts

Can someone please tell me what's wrong with my code?

//  Print vertex coordinates
for(int i=0; i<((Model *)model->GetChild(0))->GetSurface(0)->CountVertices(); i++)
{
	Vec3 vp = ((Model *)model->GetChild(0))->GetSurface(0)->GetVertexPosition(i);
	Vec3 vg = Transform::Point(vp,model,NULL);   // Tranform local model coordinates to global??

	printf("local vertex %d: %f, %f, %f\n",i,vp.x,vp.y,vp.z);
	printf("global vertex : %f, %f, %f\n\n",vg.x,vg.y,vg.z);
}

The above prints the same coordinates for both local and global even though I know for sure that they should be different (I placed a box in the scene at 0,0,0 to confirm).  It's the same if I switch the model and NULL parameters.

 

Link to comment
Share on other sites

Attached is the model file and below is the full code.

#include "Leadwerks.h"

using namespace Leadwerks;

Model* model;

int main(int argc, const char *argv[])
{
	Leadwerks::Window* window = Leadwerks::Window::Create();
	Context* context = Context::Create(window);
	World* world = World::Create();

	Camera* camera = Camera::Create();
    camera->SetRotation(15, 0, 0);
    camera->Move(0, 0, -2);

	Light* light = DirectionalLight::Create();
	light->SetRotation(35, 35, 0);

	//  Create a box at 0,0,0 for reference
	Model* boxmodel = NULL;
    boxmodel = Model::Box();
    boxmodel->SetPosition(0, 0, 0);
    boxmodel->SetScale(0.1, 0.1, 0.1);

	//  Load ground model
	model = Model::Load("Models/ground.mdl");

	while (true)
	{
		if(window->Closed() || window->KeyDown(Key::Escape)) return false;

		if(window->KeyHit(Key::Enter))
		{
			//  Print vertex coordinates
			for(int i=0; i<((Model *)model->GetChild(0))->GetSurface(0)->CountVertices(); i++)
			{
				Vec3 vp=((Model *)model->GetChild(0))->GetSurface(0)->GetVertexPosition(i);
				Vec3 vg = Transform::Point(vp,model,NULL);

				printf("local vertex %d: %f, %f, %f\n",i,vp.x,vp.y,vp.z);
				printf("global vertex : %f, %f, %f\n\n",vg.x,vg.y,vg.z);
			}
		}

		Leadwerks::Time::Update();
		world->Update();
		world->Render();
		context->Sync();
	}
    return 0;
}

And a screenshot:

ground.jpg.d3346d6082d8c563bd6f4ac978f316aa.jpg

The created white box is at global 0,0,0, as is supposed to be the vertex it's over.  (If I move the box to the right one meter, it will be on the other "grid" and over the next vertex.)  But the following is the output.

Quote

local vertex 0: -1.500000, 0.500000, -0.000000
global vertex : -1.500000, 0.500000, 0.000000

local vertex 1: -1.500000, -0.500000, -0.000000
global vertex : -1.500000, -0.500000, -0.000000

local vertex 2: -0.500000, 0.500000, -0.000000
global vertex : -0.500000, 0.500000, 0.000000

local vertex 3: -0.500000, -0.500000, -0.000000
global vertex : -0.500000, -0.500000, -0.000000

local vertex 4: 0.500000, 0.500000, -0.000000
global vertex : 0.500000, 0.500000, 0.000000

local vertex 5: 0.500000, -0.500000, -0.000000
global vertex : 0.500000, -0.500000, -0.000000

local vertex 6: 1.500000, 0.500000, -0.000000
global vertex : 1.500000, 0.500000, 0.000000

local vertex 7: 1.500000, -0.500000, -0.000000
global vertex : 1.500000, -0.500000, -0.000000

The global vertices are supposed to be at

top row: (-1, 0, 0) (0, 0, 0) (1, 0, 0) (2, 0, 0)

bottom row: (-1, -1, 0) (0, -1, 0) (1, -1, 0) (2, -1, 0)

Models.zip

Link to comment
Share on other sites

  • Solution

Change this:

Vec3 vg = Transform::Point(vp, model, NULL);

To this:

Vec3 vg = Transform::Point(vp, model->GetChild(0), NULL);

Vertex positions are relative to the model they are part of.

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Related followup questions:

Say I move 100 vertices.  When do I call the following two functions?

((Model *)groundmodel->GetChild(0))->GetSurface(0)->UpdateAABB();

groundmodel->UpdateAABB(Entity::LocalAABB | Entity::GlobalAABB);

1.  Is it OK to only call both of those at the end or do either of those need to be called after every SetVertexPosition position call?

2.  Are both calls above correct?  In other words, does the first one need to include GetChild and the second one doesn't?

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