Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple tile visibility checking in the ViewModule #5

Merged
merged 2 commits into from
Sep 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ ios:
xcodebuild -workspace ios/TangramiOS.xcworkspace -scheme TangramiOS -destination 'platform=iOS Simulator,name=iPhone Retina (3.5-inch)'

osx/lib/libtangram.so: core/tangram.cpp core/tangram.h
cc -o osx/lib/libtangram.so core/tangram.cpp osx/src/platform_osx.cpp -Icore -DPLATFORM_OSX -lglfw3 -framework OpenGL -shared
clang++ -o osx/lib/libtangram.so core/tangram.cpp core/viewModule/viewModule.cpp osx/src/platform_osx.cpp -Icore -Icore/include -DPLATFORM_OSX -lglfw3 -framework OpenGL -shared -std=c++11

osx/bin/TangramOSX: osx/lib/libtangram.so
clang -o osx/bin/TangramOSX osx/src/main.cpp osx/lib/libtangram.so -DPLATFORM_OSX -lglfw3 -framework Cocoa -framework IOKit -framework OpenGL -framework CoreVideo
clang++ -o osx/bin/TangramOSX osx/src/main.cpp osx/lib/libtangram.so -Icore/include -DPLATFORM_OSX -lglfw3 -framework Cocoa -framework IOKit -framework OpenGL -framework CoreVideo

osx: osx/bin/TangramOSX
74 changes: 60 additions & 14 deletions core/viewModule/viewModule.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#include "viewModule.h"

