diff --git a/Bamboo.sdf b/Bamboo.sdf index 68cc410..c393e44 100644 Binary files a/Bamboo.sdf and b/Bamboo.sdf differ diff --git a/BambooStick.cpp b/BambooStick.cpp index 761957c..1183c6a 100644 --- a/BambooStick.cpp +++ b/BambooStick.cpp @@ -1,9 +1,10 @@ #include "BambooStick.h" +const int BambooStick::leafness = 3; +const float BambooStick::botTopFraction = 1.2f; +const float BambooStick::botHeightFraction = 0.2f; + BambooStick::BambooStick() { - position.x = 30; - position.y = 30; - position.z = 100; } BambooStick::BambooStick(GLUquadric * pQobj, float x, float y, float z) { @@ -13,66 +14,48 @@ BambooStick::BambooStick(GLUquadric * pQobj, float x, float y, float z) { qobj = pQobj; segments = new std::vector(); + baseSegmentHeight = (float)(rand() % 5 + 3); generate(); } - BambooStick::~BambooStick() { + segments->clear(); + delete segments; } - void BambooStick::render() { - - //glPushMatrix(); - ////qobj = gluNewQuadric(); - //gluQuadricNormals(qobj, GLU_SMOOTH); - //glTranslatef(position.x, position.y, position.z); - //glRotatef(99.f, 1.f, 0.f, 0.f); - //gluCylinder(qobj, 2.0, 1.0, 10.0, 9, 1); - //glPopMatrix(); - - //glPushMatrix(); - //// qobj = gluNewQuadric(); - //gluQuadricNormals(qobj, GLU_SMOOTH); - //glTranslatef(position.x, position.y + 5.f, position.z + 0.5f); - //glRotatef(99.f, 1.f, 0.f, 0.f); - //gluCylinder(qobj, 2.0, 1.0, 5.0, 9, 1); - //glPopMatrix(); - float height = 0; for (int i = 0; i < segments->size(); i++) { glPushMatrix(); gluQuadricNormals(qobj, GLU_SMOOTH); - glTranslatef(position.x, - position.y + getSegment(i)->height + height, - position.z + 0.5f*getSegment(i)->botRadius); + glTranslatef(position.x, position.y + getSegment(i)->height + height, position.z); glRotatef(90.f, 1.f, 0.f, 0.f); gluCylinder(qobj, getSegment(i)->topRadius, getSegment(i)->botRadius, getSegment(i)->height, 9, 1); glPopMatrix(); - height += getSegment(i)->height/2.f; + height += getSegment(i)->height; } - - } void BambooStick::generate() { - Segment * s = new Segment(); - s->botRadius = 1.f; - s->topRadius = 2.f; - s->height = 5.f; - segments->push_back(s); - - s = new Segment(); - s->botRadius = 1.f; - s->topRadius = 2.f; - s->height = 3.f; - segments->push_back(s); + int segmentsNo = rand() % 5 + 3; + for (int i = 0; i < segmentsNo; i++) + segments->push_back(generateSegment()); +} - s = new Segment(); - s->botRadius = 1.f; - s->topRadius = 2.f; - s->height = 4.f; - segments->push_back(s); +BambooStick::Segment * BambooStick::generateSegment() { + Segment * s = new Segment(); + float hi = baseSegmentHeight + (baseSegmentHeight*0.35f); + float lo = baseSegmentHeight - (baseSegmentHeight*0.35f); + s->height = lo + static_cast (rand()) / (static_cast (RAND_MAX / (hi - lo))); + s->botRadius = baseSegmentHeight * botHeightFraction; + s->topRadius = s->botRadius * botTopFraction; + + if (!(rand() % leafness)) + s->leaf = true; + else + s->leaf = false; + + return s; } BambooStick::Segment * BambooStick::getSegment(int i) { diff --git a/BambooStick.h b/BambooStick.h index 841cab1..609227f 100644 --- a/BambooStick.h +++ b/BambooStick.h @@ -1,28 +1,35 @@ #pragma once #include "Entity.h" -#include class BambooStick : public Entity{ private: + static const int leafness; //denominator in chance of leaf in each segment (where numerator is 1) + static const float botTopFraction; + static const float botHeightFraction; + Position position; + float baseSegmentHeight; struct Segment { float botRadius; float topRadius; float height; + bool leaf; }; float width, height; GLUquadricObj *qobj; std::vector * segments; + void generate(); + Segment * generateSegment(); + public: BambooStick(); BambooStick(GLUquadric * q, float x, float y, float z); ~BambooStick(); void render(); - void generate(); Segment * getSegment(int x); }; diff --git a/Camera.cpp b/Camera.cpp index 7443c83..e2e22b0 100644 --- a/Camera.cpp +++ b/Camera.cpp @@ -46,11 +46,8 @@ void Camera::computeDirVertical() { } void Camera::refresh() { - //if (position.deltaMove != 0) computePos(); - //if (position.deltaAngle != 0) computeDir(); - //if (position.deltaAngleVertical != 0) computeDirVertical(); gluLookAt(position.x, position.y, position.z, @@ -62,10 +59,10 @@ void Camera::update(Action a, int key) { if (a == NORMAL_DOWN) { switch (key) { case 'w': - position.deltaMove = 1.8f; + position.deltaMove = 2.f; break; case 's': - position.deltaMove = -0.5f; + position.deltaMove = -2.f; break; } } @@ -80,10 +77,10 @@ void Camera::update(Action a, int key) { if (a == ARROW_DOWN) { switch (key) { case GLUT_KEY_LEFT: - position.deltaAngle = -0.01f; + position.deltaAngle = -0.02f; break; case GLUT_KEY_RIGHT: - position.deltaAngle = 0.01f; + position.deltaAngle = 0.02f; break; case GLUT_KEY_UP: position.deltaAngleVertical = 0.01f; diff --git a/Debug/Bamboo.tlog/CL.read.1.tlog b/Debug/Bamboo.tlog/CL.read.1.tlog index fc12abe..786a7b7 100644 Binary files a/Debug/Bamboo.tlog/CL.read.1.tlog and b/Debug/Bamboo.tlog/CL.read.1.tlog differ diff --git a/Debug/Bamboo.tlog/cl.command.1.tlog b/Debug/Bamboo.tlog/cl.command.1.tlog index 6fac714..1acb2b5 100644 Binary files a/Debug/Bamboo.tlog/cl.command.1.tlog and b/Debug/Bamboo.tlog/cl.command.1.tlog differ diff --git a/Debug/Bamboo.tlog/cl.write.1.tlog b/Debug/Bamboo.tlog/cl.write.1.tlog index 8761e85..2a550e1 100644 Binary files a/Debug/Bamboo.tlog/cl.write.1.tlog and b/Debug/Bamboo.tlog/cl.write.1.tlog differ diff --git a/Debug/Bamboo.tlog/link.command.1.tlog b/Debug/Bamboo.tlog/link.command.1.tlog index e3e9c0a..e58e47c 100644 Binary files a/Debug/Bamboo.tlog/link.command.1.tlog and b/Debug/Bamboo.tlog/link.command.1.tlog differ diff --git a/Debug/Bamboo.tlog/link.read.1.tlog b/Debug/Bamboo.tlog/link.read.1.tlog index eba01e8..fb3160e 100644 Binary files a/Debug/Bamboo.tlog/link.read.1.tlog and b/Debug/Bamboo.tlog/link.read.1.tlog differ diff --git a/Debug/Bamboo.tlog/link.write.1.tlog b/Debug/Bamboo.tlog/link.write.1.tlog index 20e68d5..c75296d 100644 Binary files a/Debug/Bamboo.tlog/link.write.1.tlog and b/Debug/Bamboo.tlog/link.write.1.tlog differ diff --git a/Debug/vc120.idb b/Debug/vc120.idb index 752ec19..0e2892c 100644 Binary files a/Debug/vc120.idb and b/Debug/vc120.idb differ diff --git a/Entity.h b/Entity.h index cf36678..ff48ff6 100644 --- a/Entity.h +++ b/Entity.h @@ -2,6 +2,10 @@ #include #include #include "glut.h" +#include /* srand, rand */ +#include /* time */ +#include + class Entity { protected: struct Position { diff --git a/Labka03.vcxproj b/Labka03.vcxproj index e8ce561..58611d3 100644 --- a/Labka03.vcxproj +++ b/Labka03.vcxproj @@ -82,12 +82,14 @@ + + diff --git a/Labka03.vcxproj.filters b/Labka03.vcxproj.filters index 48bea86..2213921 100644 --- a/Labka03.vcxproj.filters +++ b/Labka03.vcxproj.filters @@ -30,6 +30,9 @@ Source Files + + Source Files + @@ -44,5 +47,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Map.cpp b/Map.cpp new file mode 100644 index 0000000..718f33e --- /dev/null +++ b/Map.cpp @@ -0,0 +1,31 @@ +#include "Map.h" + + +Map::Map() { + entities = new std::vector(); + + qobj = gluNewQuadric(); + Entity * e; + e = new Floor(-2.f, 0.f, -30.f, 100.f, 100.f); + entities->push_back(e); + + for (int x = -5; x < 5; x++) { + for (int z = -8; z < 2; z++) { + if ((z + 8) % 2) + e = new BambooStick(qobj, 10 * x + 5, 0, 10 * z); + else + e = new BambooStick(qobj, 10 * x, 0, 10 * z); + + entities->push_back(e); + } + } +} + + +Map::~Map() { +} + +void Map::render() { + for (int i = 0; i < entities->size(); i++) + (*entities)[i]->render(); +} \ No newline at end of file diff --git a/Map.h b/Map.h new file mode 100644 index 0000000..d215122 --- /dev/null +++ b/Map.h @@ -0,0 +1,16 @@ +#pragma once +#include "Entity.h" +#include "BambooStick.h" +#include "Floor.h" + +class Map { +private: + std::vector * entities; + GLUquadric * qobj; +public: + Map(); + ~Map(); + + void render(); +}; + diff --git a/gkom.cpp b/gkom.cpp index 670b9a6..dfc1314 100644 --- a/gkom.cpp +++ b/gkom.cpp @@ -10,16 +10,19 @@ #include "BambooStick.h" #include "Floor.h" #include "Camera.h" +#include "Map.h" int width; int height; double lasttime; double timediff; double fps; -std::vector * entities; Camera * camera; +Map * map; void init() { + srand(time(NULL)); + GLfloat mat_ambient[] = {1.0, 1.0, 1.0, 1.0}; //mat - wspolczynniki odbicia GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_position[] = {0.0, 0.0, 10.0, 1.0}; @@ -40,24 +43,14 @@ void init() { glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); - entities = new std::vector(); camera = new Camera(); - GLUquadric * qobj = gluNewQuadric(); - Entity * e; - e = new BambooStick(qobj, 10, 0, -30); - entities->push_back(e); - e = new BambooStick(qobj, -10, 0, -30); - entities->push_back(e); - e = new Floor(0.f, 0.f, -30.f, 10.f, 10.f); - entities->push_back(e); + map = new Map(); } void displayObjects() { - glPushMatrix(); - for (int i = 0; i < entities->size(); i++) - (*entities)[i]->render(); + map->render(); glPopMatrix(); }