Jump to content

  • Log In with Google      Sign In   
  • Create Account


thewhiteaussie

Member Since 08 Aug 2012
Offline Last Active Aug 21 2013 04:33 PM

Topics I've Started

Is the concatenation of a VQS with its inverse commutative?

22 June 2013 - 04:41 PM

Quaternion concatenation is noncommutative. That is

 

qa * qb    qb * qa

 

However

 

q-1 * q = q * q-1 = Iq

 

where q-1 is the inverse of q and Iq is the identity quaternion. VQS concatenation is also noncommutative:

 

TA_B  * TB_C     TB_C * TA_B

 

Where TA_B represents a VQS transformation. Now, we find the inverse of TA_B like so:

 

TA_B-1 = TB_A

 

My question is, is the concatenation of a VQS with its inverse commutative? Ie, is the following statement correct?

 

TA_B * TB_A = TB_A * TA_B = IVQS

 

Where IVQS  is the identity VQS. With the implementation I’m using I’m finding

 

T -1 * T   =   IVQS, whereas

T * T -1      IVQS

 

This seems incorrect; both sould return IVQS.

 

 

EDIT:

 

Here is the implementation of VQS Inverse and concatenation functions I'm using:

//--------------------------------------------------------------------------------
//		Concatenation
//--------------------------------------------------------------------------------
VQS VQS::operator*(const VQS& rhs) const
{
	VQS result;

	//Combine translation vectors
	result.v = q.Rotate(rhs.v) * s + v;

	//Combine quaternions
	result.q = q * rhs.q;

	//Combine scales
	result.s = s * rhs.s;

	//Return result
	return result;

}	//End: VQS::operator*()


//--------------------------------------------------------------------------------
//		Returns inverse VQS
//--------------------------------------------------------------------------------
VQS Inverse(const VQS& other)
{
	VQS temp;

	//Inverse scale
	temp.s = 1.0f / other.s;

	//Inverse quaternion
	temp.q = Inverse(other.q);

	//Inverse vector
	temp.v = temp.q.Rotate(-other.v) * temp.s;

	return temp;

}	//End: Inverse()

World to Camera Transformation using Quaternions

12 January 2013 - 02:14 AM

I have a question regarding quaternions and rotations.

 

Let the quaternion q(w, x, y, z) represent the orientation of a camera in world space. I use a UVN camera system where u = right vector, v = up vector and n = forward vector. With zero rotation the camera aligns with the world basis vectors (x, y, z) like so:

 

u aligns with +x

v aligns with +z

n aligns with +y

 

My question is how do I construct a quaternion that rotates objects from the RH coordinate system of the world space to a LH coordinate system with the following alignments:

 

u aligns with +x

v aligns with +y

n aligns with +z

 

Previously I've use the matrix

 

[ ux   vx   nx ]

[ uy   vy   ny ]

[ uz   vz   nz ]

 

however want to switch to a completely quaternion based system.

 

I suspect I'll need q-1. This rotates objects into camera space, preserving handedness. To switch alignments I suspect I'll need to swap terms in the quaternion.

 

Thanks.


Triangle rasterization troubles

16 December 2012 - 11:14 PM

I'm trying to write a couple of functions to draw (filled) flat-top and flat-bottom triangles. For the large part they work however cracks are still sometimes seen between adjacent triangles. I use an interpolating technique whereby I raster the triangle line by line, calculating the new left and right limits at each step. Much of this is explained in the code, but here is the general idea (for a flat bottom):

1) Find dy (height)
2) Find dy/dx (slope) from the top point to each bottom point
3) Move to the starting row ( floor(top point) ), and find initial x-start and x-end coordinates
4) Interpolate down the triangle

I should note that cracks are only seen between triangles that join at the sides, not top/bottom joins. I've been at this so long I don't know what to try anymore. I think the logic is solid, maybe any cracks are due to floating point error. I'd really appreciate some feedback.

[source lang="cpp"]typedef unsigned int uint32;Line(uint32 y, uint32 x_left, uint32 x_right); //Draws a horizontal line at these coordsstruct vector2{ float x,y;};//--------------------------------------------------------------------------------// Draws a flat bottom triangle from top to bottom//--------------------------------------------------------------------------------void Draw_Bottom_Tri_SOLID(Vector2 p0, Vector2 p1, Vector2 p2){ //Point order: //Bottom left: p0 //Bottom right: p1 //Top point: p2 //calculate dy float dy = p2.y - p0.y; //dx/dy for the left and right edges float dxdy_left = (p0.x - p2.x)/dy; float dxdy_right = (p1.x - p2.x)/dy; //Since we start the raster process at floor(p2.y) //we need to shift the x start and x end postions along //by this factor: float y_bump = p2.y - floor(p2.y); //Initial start and end x values float xs = p2.x + dxdy_left*y_bump; //x left (start) float xe = p2.x + dxdy_right*y_bump; //x right (end) uint32 yb = uint32(p0.y) + 1; //y bottom, +1 for top left fill convention uint32 yt = uint32(p2.y); //y top, use casting instead of std::floor //Draw lines for (uint32 i = yt; i >= yb; i--) { //Set left and right limits, use casting instead of std::floor uint32 left = uint32(xs) + 1; //+1 for top left fill convention uint32 right = uint32(xe); //Draw line, can also be std::fill or simply a for loop. Line(i, left, right); //Increment limits xs += dxdy_left; xe += dxdy_right; }} //End: Draw_Bottom_Tri_SOLID()//--------------------------------------------------------------------------------// Draws a flat top triangle from bottom to top//--------------------------------------------------------------------------------void Draw_Top_Tri_SOLID(Vector2 p0, Vector2 p1, Vector2 p2){ //Point order: //Top left: p0 //Top right: p1 //Bottom point: p2 //calculate dy (height) float dy = p0.y - p2.y; //dx/dy for the left and right edges float dxdy_left = (p0.x - p2.x)/dy; float dxdy_right = (p1.x - p2.x)/dy; //Find shifting factor float y_bump = ceil(p2.y) - p2.y; //Initial start and end x values float xs = p2.x + dxdy_left*y_bump; //x left (start) float xe = p2.x + dxdy_right*y_bump; //x right (end) uint32 yb = uint32(p2.y) + 1; //y bottom, +1 for top left fill convention uint32 yt = uint32(p0.y) ; //y top //Draw lines for (uint32 i = yb; i <= yt; i++) { //Set left and right limits uint32 left = uint32(xs) + 1; //+1 for top left fill convention uint32 right = uint32(xe); //Draw line, can be std::fill or simply a for loop. Line(i, left, right); //Increment limits xs += dxdy_left; xe += dxdy_right; }} //End: Draw_Top_Tri_SOLID()[/source]

