Skip to content

Commit

Permalink
map class and bamboo generation (w/o normal distribution)
Browse files Browse the repository at this point in the history
  • Loading branch information
nietup committed Jun 13, 2015
1 parent a531018 commit 8db42af
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 66 deletions.
Binary file modified Bamboo.sdf
Binary file not shown.
71 changes: 27 additions & 44 deletions BambooStick.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -13,66 +14,48 @@ BambooStick::BambooStick(GLUquadric * pQobj, float x, float y, float z) {
qobj = pQobj;
segments = new std::vector<Segment *>();

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 <float> (rand()) / (static_cast <float> (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) {
Expand Down
11 changes: 9 additions & 2 deletions BambooStick.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
#pragma once
#include "Entity.h"
#include <vector>

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<Segment *> * 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);
};

11 changes: 4 additions & 7 deletions Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand Down
Binary file modified Debug/Bamboo.tlog/CL.read.1.tlog
Binary file not shown.
Binary file modified Debug/Bamboo.tlog/cl.command.1.tlog
Binary file not shown.
Binary file modified Debug/Bamboo.tlog/cl.write.1.tlog
Binary file not shown.
Binary file modified Debug/Bamboo.tlog/link.command.1.tlog
Binary file not shown.
Binary file modified Debug/Bamboo.tlog/link.read.1.tlog
Binary file not shown.
Binary file modified Debug/Bamboo.tlog/link.write.1.tlog
Binary file not shown.
Binary file modified Debug/vc120.idb
Binary file not shown.
4 changes: 4 additions & 0 deletions Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include <windows.h>
#include <GL/gl.h>
#include "glut.h"
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <vector>

class Entity {
protected:
struct Position {
Expand Down
2 changes: 2 additions & 0 deletions Labka03.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@
<ClCompile Include="Entity.cpp" />
<ClCompile Include="Floor.cpp" />
<ClCompile Include="gkom.cpp" />
<ClCompile Include="Map.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="BambooStick.h" />
<ClInclude Include="Camera.h" />
<ClInclude Include="Entity.h" />
<ClInclude Include="Floor.h" />
<ClInclude Include="Map.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
6 changes: 6 additions & 0 deletions Labka03.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<ClCompile Include="Camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Map.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="BambooStick.h">
Expand All @@ -44,5 +47,8 @@
<ClInclude Include="Camera.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Map.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions Map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "Map.h"


Map::Map() {
entities = new std::vector<Entity *>();

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();
}
16 changes: 16 additions & 0 deletions Map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "Entity.h"
#include "BambooStick.h"
#include "Floor.h"

class Map {
private:
std::vector<Entity *> * entities;
GLUquadric * qobj;
public:
Map();
~Map();

void render();
};

19 changes: 6 additions & 13 deletions gkom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity *> * 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};
Expand All @@ -40,24 +43,14 @@ void init() {
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);

entities = new std::vector<Entity *>();
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();
}
Expand Down

0 comments on commit 8db42af

Please sign in to comment.