I'm trying to make a shooting tank in OpenGL on C++.
I've drawn the tank and he moves on sin shaped ground, and I want to keep the end of the cannon in variables named currX, currY. My display function is:
void display()
{
double dx, dy, beta;
glClear(GL_COLOR_BUFFER_BIT); // clean frame buffer
// DrawSky();
DrawGround(); // draws a y = 0.075 * sin(x * 10) shaped ground
glPushMatrix();
// rotation of 2*PI equals 2*PI*Radius of Wheel
// in our case the wheel is rotated each time by angle (in degrees)
dx = direction * 2 * PI * 0.03 * (angle / 360);
dy = 0.075 * sin(dx * 10);
beta = atan(0.075 * 10 * cos(dx * 10)); // derrivative
beta *= 180 / PI; // transforms beta to degrees
glTranslated(dx, dy, 0);
if (direction == 1)
{
glRotated(180, 0, 1, 0);
beta = -beta;
}
glScaled(0.3, 0.3, 1);
glRotated(beta, 0, 0, 1);
DrawTank();
glPopMatrix();
//....
}
Now the problem is if I know that the tank cannon end was P = (-0.285,0.4175) before it was scaled and translated what will be the x,y of the cannon end at the end?
I've tried to multiply P by 0.3 which is the scaling factor in both x,y axis and its close to the end but not exactly there. What should I do?
should I calculate the currX,currY in the DrawTank or in the display?
in the image below the white point should be located at the end of the cannon.
Related
I have three points, lets say C as a center point while P1 and P2 are two other points.
I have computed angle between C and P1 know as angle1 and c p2 called angle 2.
here is the code I use to compute it
angle1 = atan2(p1.y - c.y, p1.x - c.x);
angle2 = atan2(p2.y - c.y, p2.x - c.x);
after that I changed them into degrees by using this.
if (angle1 >= 0)
angle1 = angle1 * (180 / PI);
else
angle1 = (angle1 + 2 * PI) * (180 / PI);
if(angle2 >= 0)
angle2 = angle2 * (180 / PI);
else
angle2 = (angle2 + 2 * PI) * (180 / PI);
Then I use this openCv method to compute draw the arc, some time arc is perfect between two point while some times it fills all the circle other than the two points which I post image here.
radius = sqrt(pow(c.x - p1.x, 2.0) + pow(c.y - p1.y, 2.0));
ellipse(outPutMat, c, cv::Size(radius, radius), 0, angle1, angle2, Scalar(0, 0, 0), -1, 8, 0);
here are the images
red points are the points, while black is ellipse filled color.
For 1st image agngle1 = 42.1376 while angle2 = 338.962
For 2nd image agngle1 = 152.447 while angle2 = 223.363
2nd image produced right results but first is wrong. I just want to fill area between points.
After short check - it seems that OpenCV function calculates middle angle as ma = (angle1 + angle2) / 2 and draws arc through this point.
Both (-45,45) and (45,-45) give the same 90-degrees arc through zero, both (315,45) and (45,315) give the same 270-degrees arc.
To get desired result, you have not map negative angle to positive value ((angle1 + 2 * PI)), and use 42 and -21 values in the first case.
I have a program that reads a 360 mono panorama and reads an IMU, drawing the correct part of the panorama based on the head location.
I am creating two windows, one per display, and do not want to rely on GLUT_STEREO. The draw() calls for each display are therefore independent, but right now they render the same thing, which is a gluSphere to represent the panorama. To draw the correct part of the sphere, IMU data (quaternion) becomes a rotation matrix, and that matrix is multiplied with the projection.
I wish to create a little bit of overlap with the two images, as shown with the following image:
For example, the red rectangle is my left display and the blue rectangle is my right display, but there is some overlap in the middle.
I was reading some article about stereo rendering, and I thought the solution would be to replace the call from gluPerspective() to glFrustum(), and simply modify both the left and right parameter at the same time. I thought subtracting some value to left/right parameter of glFrustum() on the display and adding some value to the left/right parameter of glFrustum() would do the trick. I modified the glutReshapeFunc() callback's projection matrix to do just that:
void resize(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble near = 0.1;
GLdouble far = 100.0;
GLdouble aspect = (double) width / (double) height;
GLdouble top = tan(FOVY / 360 * M_PI) * near;
GLdouble bottom = -top;
GLdouble right = top * aspect;
GLdouble left = -right;
// TODO: Canned value for testing
left += 0.5;
right += 0.5;
glFrustum(left, right, bottom, top, near, far);
// gluPerspective(FOVY, aspect, near, far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Unfortunately, this does not do what I expect (and I am really not sure why). I would think modifying both left and right parameter of glFrustum() would keep the same horizontal FOV but move it to the left or right. It seems to either stretch the image in or out.
I have played around with glTranslatef() on the ModelView or glLookAt(), but there place are not clear to me. Why is glFrustum() not having the right behavior please, and what am I missing?
Modify the frustum and the camera.
You need need two different camera matrices to simulate the eye separation and slightly different frustums to eliminate toe-in.
3D Stereo Rendering
Using OpenGL (and GLUT):
/* Misc stuff */
ratio = camera.screenwidth / (double)camera.screenheight;
radians = DTOR * camera.aperture / 2;
wd2 = near * tan(radians);
ndfl = near / camera.focallength;
/* Derive the two eye positions */
CROSSPROD(camera.vd,camera.vu,r);
Normalise(&r);
r.x *= camera.eyesep / 2.0;
r.y *= camera.eyesep / 2.0;
r.z *= camera.eyesep / 2.0;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
left = - ratio * wd2 - 0.5 * camera.eyesep * ndfl;
right = ratio * wd2 - 0.5 * camera.eyesep * ndfl;
top = wd2;
bottom = - wd2;
glFrustum(left,right,bottom,top,near,far);
glMatrixMode(GL_MODELVIEW);
glDrawBuffer(GL_BACK_RIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(camera.vp.x + r.x,camera.vp.y + r.y,camera.vp.z + r.z,
camera.vp.x + r.x + camera.vd.x,
camera.vp.y + r.y + camera.vd.y,
camera.vp.z + r.z + camera.vd.z,
camera.vu.x,camera.vu.y,camera.vu.z);
MakeLighting();
MakeGeometry();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
left = - ratio * wd2 + 0.5 * camera.eyesep * ndfl;
right = ratio * wd2 + 0.5 * camera.eyesep * ndfl;
top = wd2;
bottom = - wd2;
glFrustum(left,right,bottom,top,near,far);
glMatrixMode(GL_MODELVIEW);
glDrawBuffer(GL_BACK_LEFT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(camera.vp.x - r.x,camera.vp.y - r.y,camera.vp.z - r.z,
camera.vp.x - r.x + camera.vd.x,
camera.vp.y - r.y + camera.vd.y,
camera.vp.z - r.z + camera.vd.z,
camera.vu.x,camera.vu.y,camera.vu.z);
MakeLighting();
MakeGeometry();
glutSwapBuffers();
Replace the glDrawBuffer() calls with appropriate FBO binds.
I'm using C++, Allegro5, and codeblocks. I'm trying to write a test program before I write a very basic Asteroids knock-off. The only rules are use C++, Allegro, and no sprites or bitmaps (this specifically has to use primitives like lines and shapes).
I'm drawing the shape in quadrant1, redrawing it in quadrant2 translated from a relative origin, redrawing it in three rotated by certain number of degrees, and finally drawing it rescaled in quadrant4.
Parts 1 2 and 4 work fine. My problem is the rotate function. It is not rotating the correct amount, or not rotating at all. At less than 90 it rotates half of the entered degrees. After that it doesn't rotate or doesnt draw at all.
I'll post rotation method code below. I know it's bloated and needs to be cleaned up (I just overhauled some of the logic to fix 1, 2, and 4), but I really need help with rotation.
I think I'm using atan2 correctly and I've turned
void rotation (float degrees)
{
ALLEGRO_COLOR color_black = al_map_rgb(0,0,0);
ALLEGRO_COLOR color_blue = al_map_rgb(150,150,150);
ALLEGRO_COLOR color_orange = al_map_rgb(255,135,135);
ALLEGRO_COLOR color_red = al_map_rgb(255,0,0);
al_clear_to_color(al_map_rgb(255,255,255)); //clear screen to white
//draw black grid
al_draw_line(400,0, 400,600, color_black, 4.0);
al_draw_line(0,300, 800,300, color_black, 4.0);
al_draw_line(200,0, 200,600, color_black, 1.0);
al_draw_line(600,0, 600,600, color_black, 1.0);
al_draw_line(0,150, 800,150, color_black, 1.0);
al_draw_line(0,450, 800,450, color_black, 1.0);
float rx[13], ry[13];
float phi, theta, radius; //phi original angle theta added angle of rotation
float ycenter = 0, xcenter = 0; //later will be used with varying object position
theta = degrees * PI/180.0;
for (int i = 0; i < 13; i++)
{
phi = atan2(yarray[i], xarray[i]);
radius = sqrt(pow((xarray[i]-xcenter),2.0) + pow((yarray[i]-ycenter),2.0));
rx[i] = xarray[i] + radius * cos(phi + theta);
ry[i] = yarray[i] + radius * sin(phi + theta);
}
float x = quad[2][0];
float y = quad[2][1];
al_draw_triangle(x+rx[0],y+ry[0], x+rx[1],y+ry[1], x+rx[2],y+ry[2], color_red, 10);
al_draw_filled_triangle(x+rx[3],y+ry[3], x+rx[4],y+ry[4], x+rx[5],y+ry[5], color_orange);
al_draw_filled_triangle(x+rx[6],y+ry[6], x+rx[7],y+ry[7], x+rx[8],y+ry[8], color_blue); //body
al_draw_filled_triangle(x+rx[9],y+ry[9], x+rx[10],y+ry[10], x+rx[11],y+ry[11],color_blue); //wing
al_draw_filled_ellipse(x+rx[12],y+ry[12], 4, 8, color_black); //cockpit
al_flip_display();
}
I found teh answser after working it all out with paper and LOTS of print statements. I had to remove the xarray and yarray addition for rx and ry. See below.
rx[i] = radius * cos(phi + theta);
ry[i] = radius * sin(phi + theta);
replaces
rx[i] = xarray[i] + radius * cos(phi + theta);
ry[i] = yarray[i] + radius * sin(phi + theta);
So I'm trying to figure out how to mannually create a camera class that creates a local frame for camera transformations. I've created a player object based on OpenGL SuperBible's GLFrame class.
I got keyboard keys mapped to the MoveUp, MoveRight and MoveForward functions and the horizontal and vertical mouse movements are mapped to the xRot variable and rotateLocalY function. This is done to create a FPS style camera.
The problem however is in the RotateLocalY. Translation works fine and so does the vertical mouse movement but the horizontal movement scales all my objects down or up in a weird way. Besides the scaling, the rotation also seems to restrict itself to 180 degrees and rotates around the world origin (0.0) instead of my player's local position.
I figured that the scaling had something to do with normalizing vectors but the GLframe class (which I used for reference) never normalized any vectors and that class works just fine. Normalizing most of my vectors only solved the scaling and all the other problems were still there so I'm figuring one piece of code is causing all these problems?
I can't seem to figure out where the problem lies, I'll post all the appropriate code here and a screenshot to show the scaling.
Player object
Player::Player()
{
location[0] = 0.0f; location[1] = 0.0f; location[2] = 0.0f;
up[0] = 0.0f; up[1] = 1.0f; up[2] = 0.0f;
forward[0] = 0.0f; forward[1] = 0.0f; forward[2] = -1.0f;
}
// Does all the camera transformation. Should be called before scene rendering!
void Player::ApplyTransform()
{
M3DMatrix44f cameraMatrix;
this->getTransformationMatrix(cameraMatrix);
glRotatef(xAngle, 1.0f, 0.0f, 0.0f);
glMultMatrixf(cameraMatrix);
}
void Player::MoveForward(GLfloat delta)
{
location[0] += forward[0] * delta;
location[1] += forward[1] * delta;
location[2] += forward[2] * delta;
}
void Player::MoveUp(GLfloat delta)
{
location[0] += up[0] * delta;
location[1] += up[1] * delta;
location[2] += up[2] * delta;
}
void Player::MoveRight(GLfloat delta)
{
// Get X axis vector first via cross product
M3DVector3f xAxis;
m3dCrossProduct(xAxis, up, forward);
location[0] += xAxis[0] * delta;
location[1] += xAxis[1] * delta;
location[2] += xAxis[2] * delta;
}
void Player::RotateLocalY(GLfloat angle)
{
// Calculate a rotation matrix first
M3DMatrix44f rotationMatrix;
// Rotate around the up vector
m3dRotationMatrix44(rotationMatrix, angle, up[0], up[1], up[2]); // Use up vector to get correct rotations even with multiple rotations used.
// Get new forward vector out of the rotation matrix
M3DVector3f newForward;
newForward[0] = rotationMatrix[0] * forward[0] + rotationMatrix[4] * forward[1] + rotationMatrix[8] * forward[2];
newForward[1] = rotationMatrix[1] * forward[1] + rotationMatrix[5] * forward[1] + rotationMatrix[9] * forward[2];
newForward[2] = rotationMatrix[2] * forward[2] + rotationMatrix[6] * forward[1] + rotationMatrix[10] * forward[2];
m3dCopyVector3(forward, newForward);
}
void Player::getTransformationMatrix(M3DMatrix44f matrix)
{
// Get Z axis (Z axis is reversed with camera transformations)
M3DVector3f zAxis;
zAxis[0] = -forward[0];
zAxis[1] = -forward[1];
zAxis[2] = -forward[2];
// Get X axis
M3DVector3f xAxis;
m3dCrossProduct(xAxis, up, zAxis);
// Fill in X column in transformation matrix
m3dSetMatrixColumn44(matrix, xAxis, 0); // first column
matrix[3] = 0.0f; // Set 4th value to 0
// Fill in the Y column
m3dSetMatrixColumn44(matrix, up, 1); // 2nd column
matrix[7] = 0.0f;
// Fill in the Z column
m3dSetMatrixColumn44(matrix, zAxis, 2); // 3rd column
matrix[11] = 0.0f;
// Do the translation
M3DVector3f negativeLocation; // Required for camera transform (right handed OpenGL system. Looking down negative Z axis)
negativeLocation[0] = -location[0];
negativeLocation[1] = -location[1];
negativeLocation[2] = -location[2];
m3dSetMatrixColumn44(matrix, negativeLocation, 3); // 4th column
matrix[15] = 1.0f;
}
Player object header
class Player
{
public:
//////////////////////////////////////
// Variables
M3DVector3f location;
M3DVector3f up;
M3DVector3f forward;
GLfloat xAngle; // Used for FPS divided X angle rotation (can't combine yaw and pitch since we'll also get a Roll which we don't want for FPS)
/////////////////////////////////////
// Functions
Player();
void ApplyTransform();
void MoveForward(GLfloat delta);
void MoveUp(GLfloat delta);
void MoveRight(GLfloat delta);
void RotateLocalY(GLfloat angle); // Only need rotation on local axis for FPS camera style. Then a translation on world X axis. (done in apply transform)
private:
void getTransformationMatrix(M3DMatrix44f matrix);
};
Applying transformations
// Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Apply camera transforms
player.ApplyTransform();
// Set up lights
...
// Use shaders
...
// Render the scene
RenderScene();
// Do post rendering operations
glutSwapBuffers();
and mouse
float mouseSensitivity = 500.0f;
float horizontal = (width / 2) - mouseX;
float vertical = (height / 2) - mouseY;
horizontal /= mouseSensitivity;
vertical /= (mouseSensitivity / 25);
player.xAngle += -vertical;
player.RotateLocalY(horizontal);
glutWarpPointer((width / 2), (height / 2));
Honestly I think you are taking a way to complicated approach to your problem. There are many ways to create a camera. My favorite is using a R3-Vector and a Quaternion, but you could also work with a R3-Vector and two floats (pitch and yaw).
The setup with two angles is simple:
glLoadIdentity();
glTranslatef(-pos[0], -pos[1], -pos[2]);
glRotatef(-yaw, 0.0f, 0.0f, 1.0f);
glRotatef(-pitch, 0.0f, 1.0f, 0.0f);
The tricky part now is moving the camera. You must do something along the lines of:
flaot ds = speed * dt;
position += tranform_y(pich, tranform_z(yaw, Vector3(ds, 0, 0)));
How to do the transforms, I would have to look that up, but you could to it by using a rotation matrix
Rotation is trivial, just add or subtract from the pitch and yaw values.
I like using a quaternion for the orientation because it is general and thus you have a camera (any entity that is) that independent of any movement scheme. In this case you have a camera that looks like so:
class Camera
{
public:
// lots of stuff omitted
void setup();
void move_local(Vector3f value);
void rotate(float dy, float dz);
private:
mx::Vector3f position;
mx::Quaternionf orientation;
};
Then the setup code uses shamelessly gluLookAt; you could make a transformation matrix out of it, but I never got it to work right.
void Camera::setup()
{
// projection related stuff
mx::Vector3f eye = position;
mx::Vector3f forward = mx::transform(orientation, mx::Vector3f(1, 0, 0));
mx::Vector3f center = eye + forward;
mx::Vector3f up = mx::transform(orientation, mx::Vector3f(0, 0, 1));
gluLookAt(eye(0), eye(1), eye(2), center(0), center(1), center(2), up(0), up(1), up(2));
}
Moving the camera in local frame is also simple:
void Camera::move_local(Vector3f value)
{
position += mx::transform(orientation, value);
}
The rotation is also straight forward.
void Camera::rotate(float dy, float dz)
{
mx::Quaternionf o = orientation;
o = mx::axis_angle_to_quaternion(horizontal, mx::Vector3f(0, 0, 1)) * o;
o = o * mx::axis_angle_to_quaternion(vertical, mx::Vector3f(0, 1, 0));
orientation = o;
}
(Shameless plug):
If you are asking what math library I use, it is mathex. I wrote it...
I have defined 2 points on the surface of a sphere using spherical coordinates.
// define end point positions
float theta_point_1 = (5/10.0)*M_PI;
float phi_point_1 = (5/10.0)*2*M_PI;
float x_point_1 = Radius * sin(theta_point_1) * cos(phi_point_1);
float y_point_1 = Radius * sin(theta_point_1) * sin(phi_point_1);
float z_point_1 = Radius * cos(theta_point_1);
float theta_point_2 = (7/10.0)*M_PI;
float phi_point_2 = (1/10.0)*2*M_PI;
float x_point_2 = Radius * sin(theta_point_2) * cos(phi_point_2);
float y_point_2 = Radius * sin(theta_point_2) * sin(phi_point_2);
float z_point_2 = Radius * cos(theta_point_2);
// draw end points
void end_points ()
{
glColor3f (1.0, 1.0, 1.0);
glPointSize(25.0);
glBegin(GL_POINTS);
glVertex3f(x_point_1,y_point_1,z_point_1);
glVertex3f(x_point_2,y_point_2,z_point_2);
glEnd();
}
To step between the two points, I do the following:
find the difference between theta_points_1,2 and phi_points_1,2
divide the differences by 'n' (yielding 's')
redraw 'n' times, while stepping up the theta and phi by 's' each time
In the following, I've defined the differences between my theta and phi values, divided them, and then redraw them.
// begining spherical coords
float theta_point_1_value=5;
float phi_point_1_value=5;
// ending sperical coords
float theta_point_2_value=7;
float phi_point_2_value=1;
// dividing the difference evenly
float step_points=30;
float step_theta = 2/step_points;
float step_phi = 4/step_points;
// step between spherical coordinates
void stepping_points ()
{
glColor3f (1.0, 0.0, 0.0);
for (int i = 1; i < step_points; i++)
{
float theta = (theta_point_1_value/10.0)*M_PI;
float phi = (phi_point_1_value/10.0)*2*M_PI;
float x = Radius * sin(theta) * cos(phi);
float y = Radius * sin(theta) * sin(phi);
float z = Radius * cos(theta);
glPushMatrix();
glTranslatef(x,y,z);
glutSolidSphere (0.05,10,10);
glPopMatrix();
}
glEnd();
}
Now I understand, this displays 30 solid spheres at the same position. Because I have NOT included 'step_theta' or 'step_phi' in any of the redraws.
And that is the root of my question. How do I employ 'step_theta' and 'step_phi' in my redraws?
What I want to do is say something like this at the top of my 'for' loop:
for (int i = 1; i < step_points; i++)
{
float theta_point_1_value = (theta_point_1_value+step_theta);
float phi_point_1_value = (phi_point_1_value+step_phi);
float theta = (theta_point_1_value/10.0)*M_PI;
float phi = (phi_point_1_value/10.0)*2*M_PI;
float x = Radius * sin(theta) * cos(phi);
float y = Radius * sin(theta) * sin(phi);
float z = Radius * cos(theta);
glPushMatrix();
glTranslatef(x,y,z);
glutSolidSphere (0.05,10,10);
glPopMatrix();
}
The above will redraw 30 solid spheres, but they don't show between my defined end points. It's pretty clear that either my math or syntax is screwy (or more than likely, both are).
Hint: What is the range of your loop variable, i? What do you want the range of your step_theta and step_phi to be?
When you declare a variable inside the loop, it goes out of scope and is destructed after every iteration. As such, only the value of i changes between your loop iterations.
Also: Consider using a vector/point class. (x_point_1, y_point_1) is not C++ :).
If you want consistent timing regardless of frame rate, you need to track the passage of time and use that to control how far you interpolate between the two points. Remember the start time and calculate the desired end time, then each frame, calculate (float)(now-start)/(end-start). This will give you a value between 0.0 and 1.0. Multiply that value by the delta of each spherical coordinate and add their start angles and you'll get what angles you need to be at now.