////////////////////////////////////////////////////////////// // // This function returns the camera's axis // ////////////////////////////////////////////////////////////// Vector3 Camera::getAxis() { Vector3 axis; axis = cross(getCameraView(), cameraUpVector); axis = normalize(axis); return axis; }
////////////////////////////////////////////////////////////// // // This function moves the camera forward or backward depending // on the speed // ////////////////////////////////////////////////////////////// void Camera::moveCameraX(float speed) { Vector3 view = getCameraView(); cameraPosition.x += view.x * speed; cameraPosition.z += view.z * speed; cameraView.x += view.x * speed; cameraView.z += view.z * speed; }
void ObjectFromObj::renderImpl() { if (shader_) { shader_->use(); M_.setValue(getModel()); V_.setValue(getCameraView()); P_.setValue(getProjection()); } int lastBuf = -1; glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexesBuffer_); glVertexAttribPointer(++lastBuf, 4, GL_FLOAT, GL_FALSE, 0, (void*)0 ); if (indicesBuffer_ == -1) { if (texturesBuffer_ != -1) { glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, texturesBuffer_); glVertexAttribPointer(++lastBuf, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ); } if (normalsBuffer_ != -1) { glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, normalsBuffer_); glVertexAttribPointer(++lastBuf, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ); } if (fragNormalsBuffer_ != -1) { glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, fragNormalsBuffer_); glVertexAttribPointer(++lastBuf, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ); } glDrawArrays(GL_TRIANGLES, 0, vertexesSize_); } else { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer_); glDrawElements(GL_TRIANGLES, indicesSize_, GL_UNSIGNED_INT, (void*)(0)); } for (int i = 0; i <= lastBuf; ++i) glDisableVertexAttribArray(i); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); }
void Camera::rotateView(float angle, float x, float y, float z) { Vector3 view = getCameraView(); float cosTheta = cos(angle); float sinTheta = sin(angle); Vector3 newView; // This will calculate the x value for the new view newView.x = (cosTheta + (1 - cosTheta) * x * x) * view.x; newView.x += ((1 - cosTheta) * x * y - z * sinTheta) * view.y; newView.x += ((1 - cosTheta) * x * z + y * sinTheta) * view.z; // This will calculate the y value for the new view newView.y = ((1 - cosTheta) * x * y + z * sinTheta) * view.x; newView.y += (cosTheta + (1 - cosTheta) * y * y) * view.y; newView.y += ((1 - cosTheta) * y * z - x * sinTheta) * view.z; // This will calculate the z value for the new view newView.z = ((1 - cosTheta) * x * z - y * sinTheta) * view.x; newView.z += ((1 - cosTheta) * y * z + x * sinTheta) * view.y; newView.z += (cosTheta + (1 - cosTheta) * z * z) * view.z; // Stop the player from going backwards when moving // absolute up or absolute down if (newView.y >= 5 || newView.y <= -5) { return; } cameraView = cameraPosition + newView; Vector3 difference = originalPos - newView; // In order to get the rotation matrix, we need to have the original matrix // and the new rotated matrix, and find the difference, then we need to calculate // the angle from the difference. float newangle = cos(-1.0) * (((view.x * newView.x) + (view.y + newView.y) + (view.z + newView.z)) / magnitude(getCameraView())); rotationVector = Vector4(difference.x, difference.y, difference.z, newangle); }
void Pony48Engine::drawBoard() { float fTotalWidth = BOARD_WIDTH * TILE_WIDTH + (BOARD_WIDTH + 1) * TILE_SPACING; float fTotalHeight = BOARD_HEIGHT * TILE_HEIGHT + (BOARD_HEIGHT + 1) * TILE_SPACING; //Fill in bg fillRect(Point(-fTotalWidth/2.0, fTotalHeight/2.0), Point(fTotalWidth/2.0, -fTotalHeight/2.0), m_BoardBg); //Draw at z = 0 //Fill in bg for individual tiles for(int i = 0; i < BOARD_HEIGHT; i++) { for(int j = 0; j < BOARD_WIDTH; j++) { //Draw tile bg glPushMatrix(); glTranslatef(0, 0, TILEBG_DRAWZ); Point ptDrawPos(-fTotalWidth/2.0 + TILE_SPACING + (TILE_SPACING + TILE_WIDTH) * j, fTotalHeight/2.0 - TILE_SPACING - (TILE_SPACING + TILE_HEIGHT) * i); fillRect(ptDrawPos, Point(ptDrawPos.x + TILE_WIDTH, ptDrawPos.y - TILE_HEIGHT), m_TileBg[j][i]); glPopMatrix(); } } //Draw joining-tile animations for(list<TilePiece*>::iterator i = m_lSlideJoinAnimations.begin(); i != m_lSlideJoinAnimations.end(); i++) { Point ptDrawPos(-fTotalWidth/2.0 + TILE_SPACING + (TILE_SPACING + TILE_WIDTH) * (*i)->destx, fTotalHeight/2.0 - TILE_SPACING - (TILE_SPACING + TILE_HEIGHT) * (*i)->desty); glPushMatrix(); glTranslatef(ptDrawPos.x+TILE_WIDTH/2.0+(*i)->drawSlide.x, ptDrawPos.y-TILE_HEIGHT/2.0+(*i)->drawSlide.y, JOINANIM_DRAWZ); (*i)->draw(); glPopMatrix(); } //Draw tiles themselves (separate loop because z-order alpha issues with animations) for(int i = 0; i < BOARD_HEIGHT; i++) { for(int j = 0; j < BOARD_WIDTH; j++) { Point ptDrawPos(-fTotalWidth/2.0 + TILE_SPACING + (TILE_SPACING + TILE_WIDTH) * j, fTotalHeight/2.0 - TILE_SPACING - (TILE_SPACING + TILE_HEIGHT) * i); //Draw tile if(m_Board[j][i] != NULL) { glPushMatrix(); glTranslatef(ptDrawPos.x+TILE_WIDTH/2.0+m_Board[j][i]->drawSlide.x, ptDrawPos.y-TILE_HEIGHT/2.0+m_Board[j][i]->drawSlide.y, TILE_DRAWZ); m_Board[j][i]->draw(); glPopMatrix(); } } } //Draw particle fx for highest tile if(m_highestTile != NULL) { for(int i = 0; i < BOARD_HEIGHT; i++) { for(int j = 0; j < BOARD_WIDTH; j++) { //Draw tile if(m_Board[j][i] == m_highestTile) { Point ptDrawPos(-fTotalWidth/2.0 + TILE_SPACING + (TILE_SPACING + TILE_WIDTH) * j, fTotalHeight/2.0 - TILE_SPACING - (TILE_SPACING + TILE_HEIGHT) * i); glPushMatrix(); glTranslatef(ptDrawPos.x+TILE_WIDTH/2.0+m_Board[j][i]->drawSlide.x, ptDrawPos.y-TILE_HEIGHT/2.0+m_Board[j][i]->drawSlide.y, TILE_DRAWZ + 0.1); m_newHighTile->img = m_highestTile->bg->img; m_newHighTile->draw(); m_newHighTile->img = m_highestTile->seg->img; m_newHighTile->draw(); glPopMatrix(); break; } } } } //Draw arrows for direction the mouse will move the board if(m_iMouseControl >= MOUSE_MOVE_TRIP_AMT && m_iCurMode == PLAYING) { glPushMatrix(); Point ptMoveDir = worldPosFromCursor(getCursorPos()); //Rotate first to simplify logic. Hooray! switch(getDirOfVec2(ptMoveDir)) { case UP: glRotatef(90, 0, 0, 1); break; case DOWN: glRotatef(-90, 0, 0, 1); break; case LEFT: glRotatef(180, 0, 0, 1); break; } //Determine the drawing alpha based on how far away from the center the mouse is float32 fDestAlpha = min(fabs(ptMoveDir.Length() / (getCameraView().height() / 2.0)) - 0.4, 0.4); //Draw 16 arrows pointing in the direction we'll move for(int y = 0; y < BOARD_HEIGHT; y++) { for(int x = 0; x < BOARD_WIDTH; x++) { //Position to draw this arrow at Point ptDrawPos(-fTotalWidth/2.0 + (TILE_SPACING + TILE_WIDTH) * x + TILE_WIDTH / 2.0 + TILE_SPACING + m_fArrowAdd, fTotalHeight/2.0 - (TILE_SPACING + TILE_WIDTH) * y - TILE_HEIGHT / 2.0 - TILE_SPACING); //If this arrow is reaching the end of its lifespan, fade out float32 fDrawAlpha = fDestAlpha; if(m_fArrowAdd >= MOVEARROW_FADEOUTDIST && x == BOARD_WIDTH - 1) fDrawAlpha *= 1.0f - ((m_fArrowAdd - MOVEARROW_FADEOUTDIST) / MOVEARROW_FADEOUTDIST); fDrawAlpha = min(fDrawAlpha, 1.0f); fDrawAlpha = max(fDrawAlpha, 0.0f); //Now draw glColor4f(1,1,1,fDrawAlpha); glPushMatrix(); glTranslatef(ptDrawPos.x, ptDrawPos.y, MOVEARROW_DRAWZ); m_imgMouseMoveArrow->render(Point(1,1)); glPopMatrix(); //See if we should draw new arrow spawning if(!x && (ARROW_RESET - m_fArrowAdd) <= MOVEARROW_FADEINDIST) { fDrawAlpha = fDestAlpha; fDrawAlpha *= 1.0 - (ARROW_RESET - m_fArrowAdd) / MOVEARROW_FADEINDIST; fDrawAlpha = min(fDrawAlpha, 1.0f); fDrawAlpha = max(fDrawAlpha, 0.0f); glColor4f(1,1,1,fDrawAlpha); glPushMatrix(); glTranslatef(ptDrawPos.x - (TILE_SPACING + TILE_WIDTH), ptDrawPos.y, MOVEARROW_DRAWZ); m_imgMouseMoveArrow->render(Point(1,1)); glPopMatrix(); } } } glPopMatrix(); } }