Skip to content
This repository has been archived by the owner on Mar 2, 2018. It is now read-only.

Commit

Permalink
Merge pull request #41 from googlesamples/release-quine
Browse files Browse the repository at this point in the history
release-quine
  • Loading branch information
jguomoto committed Jun 25, 2015
2 parents 17cc3c9 + 3be6258 commit 7235635
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 41 deletions.
10 changes: 6 additions & 4 deletions point-cloud-jni-example/app/src/main/jni/tango_pointcloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,12 @@ bool RenderFrame() {
axis->Render(cam->GetProjectionMatrix(), cam->GetViewMatrix());

// Render point cloud based on depth buffer and depth frame transformation.
pointcloud->Render(
cam->GetProjectionMatrix(), cam->GetViewMatrix(), oc_2_ow_mat_depth,
TangoData::GetInstance().depth_buffer_size * 3,
static_cast<float*>(TangoData::GetInstance().depth_buffer));
if (TangoData::GetInstance().cur_pose_data.status_code == TANGO_POSE_VALID) {
pointcloud->Render(
cam->GetProjectionMatrix(), cam->GetViewMatrix(), oc_2_ow_mat_depth,
TangoData::GetInstance().depth_buffer_size * 3,
static_cast<float*>(TangoData::GetInstance().depth_buffer));
}

grid->SetPosition(glm::vec3(0.0f, 0.0f, 0.0f) - kHeightOffset);
// Render grid.
Expand Down
37 changes: 27 additions & 10 deletions tango-gl/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,38 @@
namespace tango_gl {

static const GLfloat const_vertices[] = {
-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f};
-1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f};

static const GLushort const_indices[] = {0, 1, 2, 2, 3, 0, 3, 2, 6, 6, 7, 3,
7, 6, 5, 5, 4, 7, 4, 0, 3, 3, 7, 4,
0, 4, 5, 5, 1, 0, 1, 5, 6, 6, 2, 1};
static const GLfloat const_normals[] = {
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, -1.0f,
0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f};

Cube::Cube() {
SetShader();
SetShader(true);
std::vector<GLfloat> vertices(
const_vertices,
const_vertices + sizeof(const_vertices) / sizeof(GLfloat));
std::vector<GLushort> indices(
const_indices, const_indices + sizeof(const_indices) / sizeof(GLushort));
SetVertices(vertices, indices);
std::vector<GLfloat> normals(
const_normals, const_normals + sizeof(const_normals) / sizeof(GLfloat));
SetVertices(vertices, normals);
glm::vec3 point_light = glm::vec3(0.5f, 2, 0.5f);
SetLightPosition(point_light);
}
} // namespace tango_gl
6 changes: 6 additions & 0 deletions tango-gl/drawable_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ void DrawableObject::SetVertices(const std::vector<GLfloat>& vertices,
vertices_ = vertices;
indices_ = indices;
}

void DrawableObject::SetVertices(const std::vector<GLfloat>& vertices,
const std::vector<GLfloat>& normals) {
vertices_ = vertices;
normals_ = normals;
}
} // namespace tango_gl
5 changes: 5 additions & 0 deletions tango-gl/include/tango-gl/drawable_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class DrawableObject : public Transform {
void SetVertices(const std::vector<GLfloat>& vertices);
void SetVertices(const std::vector<GLfloat>& vertices,
const std::vector<GLushort>& indices);
void SetVertices(const std::vector<GLfloat>& vertices,
const std::vector<GLfloat>& normals);
virtual void Render(const glm::mat4& projection_mat,
const glm::mat4& view_mat) const = 0;

Expand All @@ -48,10 +50,13 @@ class DrawableObject : public Transform {
float alpha_;
std::vector<GLushort> indices_;
std::vector<GLfloat> vertices_;
std::vector<GLfloat> normals_;

GLuint shader_program_;
GLuint uniform_color_;
GLuint uniform_mvp_mat_;
GLuint attrib_vertices_;
GLuint attrib_normals_;
};
} // namespace tango_gl
#endif // TANGO_GL_DRAWABLE_OBJECT_H_
9 changes: 9 additions & 0 deletions tango-gl/include/tango-gl/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@
namespace tango_gl {
class Mesh : public DrawableObject {
public:
void SetShader();
void SetShader(bool is_lighting_on);
void SetLightPosition(const glm::vec3& light_position);
void Render(const glm::mat4& projection_mat, const glm::mat4& view_mat) const;

protected:
bool is_lighting_on_;
glm::vec3 light_position_;
GLuint uniform_mv_mat_;
GLuint uniform_light_pos_;
};
} // namespace tango_gl
#endif // TANGO_GL_MESH_H_
29 changes: 20 additions & 9 deletions tango-gl/include/tango-gl/obj_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,38 @@

