2323using namespace gl ;
2424using 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
4754namespace forge
4855{
@@ -51,36 +58,36 @@ namespace opengl
5158
5259void 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
8188void surface_impl::unbindResources () const
8289{
83- glBindVertexArray (0 );
90+ glBindVertexArray (0 );
8491}
8592
8693glm::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