I\'m writing a program in OpenGL to load a mesh and draw an oriented bounding box around said mesh. The mesh loads correctly but when I draw the bounding box the box is the
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)