Oriented Bounding Box is Misshapen and the Wrong Size in OpenGL

扶醉桌前 提交于 2019-11-29 17:17:26

The code looks right (I haven't checked all the functions like matrixmult and like that but I assume you have tested and checked them).

Problem: you have a small misunderstanding of what are you doing there.

So, to help you a bit, but not code your coursework by myself, as that would get us both into trouble, I decided to make a small tutorial in Matlab (as I know you have access to) of what you want to do and why. Some functions are missing, but you should be able to understand what's going on:

clear;clc;
%% your "bunny"
vtx=    [ 1 0
         1 1
         2 2
         3 3 
         1 3];

% Lets draw it  

hold on
plot(vtx(:,1),vtx(:,2),'.')

% lets draw XY axis also
plot([0 4],[0 0],'k')
plot([0 0],[0 4],'k')
axis([-1 5 -1 5])
axis square

%% Draw abb
maxX=max(vtx(:,1));
maxY=max(vtx(:,2));
minX=min(vtx(:,1));
minY=min(vtx(:,2));

% Mising: Create a square and draw it 
cub=createcube(maxX,maxY,minX,minY)
drawabb(cub);

%% Create obb

C=cov(vtx);
vtxmean=mean(vtx);

[eVect,~]=eig(C);

% Draw new local coord system
plot([vtxmean(1) vtxmean(1)+eVect(1,1)],[vtxmean(2) vtxmean(2)+eVect(1,2)],'k')
plot([vtxmean(1) vtxmean(1)+eVect(2,1)],[vtxmean(2) vtxmean(2)+eVect(2,2)],'k')

Now you can see that if we get the max and min of the eigenvector, it doesnt make too much sense. Thats NOT the obb. So what are we suposed to do? Well, we can create an abb, but alligned to the NEW axis, not the XY axis!

What would we need for that? well we need to know the values of our points in the new coordinate axis, dont we?

Localvtx=fromGlobalToLocal(vtx,eVect,vtxmean);

% get the max and min of the points IN THE NEW COORD SYSTEM!
maxX=max(Localvtx(:,1));
maxY=max(Localvtx(:,2));
minX=min(Localvtx(:,1));
minY=min(Localvtx(:,2));

Fantastic!!! Now , we can create a square in this coord system, and using fromLocalToGlobal, draw it in XY!!

obbcub=createcube(maxX,maxY,minX,minY);
obbcubXY=fromLocalToGlobal(obbcub,eVect,vtxmean);
drawcube(obbcubXY);

Logical question: WHY ARE WE DOING ALL THIS!?!?

Well its an interesting question indeed. Do you play videogames? Have you ever took "an arrow in the knee?". How does a computer know if you have shooted the guy in the head or in the leg with you sniper rifle if the guy was jumping and crouching and lying all the time!!

What about an oriented bounding box! If you know the bounding box of the leg, or the head, independently of the geometrical position of the model, you can compute if the shot went inside that box or not! (dont take everything literally, this is a huge world and there are infinite ways of doing things like this).

See example:

Sidenote: Dont use my words or image or code in you report, as that will be considered cheating! (just in case)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!