mat4 Transform::getModelMatrix() { mat4 out = translate(glm::mat4(), getGlobalPosition()); out = rotate(out, getGlobalRotation().x, vec3(1.0f, 0.0f, 0.0f)); out = rotate(out, getGlobalRotation().y, vec3(0.0f, 1.0f, 0.0f)); out = rotate(out, getGlobalRotation().z, vec3(0.0f, 0.0f, 1.0f)); out = scale(out, getGlobalScale()); return out; }
Vector3d CSkeleton::fitToLocation(Joint_handle hJoint, Vector3d target_loc) { Joint_handle hRoot = rootOf(hJoint); updateGlobalPostures(hRoot); if(hRoot == parents[hJoint]) { joints[hJoint].setOffset(target_loc); updateGlobalPostures(hRoot); return globals[hJoint].getOffset(); } //updateGlobalPostures(0); Joint_handle hParent = parents[hJoint]; Vector3d dJoint, dTarget; Vector3d pJoint, pParent; pJoint = getGlobalPosition(hJoint); pParent = getGlobalPosition(hParent); dJoint = (pJoint - pParent).Normalize(); dTarget = (target_loc - pParent).Normalize(); Quaterniond qParent = getGlobalRotation(hParent); // dJoint = cast_to_vector(!qParent*dJoint*qParent); // dTarget = cast_to_vector(!qParent*dTarget*qParent); Vector3d axis = Vector3d::Cross(dJoint, dTarget); double dot = Vector3d::Dot(dJoint, dTarget); double angle = acos(Vector3d::Dot(dJoint, dTarget)); Quaterniond qtemp = !qParent*Quaterniond(0, axis.X(), axis.Y(), axis.Z())*qParent; axis = Vector3d(qtemp.X(), qtemp.Y(), qtemp.Z()); Quaterniond rot; rot.FromAxisAngle(angle, axis.X(), axis.Y(), axis.Z()); qtemp = rot*Quaterniond(0, dJoint.X(), dJoint.Y(), dJoint.Z())*!rot; Vector3d pos(qtemp.X(), qtemp.Y(), qtemp.Z()); Vector3d err = pos - dTarget; if( angle == 0.f || (axis.X() == 0 && axis.Y() == 0 && axis.Z() ==0) || dJoint == dTarget) return globals[hJoint].getOffset(); joints[hParent].Rotate(rot); //rotateJoint(hParent, rot); updateGlobalPostures(hParent); //updateGlobalPostures(0); return globals[hJoint].getOffset(); }
void CSkeleton::drawSkeleton(void) { for(size_t i = 0; i < joints.size(); i++) if(isRoot(i)) updateGlobalPostures(i); size_t n = joints.size(); Quaterniond rot, a; Vector3d pos; for(size_t i = 1; i < n; i++) { rot = getGlobalRotation( i ); pos = getGlobalPosition( i ); a = rot.Log(); glPushMatrix(); glTranslated(pos.X(), pos.Y(), pos.Z()); glRotated(a.W()*180/M_PI, a.X(), a.Y(), a.Z()); glColor3ub(255, 255, 0); glBegin(GL_LINES); glVertex3f(0, 0, 0); glVertex3f(0, 0, joints[i].getScale().Z()); glEnd(); glBegin(GL_LINES); glColor3ub(255, 0, 0); glVertex3f(0, 0, 0); glVertex3f(0.1, 0, 0); glColor3ub(0, 255, 0); glVertex3f(0, 0, 0); glVertex3f(0, 0.1, 0); glColor3ub(0, 0, 255); glVertex3f(0, 0, 0); glVertex3f(0, 0, 0.1); glEnd(); glPopMatrix(); } }
void Sprite::draw() { if (!_visible) { return; } //fix bug of transform of difference window size Vec2 windowSize = Director::getInstance()->getWindowSize(); auto originalSize = _texture2d->getRect(); Vec2 localAnchorPoint(originalSize.x*_anchor.x, originalSize.y*_anchor.y); Vec2 vertexes[4] = { -localAnchorPoint, { originalSize.x - localAnchorPoint.x, -localAnchorPoint.y }, originalSize - localAnchorPoint, { -localAnchorPoint.x, originalSize.y - localAnchorPoint.y } }; auto globalPosition = getGlobalPosition(); auto globalScale = getGlobalScale(); auto globalRotation = getGlobalRotation(); Vec2 _glScale{ 2 / windowSize.x, 2 / windowSize.y }; // init for rotating auto sin = Math::sin(globalRotation); auto cos = Math::cos(globalRotation); for (auto &vertex : vertexes) { // local transform // rotate { auto tempX = vertex.x; vertex.x = tempX*cos - vertex.y*sin; vertex.y = tempX*sin + vertex.y*cos; } vertex.x *= globalScale.x; vertex.y *= globalScale.y; // scale // global transform vertex.x += globalPosition.x; vertex.y += globalPosition.y; // gl transform vertex.x *= _glScale.x; vertex.y *= _glScale.y; vertex.x -= 1; vertex.y -= 1; } // draw float *_coord2f = _texture2d->getCoord2fPoints(); int _index_1 = 0; int _index_2 = 1; int _index_3 = 2; int _index_4 = 3; if (_is_flipx) { _index_1 = 2; _index_3 = 0; } if (_is_flipy) { _index_2 = 3; _index_4 = 1; } if (!cmd) { cmd = new RenderCmd_Quad; } cmd->_vertex[0] = vertexes[_index_1]; cmd->_vertex[1] = vertexes[_index_2]; cmd->_vertex[2] = vertexes[_index_3]; cmd->_vertex[3] = vertexes[_index_4]; cmd->_coord2f = _coord2f; cmd->tex = _texture2d; cmd->_opacity = _opactiy; Director::getInstance()->getRenderCmdQueue()->addRenderCmd(cmd); /* there are two methods to slove flip */ /* //method one int _index_1 = 0; int _index_2 = 1; int _index_3 = 2; int _index_4 = 3; if (_is_flipx) { _index_1 = 2; _index_3 = 0; } if (_is_flipy) { _index_2 = 3; _index_4 = 1; } glTexCoord2f(_coord2f[0], _coord2f[1]); glVertex2f(vertexes[_index_1].x, vertexes[_index_1].y); glTexCoord2f(_coord2f[2], _coord2f[3]); glVertex2f(vertexes[_index_2].x, vertexes[_index_2].y); glTexCoord2f(_coord2f[4], _coord2f[5]); glVertex2f(vertexes[_index_3].x, vertexes[_index_3].y); glTexCoord2f(_coord2f[6], _coord2f[7]); glVertex2f(vertexes[_index_4].x, vertexes[_index_4].y); // method two if (_is_flipx == false && _is_flipy == false) { glTexCoord2f(_coord2f[0], _coord2f[1]); glVertex2f(vertexes[0].x, vertexes[0].y); glTexCoord2f(_coord2f[2], _coord2f[3]); glVertex2f(vertexes[1].x, vertexes[1].y); glTexCoord2f(_coord2f[4], _coord2f[5]); glVertex2f(vertexes[2].x, vertexes[2].y); glTexCoord2f(_coord2f[6], _coord2f[7]); glVertex2f(vertexes[3].x, vertexes[3].y); } else if (_is_flipx == true && _is_flipy == false) { glTexCoord2f(_coord2f[0], _coord2f[1]); glVertex2f(vertexes[1].x, vertexes[1].y); glTexCoord2f(_coord2f[2], _coord2f[3]); glVertex2f(vertexes[0].x, vertexes[0].y); glTexCoord2f(_coord2f[4], _coord2f[5]); glVertex2f(vertexes[3].x, vertexes[3].y); glTexCoord2f(_coord2f[6], _coord2f[7]); glVertex2f(vertexes[2].x, vertexes[2].y); } else if (_is_flipx == false && _is_flipy == true) { glTexCoord2f(_coord2f[0], _coord2f[1]); glVertex2f(vertexes[3].x, vertexes[3].y); glTexCoord2f(_coord2f[2], _coord2f[3]); glVertex2f(vertexes[2].x, vertexes[2].y); glTexCoord2f(_coord2f[4], _coord2f[5]); glVertex2f(vertexes[1].x, vertexes[1].y); glTexCoord2f(_coord2f[6], _coord2f[7]); glVertex2f(vertexes[0].x, vertexes[0].y); } else if (_is_flipx == true && _is_flipy == true) { glTexCoord2f(_coord2f[0], _coord2f[1]); glVertex2f(vertexes[2].x, vertexes[2].y); glTexCoord2f(_coord2f[2], _coord2f[3]); glVertex2f(vertexes[3].x, vertexes[3].y); glTexCoord2f(_coord2f[4], _coord2f[5]); glVertex2f(vertexes[0].x, vertexes[0].y); glTexCoord2f(_coord2f[6], _coord2f[7]); glVertex2f(vertexes[1].x, vertexes[1].y); } */ Node::draw(); }