namespace tango_gl {
namespace obj_loader {
// Load standard .obj file into vertices, indices and normals vectors,
// Load standard .obj file into vertices, indices or normals vectors,
// OBJ file can be exported from 3D tools like 3ds Max, when exporting,
// faces must be triangulated. A readable file should look like
// faces must be triangulated.
// A readable file with only vertices should look like
// "v 1.00 2.00 3.00
// ...
// f 1 2 3
// ..."
//
// If exported with normals, file should look like
// "v 1.00 2.00 3.00
// ...
// f 1//1 2//3 3//4
// ...
// vn 1.00 2.00 3.00
// ..."
//
// this can be used with Polygon:
// this can be used with Mesh:
//
// std::vector<GLfloat> vertices;
// std::vector<GLushort> indices;
// std::vector<GLfloat> normals;
// tango_gl::obj_loader::LoadOBJData("/sdcard/model.obj", vertices, indices,
// normals);
// polygon->SetMeshData(vertices, indices);
//
// tango_gl::obj_loader::LoadOBJData("/sdcard/model.obj", vertices, indices);
// mesh->SetVertices(vertices, indices);
// or
// tango_gl::obj_loader::LoadOBJData("/sdcard/model.obj", vertices, normals);
// mesh->SetVertices(vertices, normals);

bool LoadOBJData(const char* path, std::vector<GLfloat>& vertices,
std::vector<GLushort>& indices);

bool LoadOBJData(const char* path, std::vector<GLfloat>& vertices,
std::vector<GLushort>& indices, std::vector<GLfloat>& normals);
std::vector<GLfloat>& normals);
} // namespace obj_loader
} // namespace tango_gl
#endif // TANGO_GL_OBJ_LOADER
1 change: 1 addition & 0 deletions tango-gl/include/tango-gl/shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ std::string GetBasicFragmentShader();
std::string GetColorVertexShader();
std::string GetVideoOverlayVertexShader();
std::string GetVideoOverlayFragmentShader();
std::string GetShadedVertexShader();
} // namespace shaders
} // namespace tango_gl
#endif // TANGO_GL_SHADERS_H_
68 changes: 60 additions & 8 deletions tango-gl/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,78 @@
*/

#include "tango-gl/mesh.h"
#include "tango-gl/shaders.h"

namespace tango_gl {
void Mesh::SetShader() {
DrawableObject::SetShader();
is_lighting_on_ = false;
}

void Mesh::SetShader(bool is_lighting_on) {
if (is_lighting_on) {
shader_program_ =
util::CreateProgram(shaders::GetShadedVertexShader().c_str(),
shaders::GetBasicFragmentShader().c_str());
if (!shader_program_) {
LOGE("Could not create program.");
}
uniform_mvp_mat_ = glGetUniformLocation(shader_program_, "mvp");
uniform_mv_mat_ = glGetUniformLocation(shader_program_, "mv");
uniform_light_pos_ = glGetUniformLocation(shader_program_, "lightPos");
uniform_color_ = glGetUniformLocation(shader_program_, "color");

attrib_vertices_ = glGetAttribLocation(shader_program_, "vertex");
attrib_normals_ = glGetAttribLocation(shader_program_, "normal");
is_lighting_on_ = true;
} else {
SetShader();
}
}

void Mesh::SetLightPosition(const glm::vec3& light_position) {
light_position_ = light_position;
}

void Mesh::Render(const glm::mat4& projection_mat,
const glm::mat4& view_mat) const {
if (indices_.empty()) return;
glUseProgram(shader_program_);
glm::mat4 model_mat = GetTransformationMatrix();
glm::mat4 mvp_mat = projection_mat * view_mat * model_mat;
glm::mat4 mv_mat = view_mat * model_mat;
glm::mat4 mvp_mat = projection_mat * mv_mat;
glUniformMatrix4fv(uniform_mvp_mat_, 1, GL_FALSE, glm::value_ptr(mvp_mat));
glUniform4f(uniform_color_, red_, green_, blue_, alpha_);

if (is_lighting_on_) {
glUniformMatrix4fv(uniform_mv_mat_, 1, GL_FALSE, glm::value_ptr(mv_mat));

glEnableVertexAttribArray(attrib_normals_);
glVertexAttribPointer(attrib_normals_, 3, GL_FLOAT, GL_FALSE,
3 * sizeof(GLfloat), &normals_[0]);

glm::vec4 light = glm::vec4(light_position_.x, light_position_.y,
light_position_.z, 1.0f);
glm::vec4 transformed_light = view_mat * light;
glUniform3fv(uniform_light_pos_, 1, glm::value_ptr(transformed_light));
}

glEnableVertexAttribArray(attrib_vertices_);
glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE,
3 * sizeof(GLfloat), vertices_.data());
glUniform4f(uniform_color_, red_, green_, blue_, alpha_);
glDrawElements(GL_TRIANGLES, indices_.size(), GL_UNSIGNED_SHORT,
indices_.data());

if (!indices_.empty()) {
glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE,
3 * sizeof(GLfloat), vertices_.data());
glDrawElements(GL_TRIANGLES, indices_.size(), GL_UNSIGNED_SHORT,
indices_.data());
} else {
glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE,
3 * sizeof(GLfloat), &vertices_[0]);
glDrawArrays(GL_TRIANGLES, 0, vertices_.size() / 3);
}

