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

Uvs ready to be merge #26

Merged
merged 22 commits into from
Dec 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
970ef71
first step
patriciogonzalezvivo Nov 18, 2014
c8bac3e
dynamic polyline
patriciogonzalezvivo Nov 18, 2014
0b2e262
Add some helper functions for the UVs
patriciogonzalezvivo Nov 19, 2014
584e978
fixing uv on polygons
patriciogonzalezvivo Nov 19, 2014
08797cf
Merge branch 'master' into multiStyles
matteblair Nov 19, 2014
b1cc204
changeing uvs name for texcoord as webgl
patriciogonzalezvivo Dec 1, 2014
cc9fa93
Merge branch 'master' of github.com:tangrams/tangram-es into uvs
patriciogonzalezvivo Dec 1, 2014
799a4a5
Fix warnings
matteblair Dec 1, 2014
0a9fab6
fixes
patriciogonzalezvivo Dec 1, 2014
665b0c5
merge
patriciogonzalezvivo Dec 1, 2014
1da0bfb
fix merge
patriciogonzalezvivo Dec 1, 2014
a8a2b2a
Refactor GeometryHandler to reduce code duplication
matteblair Dec 1, 2014
d689ec1
Merge branch 'uvs' of https://github.com/tangram-map/tangram-es into uvs
matteblair Dec 1, 2014
1136d7e
Refactor geometryHandler
matteblair Dec 1, 2014
2490847
Rename 'GeometryHandler' to 'Builders'
matteblair Dec 2, 2014
bd187b2
Remove unused include
matteblair Dec 2, 2014
58a864d
Undo using UVs as color; use UVs for road outline
matteblair Dec 2, 2014
60b6fd2
Documentation for Builders
matteblair Dec 2, 2014
9899312
Add newlines
matteblair Dec 2, 2014
7c03b5f
Code style changes, cleaning, and documentation for geom.h and geom.cpp
matteblair Dec 3, 2014
5e1e0e0
Remove unused (large) geometry functions
matteblair Dec 3, 2014
bb7e3ba
Adding documentation, cleaning extra functions and fixing issues
patriciogonzalezvivo Dec 3, 2014
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
7 changes: 6 additions & 1 deletion core/resources/polygon.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#ifdef GL_ES
precision mediump float;
#endif

varying vec4 v_pos;
varying vec4 v_color;
varying vec3 v_normal;
varying vec2 v_texcoord;

void main(void) {
gl_FragColor = v_color;
gl_FragColor = v_color;
}
27 changes: 21 additions & 6 deletions core/resources/polygon.vs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
#ifdef GL_ES
precision mediump float;
#endif

uniform mat4 u_modelViewProj;
uniform vec4 u_lightDirection;
uniform vec3 u_lightDirection;

attribute vec4 a_position;
attribute vec4 a_normal;
attribute vec4 a_color;
attribute vec3 a_normal;
attribute vec2 a_texcoord;

varying vec4 v_pos;
varying vec4 v_color;
varying vec3 v_normal;
varying vec2 v_texcoord;

void main() {
float lit = dot(normalize(u_lightDirection), normalize(a_normal));
v_color = a_color;
v_color.rgb *= clamp(lit * 1.5, 0.5, 1.5);
gl_Position = u_modelViewProj * a_position;

v_normal = a_normal;

v_color = a_color;

float lit = dot(normalize(u_lightDirection), normalize(a_normal));
v_color.rgb *= clamp(lit * 1.5, 0.5, 1.5);

v_texcoord = a_texcoord;

gl_Position = u_modelViewProj * a_position;
}
28 changes: 28 additions & 0 deletions core/resources/polyline.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifdef GL_ES
precision mediump float;
#endif

uniform float u_time;

varying vec4 v_pos;
varying vec4 v_color;
varying vec2 v_texcoord;

void main(void) {

/*
* Use texture coordinates to darken fragments closer to the center of the polyline,
* creating an outline effect.
*/

float radius = abs(v_texcoord.x - 0.5); // distance from center of line in texture coordinates

float threshold = 0.35; // distance within which color will be darkened

float darken = step(threshold, radius); // 0.0 if radius < threshold, else 1.0

darken = clamp(darken, 0.5, 1.0); // reduce color values by 1/2 in darkened fragments

gl_FragColor = v_color * darken;

}
37 changes: 37 additions & 0 deletions core/resources/polyline.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifdef GL_ES
precision mediump float;
#endif

uniform mat4 u_modelViewProj;
uniform vec3 u_lightDirection;
uniform float u_time;

attribute vec4 a_position;
attribute vec2 a_texcoord;

attribute vec3 a_extrudeNormal;
attribute float a_extrudeWidth;

attribute vec4 a_color;

