Skip to content

Commit 175a6b2

Browse files
authored
Merge pull request #125 from 9prady9/surface_fixes
Surface fixes and Increment version to 0.9.2
2 parents f073247 + c57384d commit 175a6b2

File tree

3 files changed

+68
-58
lines changed

3 files changed

+68
-58
lines changed

CMakeModules/Version.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ENDIF()
99

1010
SET(FG_VERSION_MAJOR "0")
1111
SET(FG_VERSION_MINOR "9")
12-
SET(FG_VERSION_PATCH "1")
12+
SET(FG_VERSION_PATCH "2")
1313

1414
SET(FG_VERSION "${FG_VERSION_MAJOR}.${FG_VERSION_MINOR}.${FG_VERSION_PATCH}")
1515
SET(FG_API_VERSION_CURRENT ${FG_VERSION_MAJOR}${FG_VERSION_MINOR})

examples/cpu/surface.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@
1717

1818
using namespace std;
1919

20-
static const float XMIN = -8.0f;
21-
static const float XMAX = 8.f;
22-
static const float YMIN = -8.0f;
23-
static const float YMAX = 8.f;
20+
static const float XMIN = -32.0f;
21+
static const float XMAX = 32.0f;
22+
static const float YMIN = -32.0f;
23+
static const float YMAX = 32.0f;
2424

25-
const float DX = 0.5;
25+
const float DX = 0.25;
2626
const size_t XSIZE = (XMAX-XMIN)/DX;
2727
const size_t YSIZE = (YMAX-YMIN)/DX;
2828