glDisableVertexAttribArray(attrib_vertices_);
if (is_lighting_on_) {
glDisableVertexAttribArray(attrib_normals_);
}
glUseProgram(0);
}

} // namespace tango_gl
82 changes: 72 additions & 10 deletions tango-gl/obj_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "tango-gl/obj_loader.h"

namespace tango_gl {
bool obj_loader::LoadOBJData(const char *path, std::vector<GLfloat>& vertices,
std::vector<GLushort>& indices, std::vector<GLfloat>& normals) {
bool obj_loader::LoadOBJData(const char* path, std::vector<GLfloat>& vertices,
std::vector<GLushort>& indices) {
FILE *file = fopen(path, "r");
if (file == NULL) {
LOGE("Failed to open file: %s", path);
Expand All @@ -43,15 +43,51 @@ bool obj_loader::LoadOBJData(const char *path, std::vector<GLfloat>& vertices,
vertices.push_back(vertex[2]);
} else if (strcmp(lineHeader, "f") == 0) {
GLushort vertexIndex[3];
int matches = fscanf(file, "%hu %hu %hu\n", &vertexIndex[0], &vertexIndex[1],
&vertexIndex[2]);
int matches = fscanf(file, "%hu %hu %hu\n", &vertexIndex[0],
&vertexIndex[1], &vertexIndex[2]);
if (matches != 3) {
LOGE("Format of 'f int int int' required for each face line");
return false;
}
indices.push_back(vertexIndex[0] - 1);
indices.push_back(vertexIndex[1] - 1);
indices.push_back(vertexIndex[2] - 1);
}
else {
char comments_buffer[1000];
fgets(comments_buffer, 1000, file);
}
}
fclose(file);
return true;
}

bool obj_loader::LoadOBJData(const char* path, std::vector<GLfloat>& vertices,
std::vector<GLfloat>& normals) {
std::vector<unsigned int> vertexIndices, normalIndices;
std::vector<GLfloat> temp_vertices, temp_normals;

FILE* file = fopen(path, "r");
if (file == NULL) {
LOGE("Failed to open file: %s", path);
return false;
}

while (1) {
char lineHeader[128];
int res = fscanf(file, "%s", lineHeader);
if (res == EOF) break;
if (strcmp(lineHeader, "v") == 0) {
GLfloat vertex[3];
int matches =
fscanf(file, "%f %f %f\n", &vertex[0], &vertex[1], &vertex[2]);
if (matches != 3) {
LOGE("Format of 'v float float float' required for each vertice line");
return false;
}
temp_vertices.push_back(vertex[0]);
temp_vertices.push_back(vertex[1]);
temp_vertices.push_back(vertex[2]);
} else if (strcmp(lineHeader, "vn") == 0) {
GLfloat normal[3];
int matches =
Expand All @@ -60,18 +96,44 @@ bool obj_loader::LoadOBJData(const char *path, std::vector<GLfloat>& vertices,
LOGE("Format of 'vn float float float' required for each normal line");
return false;
}
normals.push_back(normal[0]);
normals.push_back(normal[1]);
normals.push_back(normal[2]);
}
else {
temp_normals.push_back(normal[0]);
temp_normals.push_back(normal[1]);
temp_normals.push_back(normal[2]);
} else if (strcmp(lineHeader, "f") == 0) {
GLushort vertexIndex[3];
GLushort normalIndex[3];
int matches = fscanf(file, "%hu//%hu %hu//%hu %hu//%hu\n",
&vertexIndex[0], &normalIndex[0], &vertexIndex[1],
&normalIndex[1], &vertexIndex[2], &normalIndex[2]);
if (matches != 6) {
LOGE("Format of 'f int//int int//int int//int' required for each face");
return false;
}
// .obj file is 1-indexed, so subtract 1 from all indices.
vertexIndices.push_back(vertexIndex[0] - 1);
vertexIndices.push_back(vertexIndex[1] - 1);
vertexIndices.push_back(vertexIndex[2] - 1);
normalIndices.push_back(normalIndex[0] - 1);
normalIndices.push_back(normalIndex[1] - 1);
normalIndices.push_back(normalIndex[2] - 1);
} else {
char comments_buffer[1000];
fgets(comments_buffer, 1000, file);
}
}

for (unsigned int i = 0; i < vertexIndices.size(); i++) {
unsigned int vertexIndex = vertexIndices[i];
unsigned int normalIndex = normalIndices[i];

vertices.push_back(temp_vertices[vertexIndex * 3]);
vertices.push_back(temp_vertices[vertexIndex * 3 + 1]);
vertices.push_back(temp_vertices[vertexIndex * 3 + 2]);
normals.push_back(temp_normals[normalIndex * 3]);
normals.push_back(temp_normals[normalIndex * 3 + 1]);
normals.push_back(temp_normals[normalIndex * 3 + 2]);
}
fclose(file);
return true;
}

} // namespace tango_gl
Loading

0 comments on commit 7235635

Please sign in to comment.