Skip to content

Commit

Permalink
Merge pull request #125 from 9prady9/surface_fixes
Browse files Browse the repository at this point in the history
Surface fixes and Increment version to 0.9.2
  • Loading branch information
shehzan10 authored Dec 19, 2016
2 parents f073247 + c57384d commit 175a6b2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 58 deletions.
2 changes: 1 addition & 1 deletion CMakeModules/Version.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ENDIF()

SET(FG_VERSION_MAJOR "0")
SET(FG_VERSION_MINOR "9")
SET(FG_VERSION_PATCH "1")
SET(FG_VERSION_PATCH "2")

SET(FG_VERSION "${FG_VERSION_MAJOR}.${FG_VERSION_MINOR}.${FG_VERSION_PATCH}")
SET(FG_API_VERSION_CURRENT ${FG_VERSION_MAJOR}${FG_VERSION_MINOR})
Expand Down
16 changes: 8 additions & 8 deletions examples/cpu/surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@

using namespace std;

static const float XMIN = -8.0f;
static const float XMAX = 8.f;
static const float YMIN = -8.0f;
static const float YMAX = 8.f;
static const float XMIN = -32.0f;
static const float XMAX = 32.0f;
static const float YMIN = -32.0f;
static const float YMAX = 32.0f;

const float DX = 0.5;
const float DX = 0.25;
const size_t XSIZE = (XMAX-XMIN)/DX;
const size_t YSIZE = (YMAX-YMIN)/DX;

