OpenGL semicircular rotation and quarter circle - c++

The following code will create a new semicircular using openGL:
glPointSize(2);
glBegin(GL_POINTS);
for (float i = p; i <= (2 * p); i += 0.001) {
x = 50.0 * cos(i) + 20;
y = 50.0 * sin(i) + 20;
glVertex2f(x, y);
}
glEnd();
Now I want to know how to change this code and create a quarter circle using this format?
Also, is this possible to rotate the shape using this format and without of using GLrotateEF?

Here i is the angle in radians. The loop goes from π to 2π—this is a semicircle (but the second half of it - a bit ill-defined).
To turn it into a quarter circle loop from 0 to π/2. To rotate the circle by x degrees, convert it to radians and add to the angle.
Alternatively you can draw a portion of a circle by simply defining where it should start, and where it should stop, in angles:
double start_angle = 30;
double stop_angle = 30 + 180;
for (double a = start_angle; a <= stop_angle; a += 0.001) {
double a_rad = a / 180.0 * p;
x = 50.0 * cos(a_rad) + 20;
y = 50.0 * sin(a_rad) + 20;
glVertex2f(x, y);
}

Related

SDL2, rotating a triangle about it's origin continuously turns it into a point?

How I calculated the rotation: I've calculated the origin of the triangle by averaging the sum of its x and y coordinates and then subtracting it from the vertices. I rotate the new vertices and add back the origin to get the newly transformed points.
But the problem with this is that if I continue to rotate it about its origin (in a full circle), the size of the triangle slowly decreases and eventually becomes a point on the screen at the origin of the triangle. I want to be able to rotate it without it turning into a point.
void sprite::rotate()
{
rp(x0, y0);//first set of coord. and so on
rp(x1, y1);
rp(x2, y2);
}
void sprite::rp(float& x, float& y)
{
//calculate center of triangle
float ox = (x0 + x1 + x2) / 3;
float oy = (y0 + y1 + y2) / 3;
//subtract center from points
x -= ox;
y -= oy;
//rotate about center
float prevX = x;
double angleInRadians = angle * (PI / 180.0);
x = cos(angleInRadians) * x - sin(angleInRadians) * y;
y = sin(angleInRadians) * prevX + cos(angleInRadians) * y;
//add center back
x += ox;
y += oy;
}

Rotation: Quaternion to matrix