ViewModule::ViewModule(float width, float height) {
init(width, height);
ViewModule::ViewModule(float _width, float _height) {
init(_width, _height);
}

ViewModule::ViewModule() {
init(800, 600);
}

void ViewModule::init(float width, float height) {
void ViewModule::init(float _width, float _height) {

m_dirty = true;

// Set up projection matrix based on input width and height with an arbitrary zoom
setAspect(width, height);
setAspect(_width, _height);
setZoom(16); // Arbitrary zoom for testing

// Set up view matrix
Expand All @@ -22,30 +24,30 @@ void ViewModule::init(float width, float height) {

}

void ViewModule::setAspect(float width, float height) {
void ViewModule::setAspect(float _width, float _height) {

m_aspect = width / height;
m_aspect = _width / _height;
setZoom(m_zoom);

}

void ViewModule::setPosition(float x, float y) {
void ViewModule::setPosition(float _x, float _y) {

translate(x - m_pos.x, y - m_pos.y);
translate(_x - m_pos.x, _y - m_pos.y);

}

void ViewModule::translate(float dx, float dy) {
void ViewModule::translate(float _dx, float _dy) {

glm::translate(m_view, glm::vec3(dx, dy, 0.0));
m_pos.x += dx;
m_pos.y += dy;
glm::translate(m_view, glm::vec3(_dx, _dy, 0.0));
m_pos.x += _dx;
m_pos.y += _dy;

}

void ViewModule::setZoom(int z) {
void ViewModule::setZoom(int _z) {

m_zoom = z;
m_zoom = _z;
float tileSize = 2 * PI * EARTH_RADIUS_M / pow(2, m_zoom);
m_height = 3 * tileSize; // Set viewport size to ~3 tiles vertically
m_width = m_height * m_aspect; // Size viewport width to match aspect ratio
Expand All @@ -60,3 +62,47 @@ glm::mat2 ViewModule::getBoundsRect() {
return glm::mat2(m_pos.x - hw, m_pos.y - hh, m_pos.x + hw, m_pos.y + hh);

}

const std::vector<glm::ivec3>& ViewModule::getVisibleTiles() {

if (!m_dirty) {
return m_visibleTiles;
}

m_visibleTiles.clear();

float tileSize = 2 * PI * EARTH_RADIUS_M / pow(2, m_zoom);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small thing.
Its always recommended to do multiplication over division. Sometime the performance gains are huge..
Some Floating point processing multiplication can be 5 to 8 times faster than floating point division.

float inversePowZoon = 1/pow(2, m_zoom);
float tileSize = 2_PI_EARTH_RADIUS_M*inversePowZoon;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tallytalwar really?? Do you have some docs to point to on that - curious to read more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup... hence use of bitwise operators is also recommended. (multiplication by 2 should be replaced with one right shift, and division by 2 should be replaced by one left shift)..

there are many more such optimizations ...
http://books.google.com/books?id=IRN0IEXJzKEC&pg=PA92&lpg=PA92&dq=floating+point+division+multiplication+optimization+c%2B%2B&source=bl&ots=P1mjHg8EAs&sig=sZD71hukKFcJ6RaBJz4Q5awC9gs&hl=en&sa=X&ei=hZUXVNvRENSn8QGvq4HoBw&ved=0CFEQ6AEwBw#v=onepage&q=floating%20point%20division%20multiplication%20optimization%20c%2B%2B&f=false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep but those references are over a decade old, so I wonder if they still
apply. Bit-shifting is an old x86 technique, I use it out of habit. I
wonder though if the compiler won't apply these optimizations automatically
anyway... worth reading up on just to know.

On Mon, Sep 15, 2014 at 9:45 PM, Varun notifications@github.com wrote:

In core/viewModule/viewModule.cpp:

@@ -60,3 +62,47 @@ glm::mat2 ViewModule::getBoundsRect() {
return glm::mat2(m_pos.x - hw, m_pos.y - hh, m_pos.x + hw, m_pos.y + hh);

}
+
+const std::vectorglm::ivec3& ViewModule::getVisibleTiles() {
+

  • if (!m_dirty) {
  •   return m_visibleTiles;
    
  • }
  • m_visibleTiles.clear();
  • float tileSize = 2 * PI * EARTH_RADIUS_M / pow(2, m_zoom);

yup... hence use of bitwise operators is also recommended. (multiplication
by 2 should be replaced with one right shift, and division by 2 should be
replaced by one left shift)..

there are many more such optimizations ...

http://books.google.com/books?id=IRN0IEXJzKEC&pg=PA92&lpg=PA92&dq=floating+point+division+multiplication+optimization+c%2B%2B&source=bl&ots=P1mjHg8EAs&sig=sZD71hukKFcJ6RaBJz4Q5awC9gs&hl=en&sa=X&ei=hZUXVNvRENSn8QGvq4HoBw&ved=0CFEQ6AEwBw#v=onepage&q=floating%20point%20division%20multiplication%20optimization%20c%2B%2B&f=false


Reply to this email directly or view it on GitHub
https://github.com/tangram-map/tangram-es/pull/5/files#r17579753.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure will do..

What I have been told by developers I have interacted with at DWA and other gaming studios that these still apply. But I will still confirm. If compiler optimization are there then it will save us a lot of pain. :D


float vpLeftEdge = m_pos.x - m_width/2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same .. instead of dividing by 2, multiply by 0.5.

float vpRightEdge = vpLeftEdge + m_width;
float vpBottomEdge = m_pos.y - m_height/2;
float vpTopEdge = vpBottomEdge + m_height;

int tileX = (int) vpLeftEdge / tileSize;
int tileY = (int) vpBottomEdge / tileSize;

float x = tileX * tileSize;
float y = tileY * tileSize;

while (x < vpRightEdge) {

while (y < vpTopEdge) {

m_visibleTiles.push_back(glm::ivec3(tileX, tileY, m_zoom));
tileY++;
y += tileSize;

}

tileY = (int) vpBottomEdge / tileSize;
y = tileY * tileSize;

tileX++;
x += tileSize;
}

m_dirty = false;

return m_visibleTiles;

}
19 changes: 11 additions & 8 deletions core/viewModule/viewModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,27 @@ class ViewModule {

public:

ViewModule(float width, float height);
ViewModule(float _width, float _height);
ViewModule();

void setAspect(float width, float height);
void setPosition(float x, float y);
void setZoom(int z);
void translate(float dx, float dy);
void zoom(int dz);
void setAspect(float _width, float _height);
void setPosition(float _x, float _y);
void setZoom(int _z);
void translate(float _dx, float _dy);
void zoom(int _dz);

int getZoom() { return m_zoom; };
glm::vec3 getPosition() { return m_pos; };
glm::mat4 getViewMatrix() { return m_view; };
glm::mat4 getProjectionMatrix() { return m_proj; };

glm::mat2 getBoundsRect(); // Returns a rectangle of the current view range as [[x_min, y_min][x_max, y_max]]
const std::vector<glm::ivec3>& getVisibleTiles();

private:

bool m_dirty;
std::vector<glm::ivec3> m_visibleTiles;
glm::vec3 m_pos;
glm::mat4 m_view;
glm::mat4 m_proj;
Expand All @@ -49,6 +52,6 @@ class ViewModule {
float m_height;
float m_aspect;

void init(float width, float height);
void init(float _width, float _height);

};
};