//Update frame void myApp::update() { // Call predecessor update cglApp::update(); // Process keyboard float dx = 0.0f; float dy = 0.0f; float dr = 0.0f; if (m_keysPressed[VK_LEFT]) dx -= s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed[VK_RIGHT]) dx += s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed[VK_UP]) dy -= s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed[VK_DOWN]) dy += s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed[VK_SUBTRACT]) dr -= s_rKbd2Zoom * m_timer.getDelta(); if (m_keysPressed[VK_ADD]) dr += s_rKbd2Zoom * m_timer.getDelta(); rotateObject(dx, dy); zoom(dr); /* float rI = 0.0f; for (int i = 0; i < 10000000;i++) rI += sin(i);*/ }
//Update frame void myApp::update() { // Call predecessor update cglApp::update(); // Process keyboard float dx = 0.0f; float dy = 0.0f; float dr = 0.0f; if (m_keysPressed[VK_OEM_PLUS] && m_keysPressed[VK_SHIFT]) { // *plane.TexBias() += 0.2f; } if (m_keysPressed[VK_OEM_MINUS] && m_keysPressed[VK_SHIFT]) { // *plane.TexBias() -= 0.2f; } if (freeCamera) { if (m_keysPressed[VK_LEFT]) dx -= s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed[VK_RIGHT]) dx += s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed[VK_UP]) dy -= s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed[VK_DOWN]) dy += s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed[VK_SUBTRACT]) dr -= s_rKbd2Zoom * m_timer.getDelta(); if (m_keysPressed[VK_ADD]) dr += s_rKbd2Zoom * m_timer.getDelta(); rotateObject(dx, dy); zoom(dr); } else { if (m_keysPressed['A']) dx -= s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed['D']) dx += s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed['W']) dy -= s_rKbd2Rotate * m_timer.getDelta(); if (m_keysPressed['S']) dy += s_rKbd2Rotate * m_timer.getDelta(); moveWASDCamera(dx,dy); } /* float rI = 0.0f; for (int i = 0; i < 10000000;i++) rI += sin(i);*/ }
void drawObject(Object draw) { if (rotate != 0) { switch (rotate) { case 1 : draw = rotateObject(draw); break; case 2 : draw = rotateObject(draw); draw = rotateObject(draw); break; case 3 : draw = rotateObject(draw); draw = rotateObject(draw); draw = rotateObject(draw); break; } } if (rotate == 4) rotate = 0; switch (draw.type) { case 0 : case 1 : drawSquare(draw.centerPos[0], draw.centerPos[1]); drawSquare(draw.otherPos[0][0], draw.otherPos[1][0]); drawSquare(draw.otherPos[0][1], draw.otherPos[1][1]); break; case 2 : case 3 : case 4 : drawSquare(draw.centerPos[0], draw.centerPos[1]); drawSquare(draw.otherPos[0][0], draw.otherPos[1][0]); drawSquare(draw.otherPos[0][1], draw.otherPos[1][1]); drawSquare(draw.otherPos[0][2], draw.otherPos[1][2]); break; } }
short newCorner( short alpha, short beta, short gamma, short c1, short c2, short c3) { // generates a cube corner (3 x face) // alpha beta gamma indicate the rotation // c1, c2, c3 are the colors for the three faces in order // return index to face table (containing 3 new faces) short fo, po; short x, y, z; short a, b ,c, d, e, f, g; // determin vertex coordinates x = (U + U/2 +3); y = (U + U/2 +3); z = (U + U/2 +3); // rotation initMatrix( mo, alpha, beta, gamma, 0, 0, 0); // verify there is room in the tables if ((facec>=3) && (pyc>=3) && (pc>=7)) { // generate all points necessary for the cube corner // in base position a = newPoint( x, y, z); // A b = newPoint( x, y-U, z); // B c = newPoint( x-U, y-U, z); // C d = newPoint( x-U, y, z); // D e = newPoint( x, y-U, z-U); // E f = newPoint( x, y, z-U); // F g = newPoint( x-U, y, z-U); // G // generate all the polygons and faces // with proper orientation (normal outbound) newFace ( newPoly( a, b, c, d), c1); newFace ( newPoly( d, g, f, a), c2); fo = newFace( newPoly( a, f, e, b), c3); // add an object with 3 faces po = newObj( fo, 3); // rotate as required rotateObject( mo, po); // returns the object index return po; } // if else while( 1); } // new cube corner
void rotateRow( int x, int y, int z, int v, int s) { // rotate all objects matching x, y, z mask with value v // s verse of rotation int j, i, ix, iy, iz; // animation loop 0-90 deg for(j = 0; j <= 90; j+= STEP) { // clear painting scren gClearScreen(); // clear the hidden screen // search all objects for matching pattern for( iz=0; iz<3; iz++) for( iy=0; iy<3; iy++) for(ix=0; ix<3; ix++) { if ( ix*iy*iz == 1) continue; // ignore c[1][1][1] i = c[ix][iy][iz]; // check if requires rotation if (( (ix+1)*x == v+1) || ((iy+1)*y == v+1) || ((iz+1)*z == v+1)) { // rotate the corresponding axis // transform and draw the object initMatrix( mo, x*s*j, y*s*j, z*s*j, 0, 0, 0); drawObject( mo, i); // if final rotation if ( j == 90) rotateObject( mo, i); } else { // transform and draw the object initMatrix( mo, 0, 0, 0, 0, 0, 0); drawObject( mo, i); } } // update visible screen copyV(); } // for j steps // update pointer matrix c[][][] rotateC( x, y, z, v, s); } // animate rotation of one row/face
short newSide( short alpha, short beta, short gamma, short c1) { // generates a cube center side (1 x face) // alpha beta gamma indicate the rotation // c1 is the colors // return index to face table (containing 1 new face) short f, po; short x, y, z; short a, b ,c, d; // determin vertex coordinates x = (+ U/2 ); y = (+ U/2 ); z = (U + U/2 +3); // rotation initMatrix( mo, alpha, beta, gamma, 0, 0, 0); // verify there is room in the tables if ((facec>=1) && (pyc>=1) && (pc>=4)) { // generate all points necessary for the cube corner // in base position a = newPoint( x, y, z); // A b = newPoint( x, y-U, z); // B c = newPoint( x-U, y-U, z); // C d = newPoint( x-U, y, z); // D // generate all the polygons and faces // with proper orientation (normal outbound) f = newFace ( newPoly( a, b, c, d), c1); // add an object with 1 face po = newObj( f, 1); // rotate as required rotateObject( mo, po); // returns the object index return po; } // if else while( 1); } // new cube side
void rotateCube( int alpha, int beta, int gamma) { int j, i, ar, br, gr; for(j = 0; j <= 90; j+= STEP) { // clear painting screen gClearScreen(); // clear the hidden screen // rotate the corresponding axis ar = alpha * j; br = beta * j; gr = gamma * j ; for( i=objc; i<MAXOBJ; i++) { // transform and draw the object initMatrix( mo, ar, br, gr, 0, 0, 0); drawObject( mo, i); // if final rotation if ( j == 90) rotateObject( mo, i); } // update visible screen copyV(); } // for j steps // update pointer matrix c[][][] for ( i=0; i<3; i++) { if (( alpha<0)||( beta<0)||( gamma<0)) rotateC( ( alpha!=0), ( beta!=0), ( gamma!=0), i, -1); else rotateC( ( alpha!=0), ( beta!=0), ( gamma!=0), i, +1); } } // animate rotation of entire cube
void caGraphics::paintEvent( QPaintEvent *event ) { Q_UNUSED(event); if(thisHide) return; int m_margin = 3; QPointF p1,p2; QPainter painter( this ); painter.setRenderHint( QPainter::Antialiasing ); int margin = thisLineSize/2; int w = width() - 2 * margin; int h = height() - 2 * margin; // do not increase linesize beyond the canvas size if(w <= 0 || h <= 0) { setLineSize(thisLineSize-1); } margin = thisLineSize/2; w = width() - 2 * margin; h = height() - 2 * margin; int x = margin; int y = margin; if(thisLineStyle == Dash) { painter.setPen( QPen( getLineColor(), getLineSize(), Qt::DotLine, Qt::SquareCap) ); } else if (thisLineStyle == BigDash) { painter.setPen( QPen( getLineColor(), getLineSize(), Qt::DashLine, Qt::SquareCap) ); } else { painter.setPen( QPen( getLineColor(), getLineSize(), Qt::SolidLine, Qt::SquareCap) ); } if(thisFillStyle == Filled) { painter.setBrush(getForeground()); } if(getForm() == Rectangle) { QPolygonF rectangle; rectangle.append(QPointF(x,y)); rectangle.append(QPointF(x+w,y)); rectangle.append(QPointF(x+w,y+h)); rectangle.append(QPointF(x,y+h)); rectangle.append(QPointF(x,y)); // rotate QPolygonF rotated = rotateObject(thisTiltAngle, w, h, thisLineSize, rectangle); painter.drawPolygon(rotated); } else if(getForm() == Circle) { // rotate my calculated ellipse QPolygonF rotated = rotateObject(thisTiltAngle, w, h, thisLineSize, drawCircle(x, x+w, y, y+h)); painter.drawPolygon(rotated); } else if(getForm() == Triangle) { QPolygonF triangle; triangle.append(QPointF(x+w/2,0)); triangle.append(QPointF(x,y+h)); triangle.append(QPointF(x+w,y+h)); triangle.append(QPointF(x+w/2,0)); // rotate QPolygonF rotated = rotateObject(thisTiltAngle, w, h, thisLineSize, triangle); painter.drawPolygon(rotated); } else if(getForm() == Arc) { if(thisFillStyle == Filled) { painter.drawPie (x, y, w, h, thisStartAngle * 16, thisSpanAngle * 16); } else { painter.drawArc (x, y, w, h, thisStartAngle * 16, thisSpanAngle * 16); } } else if(getForm() == Line) { QPolygonF line; line.append(QPointF(x,y+h/2)); line.append(QPointF(x+w,y+h/2)); // rotate QPolygonF rotated = rotateObject(thisTiltAngle, w, h, thisLineSize, line); painter.drawPolygon(rotated); } else if(getForm() == Arrow) { QPolygonF lines; QPolygonF head1; QPolygonF head2; p1 = QPointF( m_margin, height()/2 ); p2 = QPointF( width()-m_margin, height()/2 ); lines.append(p1); lines.append(p2); head1 = getHead(p1,p2); if (getArrowMode() == Double) { head2 = getHead(p2,p1); } for(int i=0; i<head1.count(); i++) lines.append(head1.at(i)); for(int i=0; i<head2.count(); i++) lines.append(head2.at(i)); // rotate QPolygonF rotated = rotateObject(thisTiltAngle, w, h, thisLineSize, lines); painter.drawPolygon(rotated); } }