Clipping to frustum question.

12 November 2012 - 08:10 PM

Hello. I have a question regarding clipping a polygon against a view frustum.

For simplicity, consider the case of clipping a triangle to a rectangle. The Sutherland-Hodgman algorithm achieves this by clipping the triangle to each side of the rectangle successively. A new polygon must be created and stored after each clipping phase, to be sent on to be clipped against the next edge of the rectangle.

My question is this: can we take advantage of the recursive nature of the algorithm to avoid storing these (four) temporary polygons? Once a vertex has been clipped to one side, can we send it straight to the next clipping phase? Below is the logic I have developed thus far. It processes each line of the triangle separately by clipping against the boundaries in the following order:

[source lang="cpp"] Top -> Bottom -> Left -> Right -> Output[/source]

Points that pass the final clipping phase (Right) are added to the output polygon.

It is currently incorrect. It cannot 'wrap' a triangle around a corner of the rectangle. I think I'm on the right track though. I would love some feedback Posted Image

Here are the functions in pseudocode:
[source lang="cpp"]//Clip polygon master function. The clipping rectangle has previously been defined.void ClipPolygon(point p0, point p1, point p2){ ClipToTop(p0, p1); ClipToTop(p1, p2); ClipToTop(p2, p0);}//--------------------------------------------------------------------------------// Clip to top//--------------------------------------------------------------------------------ClipToTop(point previous, point current){ if (/*previous and current are both inside*/) { ClipToBottom(previous,current); return; } if (/*previous inside and current outside*/) { //find intersection: 'intersect' ClipToBottom(previous,intersect); return; } //If both outside: do nothing if (/*previous outside and current inside*/) { //find intersection: 'intersect' ClipToBottom(previous,intersect); ClipToBottom(intersect,current); return; }}//--------------------------------------------------------------------------------// Clip to bottom//--------------------------------------------------------------------------------ClipToBottom(point previous, point current){ if (/*previous and current are both inside*/) { ClipToLeft(previous,current); return; } if (/*previous inside and current outside*/) { //find intersection: 'intersect' ClipToLeft(previous,intersect); return; } //If both outside: do nothing if (/*previous outside and current inside*/) { //find intersection: 'intersect' ClipToLeft(previous,intersect); ClipToLeft(intersect,current); return; }}//--------------------------------------------------------------------------------// Clip to left//--------------------------------------------------------------------------------ClipToLeft(point previous, point current){ if (/*previous and current are both inside*/) { ClipToRight(previous,current); return; } if (/*previous inside and current outside*/) { //find intersection: 'intersect' ClipToRight(previous,intersect); return; } //If both outside: do nothing if (/*previous outside and current inside*/) { //find intersection: 'intersect' ClipToRight(previous,intersect); ClipToRight(intersect,current); return; }}//--------------------------------------------------------------------------------// Clip to Right//--------------------------------------------------------------------------------ClipToRight(point previous, point current){ if (/*previous and current are both inside*/) { //Add current to output polygon } if (/*previous inside and current outside*/) { //find intersection: 'intersect' //Add intersect to output polygon } //If both outside: do nothing if (/*previous outside and current inside*/) { //find intersection: 'intersect' //Add intersect to output polygon //Add current to output polygon }}[/source]

Q: Fast and simple way to store/manipulate pixels?

09 August 2012 - 12:03 AM

Hello.

I'm currently experimenting with writing simple 3D engines and so far isn't going too bad. I'm coding with C++ and using SDL. So far I've been using SDL's functions to generate surfaces and to manipulate pixels. However I would like to write my own class to represent surfaces, like so:

[source lang="cpp"]struct Color{ Uint8 red; Uint8 green; Uint8 blue; Uint8 alpha;};//Create a 1024*768 surfaceColor* Screen = new Color[1024*768];[/source]

I want to achieve easy and fast color manipulation, while removing much of the dependency of a specific rendering API. Using the above method I would simply need to convert 'Screen' to the Uint32 format SDL needs at the end of the game loop.

I guess I'd like to hear some thoughts on this idea before I proceed. I'm concerned this might not end up being a very efficient way of doing things, however at present it should be faster than converting a Uint32 pixel format to RGB data and then back again (what I'm doing now); I'm only doing one conversion during runtime. Also, is there a common, generic way to represent images that isn't API specific?

Frank

PARTNERS