void CylinderObject::calculate_matrices(float dt) { inv_radius = 1.0/radius; // acceleration at time dt Vector c_acc = acceleration * dt; // current pos pos.x = original_pos.x + c_acc.x; pos.y = original_pos.y + c_acc.y; pos.z = original_pos.z + c_acc.z; Point rot; rot.x = toRads(original_rot.x); rot.y = toRads(original_rot.y); rot.z = toRads(original_rot.z); //inverse translate matrix Matrix4x4 i_t; i_t.identity(); i_t[0][3] = -pos.x; i_t[1][3] = -pos.y; i_t[2][3] = -pos.z; //inverse rotation matrix Matrix4x4 i_rx, i_ry, i_rz; i_rx.identity(); i_rx[1][1] = cos(rot.x); i_rx[1][2] = sin(rot.x); i_rx[2][1] = -sin(rot.x); i_rx[2][2] = cos(rot.x); i_ry.identity(); i_ry[0][0] = cos(rot.y); i_ry[0][2] = -sin(rot.y); i_ry[2][0] = sin(rot.y); i_ry[2][2] = cos(rot.y); i_rz.identity(); i_rz[0][0] = cos(rot.z); i_rz[0][1] = sin(rot.z); i_rz[1][0] = -sin(rot.z); i_rz[1][1] = cos(rot.z); //inverse scale matrix Matrix4x4 i_s; i_s.identity(); i_s[0][0] = 1/original_scale.x; i_s[1][1] = 1/original_scale.y; i_s[2][2] = 1/original_scale.z; //object inverse transform matrix inv_trans = i_s * i_ry * i_rx * i_rz * i_t; }
// The display function. It is called whenever the window needs // redrawing (ie: overlapping window moves, resize, maximize) // You should redraw your polygons here void display(void) { // Clear the background glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (PERSPECTIVE) { glLoadIdentity(); //calculate carthesian coords from spherical coords c.x = rho*sin(toRads(phi))*cos(toRads(theta)); c.y = rho*sin(toRads(phi))*sin(toRads(theta)); c.z = rho*cos(toRads(phi)); //always looking at origin c.lx = c.ly = c.lz = 0.; //adjust up c.ux = cos(toRads(theta)); c.uy = sin(toRads(theta)); c.uz = 1.; // Set the camera position, orientation and target gluLookAt(c.x,c.y,c.z, c.lx,c.ly,c.lz, c.ux,c.uy,c.uz); //gluLookAt(0,5,5, 0,-2,0, 0,1,0); } if(AXIS){ // Draw a red line glColor3f(1,0,0); glBegin(GL_LINES); glVertex3f(0.0,0.0,0.0); glVertex3f(0.0,0.0,10.0); glEnd(); //Draw a blue Line glColor3f(0,0,1); glBegin(GL_LINES); glVertex3f(0.0,0.0,0.0); glVertex3f(0.0,10.0,0.0); glEnd(); // Draw a green line glColor3f(0,1,0); glBegin(GL_LINES); glVertex3f(10.0,0.0,0.0); glVertex3f(0.0,0.0,0.0); glEnd(); } if(Points) { glColor3f(1,0,0); glBegin(GL_POINTS); for(int i = 0; i < verts; i++) { glVertex3f(vertList[i].x, vertList[i].y, vertList[i].z); } glEnd(); } if(Lines) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); for(int i = 0; i < faces; i++) { glBegin(GL_QUADS); glColor3f(1.0f,0.0f,0.0f); //glVertex3f(faceList[i].v1, faceList[i].v2, faceList[i].v3); glVertex3f(vertList[faceList[i].v1].x, vertList[faceList[i].v1].y, vertList[faceList[i].v1].z); glColor3f(1.0f,0.0f,0.0f); glVertex3f(vertList[faceList[i].v2].x, vertList[faceList[i].v2].y, vertList[faceList[i].v2].z); glColor3f(1.0f,0.0f,0.0f); glVertex3f(vertList[faceList[i].v3].x, vertList[faceList[i].v3].y, vertList[faceList[i].v3].z); glVertex3f(vertList[faceList[i].v4].x, vertList[faceList[i].v4].y, vertList[faceList[i].v4].z); glEnd(); } } if(Solid) { glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); for(int i = 0; i < faces; i++) { glBegin(GL_TRIANGLE_FAN); glColor3f(1.0f,0.0f,0.0f); //glVertex3f(faceList[i].v1, faceList[i].v2, faceList[i].v3); glVertex3f(vertList[faceList[i].v1].x, vertList[faceList[i].v1].y, vertList[faceList[i].v1].z); glColor3f(1.0f,0.0f,0.0f); glVertex3f(vertList[faceList[i].v2].x, vertList[faceList[i].v2].y, vertList[faceList[i].v2].z); glColor3f(1.0f,0.0f,0.0f); glVertex3f(vertList[faceList[i].v3].x, vertList[faceList[i].v3].y, vertList[faceList[i].v3].z); glVertex3f(vertList[faceList[i].v4].x, vertList[faceList[i].v4].y, vertList[faceList[i].v4].z); glEnd(); } } // (Note that the origin is lower left corner) // (Note also that the window spans (0,1) ) // Finish drawing, update the frame buffer, and swap buffers glutSwapBuffers(); }
Screen::Screen(const CmdArgs &args) : _width(args.width()), _height(args.height()) { // Read the input file. std::string input = args.inputFilename(); std::ifstream in(input); stop_if(!in.is_open(), "failed to open input file (%s).", input.c_str()); Point camera, center; Vector up, right; float fovy; in >> camera.x >> camera.y >> camera.z; in >> center.x >> center.y >> center.z; in >> up.x >> up.y >> up.z; in >> fovy; // Camera direction to the center of the screen.. Vector dir = (center - camera).normalize(); // Make up orthogonal to the camera direction. up -= Vector::proj(dir, up); up.normalize(); // Calculate the right direction. right = Vector::cross(dir, up); // Calculate the width and height of the screen. float d = Point::distance(center, camera); _heightSize = 2 * tan(toRads(fovy / 2.0f)) * d; _widthSize = (_heightSize * _width) / _height; // Save the top left pixel. Point topLeft = center - (right * (_widthSize / 2)) + (up * (_heightSize / 2)); _topLeftPixelPos[0] = topLeft.x; _topLeftPixelPos[1] = topLeft.y; _topLeftPixelPos[2] = topLeft.z; _topLeftPixelPos[3] = 1.0f; _cameraPos[0] = camera.x; _cameraPos[1] = camera.y; _cameraPos[2] = camera.z; _cameraPos[3] = 1.0f; _upVector[0] = up.x; _upVector[1] = up.y; _upVector[2] = up.z; _upVector[3] = 0.0f; _rightVector[0] = right.x; _rightVector[1] = right.y; _rightVector[2] = right.z; _rightVector[3] = 0.0f; // If the distance from the camera to the center of the screen is d and // half the fovy is f, the maximum height of the screen is: // A schematic (from the side); // . // . | // . | <- h // . | // . f | // Camera -> .----------------. <- Screen center // . ^d | // . | // . | <- h // . | // . // tan(f) = h/d => h = tan(f) * d; // The height of the screen is h = tan(f) * d. // // The we know that if the screen has n pixels from top to low (2h), then // 2h ~ n // The width 2w of the screen, with m pixels, is: // 2w ~ m // Then 2w = (2h*m)/n }
void Angle::setDegrees(double deg) { degrees = deg; radians = toRads(deg); }
void Camera::move() { float camMovementXComponent = 0.0f; float camMovementYComponent = 0.0f; float camMovementZComponent = 0.0f; if (holdingForward == true) { float pitchFactor = cos(toRads(XRot)); camMovementXComponent += (SpeedFactor * float(sin(toRads(YRot))) ) * pitchFactor; camMovementYComponent += SpeedFactor * float(sin(toRads(XRot))) * -1.0f; float yawFactor = float(cos(toRads(XRot))); camMovementZComponent += ( SpeedFactor * float(cos(toRads(YRot))) * -1.0f ) * yawFactor; } if (holdingBackward == true) { float pitchFactor = cos(toRads(XRot)); camMovementXComponent += ( SpeedFactor * float(sin(toRads(YRot))) * -1.0f) * pitchFactor; camMovementYComponent += SpeedFactor * float(sin(toRads(XRot))); float yawFactor = float(cos(toRads(XRot))); camMovementZComponent += (SpeedFactor * float(cos(toRads(YRot))) ) * yawFactor; } if (holdingLeftStrafe == true) { float yRotRad = toRads(YRot); camMovementXComponent += -SpeedFactor * float(cos(yRotRad)); camMovementZComponent += -SpeedFactor * float(sin(yRotRad)); } if (holdingRightStrafe == true) { float yRotRad = toRads(YRot); camMovementXComponent += SpeedFactor * float(cos(yRotRad)); camMovementZComponent += SpeedFactor * float(sin(yRotRad)); } XSpeed = camMovementXComponent; YSpeed = camMovementYComponent; ZSpeed = camMovementZComponent; if (XSpeed > SpeedFactor) XSpeed = SpeedFactor; if (XSpeed < -SpeedFactor) XSpeed = -SpeedFactor; if (YSpeed > SpeedFactor) YSpeed = SpeedFactor; if (YSpeed < -SpeedFactor) YSpeed = -SpeedFactor; if (ZSpeed > SpeedFactor) ZSpeed = SpeedFactor; if (ZSpeed < -SpeedFactor) ZSpeed = -SpeedFactor; XPos += XSpeed; YPos += YSpeed; ZPos += ZSpeed; glRotatef(XRot, 1.0f, 0.0f, 0.0f); glRotatef(YRot, 0.0f, 1.0f, 0.0f); glTranslatef(-XPos,-YPos,-ZPos); }