diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2019-01-21 19:42:19 +0100 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2019-01-21 19:42:19 +0100 |
commit | 8693fd830680355d0633d6a4b681386832183b0d (patch) | |
tree | bd6cf0f2c08ac2072519c3781c1d1d7a7261eded | |
parent | b8aa898b1f3182d028a67c39d8643b331c565053 (diff) | |
download | termgl-8693fd830680355d0633d6a4b681386832183b0d.tar.gz |
Implemented rotate
angle is stored seperately, original vectors stay untouched. the
resulting vectors are calculated in write() with applyRotation() to
avoid growing rounding errors
-rw-r--r-- | cWiremesh.cpp | 49 | ||||
-rw-r--r-- | main.cpp | 14 |
2 files changed, 53 insertions, 10 deletions
diff --git a/cWiremesh.cpp b/cWiremesh.cpp index 91f92af..085f366 100644 --- a/cWiremesh.cpp +++ b/cWiremesh.cpp @@ -3,6 +3,7 @@ cWiremesh::cWiremesh() { position = {0,0,0}; + angle = {0,0,0}; } cWiremesh::~cWiremesh() @@ -35,9 +36,12 @@ void cWiremesh::write(cRender *_render) for(long unsigned int i = 0; i < vectors.size(); i++) { + sCoord3d vorigin = applyRotation(vectors[i].origin, angle); + sCoord3d vdirection = applyRotation(vectors[i].direction, angle); + _render->drawLine(vectors[i].c, - translate(vectors[i].origin + position, origin), - translate(vectors[i].origin + vectors[i].direction + position, origin), + translate(vorigin + position, origin), + translate(vorigin + vdirection + position, origin), true, vectors[i].color); } } @@ -81,21 +85,54 @@ void cWiremesh::scale(float _scalar) } } -sCoord3d applyRotation(sCoord3d _vector, sCoord3d _angle) +sCoord3d cWiremesh::applyRotation(sCoord3d _vector, sCoord3d _angle) { sCoord3d ret = _vector; + //Perform some algebra-magic + //couldn't be bothered to implement or use a matrix class + if(_angle.x) { - + float rads = (float)_angle.x * PI / 180.0; + + ret.y = (int)( + (float)_vector.y * cos(rads) - + (float)_vector.z * sin(rads) + ); + ret.z = (int)( + (float)_vector.y * sin(rads) + + (float)_vector.z * cos(rads) + ); } if(_angle.y) { - + float rads = (float)_angle.y * PI / 180.0; + sCoord3d tmp = ret; + + ret.x = (int)( + (float)tmp.x * cos(rads) + + (float)tmp.z * sin(rads) + ); + + ret.z = (int)( + - (float)tmp.x * sin(rads) + + (float)tmp.z * cos(rads) + ); } if(_angle.z) { - + float rads = (float)_angle.z * PI / 180.0; + sCoord3d tmp = ret; + + ret.x = (int) ( + (float)tmp.x * cos(rads) - + (float)tmp.y * sin(rads) + ); + ret.y = (int) ( + (float)tmp.x * sin(rads) + + (float)tmp.y * cos(rads) + ); } return ret; @@ -91,16 +91,22 @@ int main() switch(ie.c) { case 'w': - position.z++; + obj.rotate({-10,0,0}); break; case 's': - position.z--; + obj.rotate({10,0,0}); break; case 'a': - obj.scale(1.2); + obj.rotate({0,-10,0}); break; case 'd': - obj.scale(0.8); + obj.rotate({0,10,0}); + break; + case 'q': + obj.rotate({0,0,-10}); + break; + case 'e': + obj.rotate({0,0,10}); break; }; obj.setPosition(position); |