-
Notifications
You must be signed in to change notification settings - Fork 240
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -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); | ||
|
||
float vpLeftEdge = m_pos.x - m_width/2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also this: http://msdn.microsoft.com/en-us/library/aa289157(v=vs.71).aspx
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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