2929
void genSurface(float dx, std::vector<float> &vec )
3030
{
3131
vec.clear();
32-
for(float x=XMIN; x < XMAX; x+=dx){
33-
for(float y=YMIN; y < YMAX; y+=dx){
32+
for(float x=XMIN; x < XMAX; x+=dx) {
33+
for(float y=YMIN; y < YMAX; y+=dx) {
3434
vec.push_back(x);
3535
vec.push_back(y);
3636
float z = sqrt(x*x+y*y) + 2.2204e-16;
@@ -50,7 +50,7 @@ int main(void)
5050
wnd.makeCurrent();
5151

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

5656
forge::Surface surf = chart.surface(XSIZE, YSIZE, forge::f32);

src/backend/opengl/surface_impl.cpp

+59-49
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,33 @@
2323
using namespace gl;
2424
using namespace std;
2525

26-
void generateGridIndices(unsigned short rows, unsigned short cols, unsigned short *indices)
26+
void generateGridIndices(std::vector<unsigned int>& indices,
27+
unsigned short rows, unsigned short cols)
2728
{
28-
unsigned short idx = 0;
29-
for(unsigned short r = 0; r < rows-1; ++r){
30-
for(unsigned short c = 0; c < cols*2; ++c){
31-
unsigned short i = c + (r * (cols*2));
32-
33-
if(c == cols * 2 - 1) {
34-
*indices++ = idx;
35-
}else{
36-
*indices++ = idx;
37-
if(i%2 == 0){
38-
idx += cols;
39-
} else {
40-
idx -= (r%2 == 0) ? (cols-1) : (cols+1);
41-
}
29+
const int numDegens = 2 * (rows - 2);
30+
const int verticesPerStrip = 2 * cols;
31+
32+
//reserve the size of vector
33+
indices.reserve(verticesPerStrip + numDegens);
34+
35+
for (int r = 0; r < (rows-1); ++r) {
36+
if (r > 0) {
37+
// repeat first vertex for degenerate triangle
38+
indices.push_back(r*rows);
39+
}
40+
41+
for (int c = 0; c < cols; ++c) {
42+
// One part of the strip
43+
indices.push_back(r*rows + c);
44+
indices.push_back((r+1)*rows + c);
45+
}
46+
47+
if (r < (rows-2)) {
48+
// repeat last vertex for degenerate triangle
49+
indices.push_back(((r + 1) * rows) + (cols - 1));
4250
}
4351
}
4452
}
45-
}
4653

4754
namespace forge
4855
{
@@ -51,36 +58,36 @@ namespace opengl
5158

5259
void surface_impl::bindResources(const int pWindowId)
5360
{
54-
if (mVAOMap.find(pWindowId) == mVAOMap.end()) {
55-
GLuint vao = 0;
56-
/* create a vertex array object
57-
* with appropriate bindings */
58-
glGenVertexArrays(1, &vao);
59-
glBindVertexArray(vao);
60-
// attach plot vertices
61-
glEnableVertexAttribArray(mSurfPointIndex);
62-
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
63-
glVertexAttribPointer(mSurfPointIndex, 3, mDataType, GL_FALSE, 0, 0);
64-
glEnableVertexAttribArray(mSurfColorIndex);
65-
glBindBuffer(GL_ARRAY_BUFFER, mCBO);
66-
glVertexAttribPointer(mSurfColorIndex, 3, GL_FLOAT, GL_FALSE, 0, 0);
67-
glEnableVertexAttribArray(mSurfAlphaIndex);
68-
glBindBuffer(GL_ARRAY_BUFFER, mABO);
69-
glVertexAttribPointer(mSurfAlphaIndex, 1, GL_FLOAT, GL_FALSE, 0, 0);
70-
//attach indices
71-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIBO);
72-
glBindVertexArray(0);
73-
/* store the vertex array object corresponding to
74-
* the window instance in the map */
75-
mVAOMap[pWindowId] = vao;
76-
}
61+
if (mVAOMap.find(pWindowId) == mVAOMap.end()) {
62+
GLuint vao = 0;
63+
/* create a vertex array object
64+
* with appropriate bindings */
65+
glGenVertexArrays(1, &vao);
66+
glBindVertexArray(vao);
67+
// attach plot vertices
68+
glEnableVertexAttribArray(mSurfPointIndex);
69+
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
70+
glVertexAttribPointer(mSurfPointIndex, 3, mDataType, GL_FALSE, 0, 0);
71+
glEnableVertexAttribArray(mSurfColorIndex);
72+
glBindBuffer(GL_ARRAY_BUFFER, mCBO);
73+
glVertexAttribPointer(mSurfColorIndex, 3, GL_FLOAT, GL_FALSE, 0, 0);
74+
glEnableVertexAttribArray(mSurfAlphaIndex);
75+
glBindBuffer(GL_ARRAY_BUFFER, mABO);
76+
glVertexAttribPointer(mSurfAlphaIndex, 1, GL_FLOAT, GL_FALSE, 0, 0);
77+
//attach indices
78+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIBO);
79+
glBindVertexArray(0);
80+
/* store the vertex array object corresponding to
81+
* the window instance in the map */
82+
mVAOMap[pWindowId] = vao;
83+
}
7784

78-
glBindVertexArray(mVAOMap[pWindowId]);
85+
glBindVertexArray(mVAOMap[pWindowId]);
7986
}
8087

8188
void surface_impl::unbindResources() const
8289
{
83-
glBindVertexArray(0);
90+
glBindVertexArray(0);
8491
}
8592

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

122129
bindResources(pWindowId);
123-
glDrawElements(GL_TRIANGLE_STRIP, mIBOSize, GL_UNSIGNED_SHORT, (void*)0 );
130+
glDrawElements(GL_TRIANGLE_STRIP, mIBOSize, GL_UNSIGNED_INT, (void*)0);
124131
unbindResources();
125132
mSurfProgram.unbind();
126133

127-
if(mMarkerType != FG_MARKER_NONE) {
134+
if (mMarkerType != FG_MARKER_NONE) {
128135
glEnable(GL_PROGRAM_POINT_SIZE);
129136
mMarkerProgram.bind();
130137

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

137144
bindResources(pWindowId);
138-
glDrawElements(GL_POINTS, mIBOSize, GL_UNSIGNED_SHORT, (void*)0);
145+
glDrawElements(GL_POINTS, mIBOSize, GL_UNSIGNED_INT, (void*)0);
139146
unbindResources();
140147

141148
mMarkerProgram.unbind();
@@ -201,10 +208,13 @@ surface_impl::surface_impl(unsigned pNumXPoints, unsigned pNumYPoints,
201208

202209
#undef SURF_CREATE_BUFFERS
203210

204-
mIBOSize = (2 * mNumYPoints) * (mNumXPoints - 1);
205-
std::vector<ushort> indices(mIBOSize);
206-
generateGridIndices(mNumXPoints, mNumYPoints, indices.data());
207-
mIBO = createBuffer<ushort>(GL_ELEMENT_ARRAY_BUFFER, mIBOSize, indices.data(), GL_STATIC_DRAW);
211+
std::vector<unsigned int> indices;
212+
213+
generateGridIndices(indices, mNumXPoints, mNumYPoints);
214+
215+
mIBOSize = indices.size();
216+
217+
mIBO = createBuffer<uint>(GL_ELEMENT_ARRAY_BUFFER, mIBOSize, indices.data(), GL_STATIC_DRAW);
208218

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

258268
bindResources(pWindowId);
259-
glDrawElements(GL_POINTS, mIBOSize, GL_UNSIGNED_SHORT, (void*)0);
269+
glDrawElements(GL_POINTS, mIBOSize, GL_UNSIGNED_INT, (void*)0);
260270
unbindResources();
261271

262272
mMarkerProgram.unbind();

0 commit comments

Comments
 (0)