varying vec4 v_pos;
varying vec4 v_color;
varying vec3 v_normal;
varying vec2 v_texcoord;

void main() {

v_normal = vec3(0.,0.,1.);

v_color = a_color;

float lit = dot(normalize(u_lightDirection), normalize(v_normal));
v_color.rgb *= clamp(lit * 1.5, 0.5, 1.5);

v_texcoord = a_texcoord;

v_pos = a_position;
v_pos.xyz += a_extrudeNormal*a_extrudeWidth;

gl_Position = u_modelViewProj * v_pos;
}
122 changes: 122 additions & 0 deletions core/src/style/polygonStyle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "polygonStyle.h"
#include "util/builders.h"

PolygonStyle::PolygonStyle(std::string _name, GLenum _drawMode) : Style(_name, _drawMode) {

constructVertexLayout();
constructShaderProgram();

}

void PolygonStyle::constructVertexLayout() {

// TODO: Ideally this would be in the same location as the struct that it basically describes
m_vertexLayout = std::shared_ptr<VertexLayout>(new VertexLayout({
{"a_position", 3, GL_FLOAT, false, 0},
{"a_normal", 3, GL_FLOAT, false, 0},
{"a_texcoord", 2, GL_FLOAT, false, 0},
{"a_color", 4, GL_UNSIGNED_BYTE, true, 0}
}));

}

void PolygonStyle::constructShaderProgram() {

std::string vertShaderSrcStr = stringFromResource("polygon.vs");

std::string fragShaderSrcStr = stringFromResource("polygon.fs");

m_shaderProgram = std::make_shared<ShaderProgram>();
m_shaderProgram->buildFromSourceStrings(fragShaderSrcStr, vertShaderSrcStr);

}

void PolygonStyle::setup() {
m_shaderProgram->setUniformf("u_lightDirection", -1.0, -1.0, 1.0);
}

void PolygonStyle::buildPoint(Point& _point, std::string& _layer, Properties& _props, VboMesh& _mesh) {
// No-op
}

void PolygonStyle::buildLine(Line& _line, std::string& _layer, Properties& _props, VboMesh& _mesh) {
std::vector<PosNormColVertex> vertices;
std::vector<GLushort> indices;
std::vector<glm::vec3> points;
std::vector<glm::vec2> texcoords;

GLuint abgr = 0xff969696; // Default road color
float halfWidth = 0.02;

Builders::buildPolyLine(_line, halfWidth, points, indices, texcoords);

for (int i = 0; i < points.size(); i++) {
glm::vec3 p = points[i];
glm::vec3 n = glm::vec3(0.0f, 0.0f, 1.0f);
glm::vec2 u = texcoords[i];
vertices.push_back({ p.x, p.y, p.z, n.x, n.y, n.z, u.x, u.y, abgr });
}

// Make sure indices get correctly offset
int vertOffset = _mesh.numVertices();
for (auto& ind : indices) {
ind += vertOffset;
}

_mesh.addVertices((GLbyte*)vertices.data(), vertices.size());
_mesh.addIndices(indices.data(), indices.size());
}

void PolygonStyle::buildPolygon(Polygon& _polygon, std::string& _layer, Properties& _props, VboMesh& _mesh) {

std::vector<PosNormColVertex> vertices;
std::vector<GLushort> indices;
std::vector<glm::vec3> points;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> texcoords;

GLuint abgr = 0xffaaaaaa; // Default color

if (_layer.compare("buildings") == 0) {
abgr = 0xffcedcde;
} else if (_layer.compare("water") == 0) {
abgr = 0xff917d1a;
} else if (_layer.compare("roads") == 0) {
abgr = 0xff969696;
} else if (_layer.compare("earth") == 0) {
abgr = 0xff669171;
} else if (_layer.compare("landuse") == 0) {
abgr = 0xff507480;
}

float height = _props.numericProps["height"]; // Inits to zero if not present in data
float minHeight = _props.numericProps["min_height"]; // Inits to zero if not present in data

if (minHeight != height) {
for (auto& line : _polygon) {
for (auto& point : line) {
point.z = height;
}
}
Builders::buildPolygonExtrusion(_polygon, minHeight, points, normals, indices, texcoords);
}

Builders::buildPolygon(_polygon, points, normals, indices, texcoords);

for (int i = 0; i < points.size(); i++) {
glm::vec3 p = points[i];
glm::vec3 n = normals[i];
glm::vec2 u = texcoords[i];
vertices.push_back({ p.x, p.y, p.z, n.x, n.y, n.z, u.x, u.y, abgr });
}

// Make sure indices get correctly offset
int vertOffset = _mesh.numVertices();
for (auto& ind : indices) {
ind += vertOffset;
}

_mesh.addVertices((GLbyte*)vertices.data(), vertices.size());
_mesh.addIndices(indices.data(), indices.size());

}
40 changes: 40 additions & 0 deletions core/src/style/polygonStyle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "style.h"

