Skip to content

Commit

Permalink
Merge pull request #17 from stacompsoc/frontend/opengl-rewrite
Browse files Browse the repository at this point in the history
OpenGL Frontend optimized drawing shapes
  • Loading branch information
theoden8 authored Apr 5, 2017
2 parents f2e6857 + 22e6aff commit f0bd7f5
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 179 deletions.
1 change: 1 addition & 0 deletions src/frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ planetarium
*.jpg
*.png
*.log
*.trace
textures/sam.tga
55 changes: 35 additions & 20 deletions src/frontend/Quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ void Quad::Init() {
AddTriangle(c, d, a, 1, true);
}

void Quad::SetTexcoords(size_t index, GLfloat texcoords[6]) {
if(index == 0) {
GLfloat tmp[6] = {
0, 0,
0, 1,
1, 1,
};
memcpy(texcoords, tmp, 6 * sizeof(GLfloat));
} else {
GLfloat tmp[6] = {
1, 1,
1, 0,
0, 0,
};
memcpy(texcoords, tmp, 6 * sizeof(GLfloat));
}
gl_log("%.2f,%.2f\n", texcoords[0], texcoords[1]);
gl_log("%.2f,%.2f\n", texcoords[2], texcoords[3]);
gl_log("%.2f,%.2f\n", texcoords[4], texcoords[5]);
}

void Quad::AddTriangle(
const glm::vec3 &a,
const glm::vec3 &b,
Expand All @@ -38,25 +59,19 @@ void Quad::AddTriangle(
size_t color = 0;
triangles.back().Init(buffer, color);
} else {
GLfloat cpy[6];
if(index == 0) {
GLfloat texcoords[6] = {
0, 0,
0, 1,
1, 1,
};
memcpy(cpy, texcoords, 6 * sizeof(GLfloat));
} else {
GLfloat texcoords[6] = {
1, 1,
1, 0,
0, 0,
};
memcpy(cpy, texcoords, 6 * sizeof(GLfloat));
}
gl_log("%.2f,%.2f\n", cpy[0], cpy[1]);
gl_log("%.2f,%.2f\n", cpy[2], cpy[3]);
gl_log("%.2f,%.2f\n", cpy[4], cpy[5]);
triangles.back().Init(buffer, cpy);
gl_log("is textured %d\n", is_textured);
GLfloat texcoords[6];
SetTexcoords(index, texcoords);
triangles.back().Init(buffer, texcoords);
}
}

void Quad::Draw() {
for(auto &t : triangles)
t.Draw();
}

void Quad::Clear() {
for(auto &t : triangles)
t.Clear();
}
7 changes: 5 additions & 2 deletions src/frontend/Quad.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "Shape.hpp"
#include "Triangle.hpp"