void genSurface(float dx, std::vector<float> &vec )
{
vec.clear();
for(float x=XMIN; x < XMAX; x+=dx){
for(float y=YMIN; y < YMAX; y+=dx){
for(float x=XMIN; x < XMAX; x+=dx) {
for(float y=YMIN; y < YMAX; y+=dx) {
vec.push_back(x);
vec.push_back(y);
float z = sqrt(x*x+y*y) + 2.2204e-16;
Expand All @@ -50,7 +50,7 @@ int main(void)
wnd.makeCurrent();

forge::Chart chart(FG_CHART_3D);
chart.setAxesLimits(-10.f, 10.f, -10.f, 10.f, -0.5f, 1.f);
chart.setAxesLimits(XMIN-2.0f, XMAX+2.0f, YMIN-2.0f, YMAX+2.0f, -0.5f, 1.f);
chart.setAxesTitles("x-axis", "y-axis", "z-axis");

forge::Surface surf = chart.surface(XSIZE, YSIZE, forge::f32);
Expand Down
108 changes: 59 additions & 49 deletions src/backend/opengl/surface_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,33 @@
using namespace gl;
using namespace std;

void generateGridIndices(unsigned short rows, unsigned short cols, unsigned short *indices)
void generateGridIndices(std::vector<unsigned int>& indices,
unsigned short rows, unsigned short cols)
{
unsigned short idx = 0;
for(unsigned short r = 0; r < rows-1; ++r){
for(unsigned short c = 0; c < cols*2; ++c){
unsigned short i = c + (r * (cols*2));

if(c == cols * 2 - 1) {
*indices++ = idx;
}else{
*indices++ = idx;
if(i%2 == 0){
idx += cols;
} else {
idx -= (r%2 == 0) ? (cols-1) : (cols+1);
}
const int numDegens = 2 * (rows - 2);
const int verticesPerStrip = 2 * cols;

//reserve the size of vector
indices.reserve(verticesPerStrip + numDegens);

for (int r = 0; r < (rows-1); ++r) {
if (r > 0) {
// repeat first vertex for degenerate triangle
indices.push_back(r*rows);
}

for (int c = 0; c < cols; ++c) {
// One part of the strip
indices.push_back(r*rows + c);
indices.push_back((r+1)*rows + c);
}

if (r < (rows-2)) {
// repeat last vertex for degenerate triangle
indices.push_back(((r + 1) * rows) + (cols - 1));
}
}
}
}

namespace forge
{
Expand All @@ -51,36 +58,36 @@ namespace opengl

void surface_impl::bindResources(const int pWindowId)
{
if (mVAOMap.find(pWindowId) == mVAOMap.end()) {
GLuint vao = 0;
/* create a vertex array object
* with appropriate bindings */
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// attach plot vertices
glEnableVertexAttribArray(mSurfPointIndex);
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
glVertexAttribPointer(mSurfPointIndex, 3, mDataType, GL_FALSE, 0, 0);
glEnableVertexAttribArray(mSurfColorIndex);
glBindBuffer(GL_ARRAY_BUFFER, mCBO);
glVertexAttribPointer(mSurfColorIndex, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(mSurfAlphaIndex);
glBindBuffer(GL_ARRAY_BUFFER, mABO);
glVertexAttribPointer(mSurfAlphaIndex, 1, GL_FLOAT, GL_FALSE, 0, 0);
//attach indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIBO);
glBindVertexArray(0);
/* store the vertex array object corresponding to
* the window instance in the map */
mVAOMap[pWindowId] = vao;
}
if (mVAOMap.find(pWindowId) == mVAOMap.end()) {
GLuint vao = 0;
/* create a vertex array object
* with appropriate bindings */
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// attach plot vertices
glEnableVertexAttribArray(mSurfPointIndex);
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
glVertexAttribPointer(mSurfPointIndex, 3, mDataType, GL_FALSE, 0, 0);
glEnableVertexAttribArray(mSurfColorIndex);
glBindBuffer(GL_ARRAY_BUFFER, mCBO);
glVertexAttribPointer(mSurfColorIndex, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(mSurfAlphaIndex);
glBindBuffer(GL_ARRAY_BUFFER, mABO);
glVertexAttribPointer(mSurfAlphaIndex, 1, GL_FLOAT, GL_FALSE, 0, 0);
//attach indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIBO);
glBindVertexArray(0);
/* store the vertex array object corresponding to
* the window instance in the map */
mVAOMap[pWindowId] = vao;
}

glBindVertexArray(mVAOMap[pWindowId]);
glBindVertexArray(mVAOMap[pWindowId]);
}

void surface_impl::unbindResources() const
{
glBindVertexArray(0);
glBindVertexArray(0);
}

glm::mat4 surface_impl::computeTransformMat(const glm::mat4& pView, const glm::mat4& pOrient)
Expand Down Expand Up @@ -120,11 +127,11 @@ void surface_impl::renderGraph(const int pWindowId, const glm::mat4& transform)
glUniform1i(mSurfPVAIndex, mIsPVAOn);

bindResources(pWindowId);
glDrawElements(GL_TRIANGLE_STRIP, mIBOSize, GL_UNSIGNED_SHORT, (void*)0 );
glDrawElements(GL_TRIANGLE_STRIP, mIBOSize, GL_UNSIGNED_INT, (void*)0);
unbindResources();
mSurfProgram.unbind();

if(mMarkerType != FG_MARKER_NONE) {
if (mMarkerType != FG_MARKER_NONE) {
glEnable(GL_PROGRAM_POINT_SIZE);
mMarkerProgram.bind();

Expand All @@ -135,7 +142,7 @@ void surface_impl::renderGraph(const int pWindowId, const glm::mat4& transform)
glUniform4fv(mMarkerColIndex, 1, mColor);

bindResources(pWindowId);
glDrawElements(GL_POINTS, mIBOSize, GL_UNSIGNED_SHORT, (void*)0);
glDrawElements(GL_POINTS, mIBOSize, GL_UNSIGNED_INT, (void*)0);
unbindResources();

mMarkerProgram.unbind();
Expand Down Expand Up @@ -201,10 +208,13 @@ surface_impl::surface_impl(unsigned pNumXPoints, unsigned pNumYPoints,

#undef SURF_CREATE_BUFFERS

mIBOSize = (2 * mNumYPoints) * (mNumXPoints - 1);
std::vector<ushort> indices(mIBOSize);
generateGridIndices(mNumXPoints, mNumYPoints, indices.data());
mIBO = createBuffer<ushort>(GL_ELEMENT_ARRAY_BUFFER, mIBOSize, indices.data(), GL_STATIC_DRAW);
std::vector<unsigned int> indices;

generateGridIndices(indices, mNumXPoints, mNumYPoints);

mIBOSize = indices.size();

mIBO = createBuffer<uint>(GL_ELEMENT_ARRAY_BUFFER, mIBOSize, indices.data(), GL_STATIC_DRAW);

CheckGL("End surface_impl::surface_impl");
}
Expand Down Expand Up @@ -256,7 +266,7 @@ void scatter3_impl::renderGraph(const int pWindowId, const glm::mat4& transform)
glUniform4fv(mMarkerColIndex, 1, mColor);

bindResources(pWindowId);
glDrawElements(GL_POINTS, mIBOSize, GL_UNSIGNED_SHORT, (void*)0);
glDrawElements(GL_POINTS, mIBOSize, GL_UNSIGNED_INT, (void*)0);
unbindResources();

mMarkerProgram.unbind();
Expand Down

0 comments on commit 175a6b2

Please sign in to comment.