class PolygonStyle : public Style {

protected:

struct PosNormColVertex {
// Position Data
GLfloat pos_x;
GLfloat pos_y;
GLfloat pos_z;
// Normal Data
GLfloat norm_x;
GLfloat norm_y;
GLfloat norm_z;
// UV Data
GLfloat texcoord_x;
GLfloat texcoord_y;
// Color Data
GLuint abgr;
};

virtual void constructVertexLayout() override;
virtual void constructShaderProgram() override;
virtual void buildPoint(Point& _point, std::string& _layer, Properties& _props, VboMesh& _mesh) override;
virtual void buildLine(Line& _line, std::string& _layer, Properties& _props, VboMesh& _mesh) override;
virtual void buildPolygon(Polygon& _polygon, std::string& _layer, Properties& _props, VboMesh& _mesh) override;

public:

PolygonStyle(GLenum _drawMode = GL_TRIANGLES);
PolygonStyle(std::string _name, GLenum _drawMode = GL_TRIANGLES);

virtual void setup() override;

virtual ~PolygonStyle() {
}
};
89 changes: 89 additions & 0 deletions core/src/style/polylineStyle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "polylineStyle.h"
#include "util/builders.h"

#include <time.h>

PolylineStyle::PolylineStyle(std::string _name, GLenum _drawMode) : Style(_name, _drawMode) {

constructVertexLayout();
constructShaderProgram();

}

void PolylineStyle::constructVertexLayout() {

// TODO: Ideally this would be in the same location as the struct that it basically describes
m_vertexLayout = std::shared_ptr<VertexLayout>(new VertexLayout({
{"a_position", 3, GL_FLOAT, false, 0},
{"a_texcoord", 2, GL_FLOAT, false, 0},
{"a_extrudeNormal", 2, GL_FLOAT, false, 0},
{"a_extrudeWidth", 1, GL_FLOAT, false, 0},
{"a_color", 4, GL_UNSIGNED_BYTE, true, 0}
}));

}

void PolylineStyle::constructShaderProgram() {

std::string vertShaderSrcStr = stringFromResource("polyline.vs");
std::string fragShaderSrcStr = stringFromResource("polyline.fs");

m_shaderProgram = std::make_shared<ShaderProgram>();
m_shaderProgram->buildFromSourceStrings(fragShaderSrcStr, vertShaderSrcStr);

}

void PolylineStyle::setup() {
clock_t t = clock();
m_shaderProgram->setUniformf("u_lightDirection", -1.0, -1.0, 1.0);
m_shaderProgram->setUniformf("u_time", ((float)t)/CLOCKS_PER_SEC);
}

void PolylineStyle::buildPoint(Point& _point, std::string& _layer, Properties& _props, VboMesh& _mesh) {
// No-op
}

void PolylineStyle::buildLine(Line& _line, std::string& _layer, Properties& _props, VboMesh& _mesh) {
std::vector<PosNormEnormColVertex> vertices;
std::vector<GLushort> indices;
std::vector<glm::vec3> points;
std::vector<glm::vec2> texcoords;
std::vector<glm::vec2> scalingVecs;

GLuint abgr = 0xff969696; // Default road color
float halfWidth = 0.02;

if(_props.stringProps["kind"] == "highway"){
halfWidth = 0.02;
} else if(_props.stringProps["kind"] == "major_road"){
halfWidth = 0.015;
} else if(_props.stringProps["kind"] == "minor_road"){
halfWidth = 0.01;
} else if(_props.stringProps["kind"] == "rail"){
halfWidth = 0.002;
} else if(_props.stringProps["kind"] == "path"){
halfWidth = 0.005;
}

Builders::buildScalablePolyLine(_line, points, scalingVecs, indices, texcoords);

for (int i = 0; i < points.size(); i++) {
glm::vec3 p = points[i];
glm::vec2 uv = texcoords[i];
glm::vec2 en = scalingVecs[i];
vertices.push_back({ p.x, p.y, p.z, uv.x, uv.y, en.x, en.y, halfWidth, abgr });
}

// Make sure indices get correctly offset
int vertOffset = _mesh.numVertices();
for (auto& ind : indices) {
ind += vertOffset;
}

_mesh.addVertices((GLbyte*)vertices.data(), vertices.size());
_mesh.addIndices(indices.data(), indices.size());
}

void PolylineStyle::buildPolygon(Polygon& _polygon, std::string& _layer, Properties& _props, VboMesh& _mesh) {
// No-op
}
Loading