class Quad : public Shape {
protected:
std::vector <Triangle> triangles;
void SetTexcoords(size_t index, GLfloat texcoords[6]);
void AddTriangle(
const glm::vec3 &a,
const glm::vec3 &b,
Expand All @@ -13,6 +16,6 @@ class Quad : public Shape {
Quad();
~Quad();
void Init();
using Shape::Draw;
using Shape::Clear;
void Draw();
void Clear();
};
140 changes: 89 additions & 51 deletions src/frontend/Ring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>

template <int N> const size_t Ring<N>::DIM = 300;
template <int N> const size_t Ring<N>::SIZE = DIM * DIM * 2 * 2;

template <int N>
Ring<N>::Ring():
Shape()
Expand All @@ -19,72 +22,107 @@ glm::vec3 point_on_circle(float angle) {

template <int N>
void Ring<N>::Init() {
float ir = float(10-N) / 10;
double step = float(M_PI*2) / DIM;
glGenVertexArrays(1, &vao); GLERROR
glBindVertexArray(vao); GLERROR

txcoords = new GLfloat[SIZE * 6];
ASSERT(txcoords != NULL);
vertices = new GLfloat[SIZE * 9];
ASSERT(vertices != NULL);

InitBuffers();

glGenBuffers(1, &vert_vbo); GLERROR
glBindBuffer(GL_ARRAY_BUFFER, vert_vbo); GLERROR
glBufferData(GL_ARRAY_BUFFER, SIZE * sizeof(GLfloat) * 9, vertices, GL_STREAM_DRAW); GLERROR

glGenBuffers(1, &tex_vbo); GLERROR
glBindBuffer(GL_ARRAY_BUFFER, tex_vbo); GLERROR
glBufferData(GL_ARRAY_BUFFER, SIZE * sizeof(GLfloat) * 6, txcoords, GL_STREAM_DRAW); GLERROR

ASSERT(vertices != NULL);
delete vertices;
ASSERT(txcoords != NULL);
delete txcoords;

glEnableVertexAttribArray(0); GLERROR
glBindBuffer(GL_ARRAY_BUFFER, vert_vbo); GLERROR
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); GLERROR

glEnableVertexAttribArray(1); GLERROR
glBindBuffer(GL_ARRAY_BUFFER, tex_vbo); GLERROR
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL); GLERROR

glVertexAttribDivisor(0, 0); GLERROR
glVertexAttribDivisor(1, 0); GLERROR
}

template <int N>
void Ring<N>::InitBuffers() {
const static float ir = float(10-N) / 10;
const double step = float(M_PI*2) / DIM;
size_t index = 0;
double angle = 0.;
for(int i = 0; i < DIM; ++i) {
for(size_t i = 0; i < DIM; ++i) {
glm::vec3
&&a = point_on_circle(angle),
&&b = point_on_circle(angle + step),
&&c = a * ir;
AddTriangle(a, b, c, index, true);
++index;
SetTexcoords(index);
SetVertices(a, b, c, index), ++index;
a = b * ir;
AddTriangle(c, b, a, index, true);
++index;
SetTexcoords(index);
SetVertices(c, b, a, index), ++index;
angle += step;
}
}

template <int N>
void Ring<N>::AddTriangle(
const glm::vec3 &a,
const glm::vec3 &b,
const glm::vec3 &c,
size_t index,
bool is_textured)
{
gl_log("add triangle\n");
GLfloat buffer[9];
gl_log("%.2f,%.2f,%.2f\n", a.x,a.y,a.z);
gl_log("%.2f,%.2f,%.2f\n", b.x,b.y,b.z);
gl_log("%.2f,%.2f,%.2f\n", c.x,c.y,c.z);
void Ring<N>::SetTexcoords(int index) {
ASSERT(index < SIZE);
size_t x = index / 2;
ASSERT(x < DIM);
float xstep = 1. / DIM;
float tx0 = float(x) / DIM, tx1 = tx0 + xstep;
GLfloat *buffer = &txcoords[index * 6];
if(index & 1) {
buffer[0] = tx1, buffer[1] = 1,
buffer[2] = tx0, buffer[3] = 0,
buffer[4] = tx0, buffer[5] = 1;
} else {
buffer[0] = tx1, buffer[1] = 0,
buffer[2] = tx0, buffer[3] = 0,
buffer[4] = tx1, buffer[5] = 1;
}
gl_log("%.2f,%.2f\n", buffer[0], buffer[1]);
gl_log("%.2f,%.2f\n", buffer[2], buffer[3]);
gl_log("%.2f,%.2f\n", buffer[4], buffer[5]);
}

template <int N>
void Ring<N>::SetVertices(const glm::vec3 &a, const glm::vec3 &b, const glm::vec3 &c, size_t index) {
ASSERT(index < SIZE);
GLfloat *buffer = &vertices[index * 9];
memcpy(buffer, glm::value_ptr(a), sizeof(GLfloat) * 3);
memcpy(buffer + 3, glm::value_ptr(b), sizeof(GLfloat) * 3);
memcpy(buffer + 6, glm::value_ptr(c), sizeof(GLfloat) * 3);
triangles.push_back(Triangle());
if(!is_textured) {
gl_log("is textured %d\n", is_textured);
size_t color = 0;
triangles.back().Init(buffer, color);
} else {
gl_log("is textured %d\n", is_textured);
size_t x = index / 2;
ASSERT(x < DIM);
float xstep = 1. / DIM;
float tx0 = float(x) / DIM, tx1 = tx0 + xstep;
GLfloat cpy[6];
if(index & 1) {
GLfloat texcoords[6] = {
tx1, 1,
tx0, 0,
tx0, 1,
};
memcpy(cpy, texcoords, 6 * sizeof(GLfloat));
} else {
GLfloat texcoords[6] = {
tx1, 0,
tx0, 0,
tx1, 1,
};
memcpy(cpy, texcoords, 6 * sizeof(GLfloat));
}
gl_log("%.2f,%.2f\n", cpy[0], cpy[1]);
gl_log("%.2f,%.2f\n", cpy[2], cpy[3]);
gl_log("%.2f,%.2f\n", cpy[4], cpy[5]);
triangles.back().Init(buffer, cpy);
}
gl_log("adding triangle_strip\n");
gl_log("%.2f,%.2f,%.2f\n", a.x,a.y,a.z);
gl_log("%.2f,%.2f,%.2f\n", b.x,b.y,b.z);
gl_log("%.2f,%.2f,%.2f\n", c.x,c.y,c.z);
}

template <int N>
void Ring<N>::Draw() {
glBindVertexArray(vao); GLERROR
glDrawArrays(GL_TRIANGLES, 0, SIZE * 3); GLERROR
}

template <int N>
void Ring<N>::Clear() {
glDeleteBuffers(1, &vert_vbo); GLERROR
glDeleteBuffers(1, &tex_vbo); GLERROR
glDeleteVertexArrays(1, &vao); GLERROR
}

template class Ring<1>;
Expand Down
24 changes: 14 additions & 10 deletions src/frontend/Ring.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
#pragma once

#include "Shape.hpp"
#include "Triangle.hpp"

template <int N>
class Ring : public Shape {
protected:
const int DIM = 60;
void AddTriangle(
const glm::vec3 &a,
const glm::vec3 &b,
const glm::vec3 &c,
size_t index,
bool is_textured = false
);
GLuint vao = 0;
GLuint vert_vbo = 0;
GLuint tex_vbo = 0;
GLfloat
*vertices = NULL,
*txcoords = NULL;
static const size_t DIM;
static const size_t SIZE;
void SetTexcoords(int index);
void SetVertices(const glm::vec3 &a, const glm::vec3 &b, const glm::vec3 &c, size_t index);
public:
Ring();
~Ring();
void Init();
using Shape::Draw;
using Shape::Clear;
void InitBuffers();
void Draw();
void Clear();
};
14 changes: 1 addition & 13 deletions src/frontend/Shape.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
#include "Shape.hpp"
#include "Log.hpp"

Shape::Shape():
triangles(0)
Shape::Shape()
{}

Shape::~Shape()
{}

void Shape::Draw() {
for(auto &t : triangles)
t.Draw();
}

void Shape::Clear() {
for(auto &t : triangles)
t.Clear();
triangles.clear();
}
15 changes: 2 additions & 13 deletions src/frontend/Shape.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
#pragma once

#include "Triangle.hpp"

class Shape {
protected:
std::vector <Triangle> triangles;
virtual void AddTriangle(
const glm::vec3 &a,
const glm::vec3 &b,
const glm::vec3 &c,
size_t index,
bool is_textured = false
) = 0;
public:
Shape();
virtual ~Shape();
virtual void Init() = 0;
virtual void Draw();
virtual void Clear();
virtual void Draw() = 0;
virtual void Clear() = 0;
};
2 changes: 1 addition & 1 deletion src/frontend/Space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void Space::Setup(float width, float height) {
/* instance->AddObject( */
/* Object( */
/* SPHERE, instance->planet_program, EARTH, */
/* 0.2f, 0,0,0, */
/* 1.0f, 0,0,0, */
/* 0.0f */
/* ) */
/* ); */
Expand Down
Loading

0 comments on commit f0bd7f5

Please sign in to comment.