I am trying to display a 360 panorama using an IMU for head tracking.
Yaw works correctly but the roll and pitch are reverse. I also notice that the pitch contains some roll (and maybe vice-versa).
I am receiving (W, X, Y, Z) coordinate from the IMU that I am storing in an array as X, Y, Z, W.
The next step is converting the quaternion to a rotation matrix. I have looked at many examples, and can't seem to find anything wrong with the following code:
static GLfloat rotation[16];
// Quaternion (x, y, z, w)
static void quaternionToRotation(float* quaternion)
{
// Normalize quaternion
float magnitude = sqrt(quaternion[0] * quaternion[0] +
quaternion[1] * quaternion[1] +
quaternion[2] * quaternion[2] +
quaternion[3] * quaternion[3]);
for (int i = 0; i < 4; ++i)
{
quaternion[i] /= magnitude;
}
double xx = quaternion[0] * quaternion[0], xy = quaternion[0] * quaternion[1],
xz = quaternion[0] * quaternion[2], xw = quaternion[0] * quaternion[3];
double yy = quaternion[1] * quaternion[1], yz = quaternion[1] * quaternion[2],
yw = quaternion[1] * quaternion[3];
double zz = quaternion[2] * quaternion[2], zw = quaternion[2] * quaternion[3];
// Column major order
rotation[0] = 1.0f - 2.0f * (yy + zz);
rotation[1] = 2.0f * (xy - zw);
rotation[2] = 2.0f * (xz + yw);
rotation[3] = 0;
rotation[4] = 2.0f * (xy + zw);
rotation[5] = 1.0f - 2.0f * (xx + zz);
rotation[6] = 2.0f * (yz - xw);
rotation[7] = 0;
rotation[8] = 2.0f * (xz - yw);
rotation[9] = 2.0f * (yz + xw);
rotation[10] = 1.0f - 2.0f * (xx + yy);
rotation[11] = 0;
rotation[12] = 0;
rotation[13] = 0;
rotation[14] = 0;
rotation[15] = 1;
}
The rotation matrix is then used in the draw call as such:
static void draw()
{
// Get IMU quaternion
float* quaternion = tracker.getTrackingData();
if (quaternion != NULL)
{
quaternionToRotation(quaternion);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
// TODO: Multiply initialRotation quaternion with IMU quaternion
glMultMatrixf(initialRotation); // Initial rotation to point forward
glMultMatrixf(rotation); // Rotation based on IMU
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
gluSphere(quad, 0.1, 50, 50);
glBindTexture(GL_TEXTURE_2D, 0);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
I tried to set all but one fields in the quaternion to 0, and I notice that they all work individually, except roll and pitch is swapped around. I tried swapping X and Y but this does not seem to help.
Any help would be really appreciated. Please let me know as well if you have any steps that can let me debug my issue. Thanks!

How to draw a tapered arc (curve with decreasing thickness) in OpenGL?

I have the following code to draw an arbitrary arc:
void CenteredArc::drawPolygonArc(float radius, float thickness, float startAngle, float arcAngle) {
float num_segments = 360.0;
float radiusOuter = radius + thickness / 2;
float radiusInner = radius - thickness / 2;
float theta = arcAngle / num_segments;
float tangetial_factor = tanf(theta);//calculate the tangential factor
float radial_factor = cosf(theta);//calculate the radial factor
float xxOuter = radiusOuter * cosf(startAngle);
float yyOuter = radiusOuter * sinf(startAngle);
float xxInner = radiusInner * cosf(startAngle);
float yyInner = radiusInner * sinf(startAngle);
float prevXXOuter = -1;
float prevYYOuter = -1;
float prevXXInner = -1;
float prevYYInner = -1;
glPolygonMode(GL_FRONT, GL_FILL);
for(int ii = 0; ii < num_segments; ii++)
{
if (prevXXOuter != -1) {
glBegin(GL_POLYGON);
glVertex2f(prevXXOuter, prevYYOuter);
glVertex2f(xxOuter, yyOuter);
glVertex2f(xxInner, yyInner);
glVertex2f(prevXXInner, prevYYInner);
glEnd();
}
//calculate the tangential vector
//remember, the radial vector is (x, y)
//to get the tangential vector we flip those coordinates and negate one of them
float txOuter = -yyOuter;
float tyOuter = xxOuter;
float txInner = -yyInner;
float tyInner = xxInner;
//add the tangential vector
prevXXOuter = xxOuter;
prevYYOuter = yyOuter;
prevXXInner = xxInner;
prevYYInner = yyInner;
xxOuter += txOuter * tangetial_factor;
yyOuter += tyOuter * tangetial_factor;
xxInner += txInner * tangetial_factor;
yyInner += tyInner * tangetial_factor;
//correct using the radial factor
xxOuter *= radial_factor;
yyOuter *= radial_factor;
xxInner *= radial_factor;
yyInner *= radial_factor;
}
}
However, I would like for the arc to start off with the specified thickness on one end and gradually decrease to a thickness of zero on the other end. Any suggestions?
Edit: I am not using GL_LINE_STRIP because I am trying to avoid having overlapping lines and gaps like so:
I would use a line strip with decreasing glLineWidth.
This is my implementation, it doesn't gradially reduce the lineWidth but it could be modified to do so. Sorry for the extra stuff, it's from my game engine.
for(int i=0;i<arcs().size();i++)
{
Entities::Arc temp = arcs().at(i);
glLineWidth(temp.LW.value); // change LWidth
glColor3f( temp.CL.R, temp.CL.G, temp.CL.B );
// theta is now calculated from the arc angle instead, the
// - 1 part comes from the fact that the arc is open
float theta = temp.A.value*DEG2RAD / float(WW_SPLINE_ACCURACY - 1);
float tan = tanf(theta);
float cos = cosf(theta);
// we are now at the starting angle
double x = temp.R.value * cosf(temp.A.value*DEG2RAD);
double y = temp.R.value * sinf(temp.A.value*DEG2RAD);
// since the arc is not a closed curve, this is a strip now
glBegin(GL_LINE_STRIP);
for(int ii = 0; ii < WW_SPLINE_ACCURACY; ii++)
{
glVertex2d(x + temp.C.X, y + temp.C.Y);
double tx = -y;
double ty = x;
x += tx * tan;
y += ty * tan;
x *= cos;
y *= cos; //y = ( y + (ty*tan) )*cos;
}
glEnd();
glLineWidth(WW_DEFAULT_LWIDTH); // reset LWidth
}
I also used these values
#define WW_SPLINE_ACCURACY 72 // 72 for extra smooth arcs/circles, 32 minimum
#define WW_BEZIER_ACCURACY 20
/* Math stuff */
#define DEG2RAD 3.14159/180
#define PI 3.1415926535897932384626433832795;
...
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable (GL_LINE_SMOOTH);
glEnable (GL_BLEND);
//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glEnable(GL_POLYGON_SMOOTH);
glClearColor(0.188f, 0.169f, 0.329f, 1.0f); //#302b54
I'm not allowed to release the full source since I wrote it for a company but sharing a part or two wont hurt anybody :D

OpenGL Camera rotation weirdness

I've been working on a small project with FreeGlut and Glew. Now I'm coding a camera system, but there are some things that are simply weird:
In fullscreen mode if the mouse moves in lower area of the screen, camera movements are faster than if camera moves in upper areas.
The camera makes weird movement, always in same direction, a small 8 figure move move.
code:
void MouseOps(int x, int y)
{
// Changes in mousepositions. Always same direction and
// in lower right corner of monitor faster, for some reason.
deltaX = x - MousePreviousX;
deltaY = y - MousePreviousY;
// Also I didn't bother to put * 360 in next equations,
// because it would make the camera jump for crazy.
// resx and resy are screen resolutions.
// Endresult should be that camera can
// rotate once when mouse moves over screen
yaw = yaw + (((deltaX / resx)) * deginrad);
pitch = pitch + (((deltaY / resy)) * deginrad);
//Check clippings (eg. camera wont end upside down etc.)
if(yaw >= (2 * pi) || yaw <= (-2 * pi) )
yaw = 0;
if(pitch >= (pi / 2))
pitch = pi / 2;
if(pitch <= (pi / -2))
pitch = pi / -2;
//Calculate x, y, and z coordinates of unit sphere to look at (r = 1)
cam_normX = cos(yaw) * sin(pitch);
cam_normY = sin(yaw) * sin(pitch);
cam_normZ = cos(yaw);
// Current x and y to previous
int MousePreviousX = x;
int MousePreviousY = y;
}
I tried to use this
http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates
system to calculate the point to look at. Then I passed all "cam_norm" variables to
gluLookAt(cam_posX, cam_posY, cam_posZ,
cam_posX+cam_normX, cam_posY+cam_normY, cam_posZ + cam_normZ,
cam_upX, cam_upY, cam_upZ);
I don't know why this works but it fixed all problems:
bool isCursorWarping = false;
void MouseOps(int x, int y)
{
if(isCursorWarping == false){
// Changes in mousepositions. Always same direction and in lower right corner of monitor faster, for some reason.
deltaX = x - MousePreviousX;
deltaY = y - MousePreviousY;
yaw = yaw + ((((deltaX / resx)) * deginrad) * 360);
pitch = pitch + ((((deltaY / resy)) * deginrad) * 360);
//Check clippings (eg. camera wont end upside down etc.)
if(x >= resx - 1 || y >= resy - 1 || x == 0 || y == 0)
{
warpCursor();
MousePreviousX = resx / 2;
MousePreviousY = resy / 2;
}else{
MousePreviousX = x;
MousePreviousY = y;
}
if(yaw >= (2 * pi) || yaw <= (-2 * pi) )
yaw = 0;
if(pitch >= (pi / 2))
pitch = pi / 2;
if(pitch <= (pi / -2))
pitch = pi / -2;
//Calculate x, y, and z coordinates of unit sphere to look at (r = 1)
cam_normX = cos(pitch) * cos(yaw);
cam_normY = sin(pitch) * sin(yaw);
cam_normZ = cos(pitch) * sin(yaw);
}
// Current x and y to previous and cleanup
isCursorWarping = false;
}
void warpCursor()
{
isCursorWarping = true;
glutWarpPointer(resx / 2, resy / 2);
}
Then I pass the "cam_norm" values to:
gluLookAt(0.0f, 1.0f, 2.0f, 0.0f + cam_normX, 1.0f + cam_normY, 2.0f+ cam_normZ, 0.0f, 0.1f, 0.0f);

Efficient way to draw Ellipse with OpenGL or D3D

There is a fast way to draw circle like this
void DrawCircle(float cx, float cy, float r, int num_segments)
{
float theta = 2 * 3.1415926 / float(num_segments);
float c = cosf(theta);//precalculate the sine and cosine
float s = sinf(theta);
float t;
float x = r;//we start at angle = 0
float y = 0;
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//apply the rotation matrix
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
}
I am wondering if there is a similar way to draw ellipse where its major/minor axes vector and size are both known.
If we take your example we can use an internal radius of 1 and apply horizontal/vertical radius separately in order to get an ellipse:
void DrawEllipse(float cx, float cy, float rx, float ry, int num_segments)
{
float theta = 2 * 3.1415926 / float(num_segments);
float c = cosf(theta);//precalculate the sine and cosine
float s = sinf(theta);
float t;
float x = 1;//we start at angle = 0
float y = 0;
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
//apply radius and offset
glVertex2f(x * rx + cx, y * ry + cy);//output vertex
//apply the rotation matrix
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
}
There is no way to draw a curve in openGL, just a lot of straight lines. But if you used vertex buffer objects then you won't have to send each vertex to the graphics card which will be much faster.
My Java Example
If the ellipse is ((x-cx)/a)^2 + ((y-cy)/b)^2 = 1 then change the glVertex2f call to
glVertext2d(a*x + cx, b*y + cy);
To simplify the sums, lets suppose for a while that the ellipse is centred at the origin.
If the ellipse is rotated so that the semi-major axis (of length a) makes an angle theta with the x axis, then the ellipse is the set of points p so that p' * inv(C) * p = 1, where C is the matrix R(theta) * D * R(theta)' where ' denotes transpose and D is the diagonal matrix with entries a*a,b*b (b the length of the semi-minor axis). If L is the cholesky factor (eg here) of C then the ellipse is the set of points p so that (inv(L) * p)'*(inv(L) *p ) = 1, so that L maps the unit circle to the ellipse. If we have computed L as ( u 0 ; v w) (just once, before the loop) then the glVertexf call becomes glVertex2f( u*x + cx, v*x + w*y + cy);
L can be calculated like this (where C is cos(theta) and S is sin(theta)):
u = sqrt( C*C*a*a + S*S*b*b); v = C*S*(a*a-b*b); w = a*b/u;