gamecreator Posted March 7, 2019 Share Posted March 7, 2019 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. Quote Link to comment Share on other sites More sharing options...
Josh Posted March 8, 2019 Share Posted March 8, 2019 I can’t tell without seeing the model creation code. If the model has the default orientation then local and global coords are the same. Quote 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 More sharing options...
gamecreator Posted March 8, 2019 Author Share Posted March 8, 2019 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: 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 Quote Link to comment Share on other sites More sharing options...
Solution Josh Posted March 9, 2019 Solution Share Posted March 9, 2019 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. 1 Quote 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 More sharing options...
gamecreator Posted March 9, 2019 Author Share Posted March 9, 2019 18 hours ago, Josh said: 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. Ah, of course. Simple mistake. Thank you Josh. 1 Quote Link to comment Share on other sites More sharing options...
gamecreator Posted March 10, 2019 Author Share Posted March 10, 2019 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.