Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: Think doing VBOs right, but cannot see output

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2018
    Posts
    10

    Think doing VBOs right, but cannot see output

    Think doing VBOs right, but cannot see output.

    My code is:

    Code :
            std::vector<GLfloat> vertices;
    	std::vector<GLfloat> normals;
     
    	for (int j = 0; j < vecf.size(); j++) {
    		normals.push_back(vecn[vecf.at(j)[2] - 1][0]); normals.push_back(vecn[vecf.at(j)[2] - 1][1]); normals.push_back(vecn[vecf.at(j)[2] - 1][2]);
    		vertices.push_back(vecv[vecf.at(j)[0] - 1][0]); vertices.push_back(vecv[vecf.at(j)[0] - 1][1]); vertices.push_back(vecv[vecf.at(j)[0] - 1][2]);
    		normals.push_back(vecn[vecf.at(j)[5] - 1][0]); normals.push_back(vecn[vecf.at(j)[5] - 1][1]); normals.push_back(vecn[vecf.at(j)[5] - 1][2]);
    		vertices.push_back(vecv[vecf.at(j)[3] - 1][0]); vertices.push_back(vecv[vecf.at(j)[3] - 1][1]); vertices.push_back(vecv[vecf.at(j)[3] - 1][2]);
    		normals.push_back(vecn[vecf.at(j)[8] - 1][0]); normals.push_back(vecn[vecf.at(j)[8] - 1][1]); normals.push_back(vecn[vecf.at(j)[8] - 1][2]);
    		vertices.push_back(vecv[vecf.at(j)[6] - 1][0]); vertices.push_back(vecv[vecf.at(j)[6] - 1][1]); vertices.push_back(vecv[vecf.at(j)[6] - 1][2]);
    	}
     
    	glEnableClientState(0);
    	glEnableClientState(1);
     
    	glVertexPointer(3, GL_FLOAT, 0, &vertices[0]);
    	glNormalPointer(GL_FLOAT, 0, &normals[0]);
     
    	/*glDrawArrays(GL_TRIANGLES, 0, vertices.size());*/
    	glDrawElements(GL_TRIANGLES, vertices.size(), GL_FLOAT, &vertices[0]);
     
    	glDisableClientState(0);
    	glDisableClientState(1);

    The above code runs without errors, but I see nothing drawn, just black screen.

    I've followed these:

    https://www.opengl.org/discussion_bo...lVertexPointer
    https://github.com/sriharshachilakap...Tutorial4.java

    My existing and working code with glBegin(GL_TRIANGLES); glEnd(); is:

    Code :
            glBegin(GL_TRIANGLES);
     
    	for(int j = 0; j < vecf.size(); j++){
    		glNormal3f(vecn[vecf.at(j)[2]-1][0], vecn[vecf.at(j)[2]-1][1], vecn[vecf.at(j)[2]-1][2]);
    		glVertex3f(vecv[vecf.at(j)[0]-1][0], vecv[vecf.at(j)[0]-1][1], vecv[vecf.at(j)[0]-1][2]);
    		glNormal3f(vecn[vecf.at(j)[5]-1][0], vecn[vecf.at(j)[5]-1][1], vecn[vecf.at(j)[5]-1][2]);
    		glVertex3f(vecv[vecf.at(j)[3]-1][0], vecv[vecf.at(j)[3]-1][1], vecv[vecf.at(j)[3]-1][2]);
    		glNormal3f(vecn[vecf.at(j)[8]-1][0], vecn[vecf.at(j)[8]-1][1], vecn[vecf.at(j)[8]-1][2]);
    		glVertex3f(vecv[vecf.at(j)[6]-1][0], vecv[vecf.at(j)[6]-1][1], vecv[vecf.at(j)[6]-1][2]);
    	}
     
    	glEnd();
    Last edited by mavavilj; 07-29-2018 at 03:14 AM.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,887
    Quote Originally Posted by mavavilj View Post
    Think doing VBOs right, but cannot see output.
    Note that your code doesn't use VBOs (vertex buffer objects), but uses client-side vertex arrays. If you're trying to convert glBegin/glEnd code to use VBOs, client-side vertex arrays are a reasonable intermediate step.

    Quote Originally Posted by mavavilj View Post
    Code :
    	/*glDrawArrays(GL_TRIANGLES, 0, vertices.size());*/
    This should almost work (assuming that vertices and normals contain valid data). However, the last parameter should be divided by 3 (it's the number of vertices, but vertices.size() is the number of individual floats).

    Quote Originally Posted by mavavilj View Post
    Code :
    	glDrawElements(GL_TRIANGLES, vertices.size(), GL_FLOAT, &vertices[0]);
    This definitely won't work, as the last parameter needs to be an array of indices.

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2018
    Posts
    10
    I'm using the code that I have BTW, because I noticed that I was bound to freeGLUT 2.0.1 and OpenGL 3.3. I couldn't find newer functions than these.

    I wonder if the versions I have even support VBOs?

    I will check those mentions.

  4. #4
    Junior Member Newbie
    Join Date
    Jul 2018
    Posts
    10
    Okay I tried the:

    Code :
    glDrawArrays(GL_TRIANGLES, 0, vertices.size()/3);

    added to the above. Still no output.

    The normals and vertices I believe should be correct, because my glBegin(GL_TRIANGLES); glEnd(); block looked like:

    Code :
            glBegin(GL_TRIANGLES);
     
    	for(int j = 0; j < vecf.size(); j++){
    		glNormal3f(vecn[vecf.at(j)[2]-1][0], vecn[vecf.at(j)[2]-1][1], vecn[vecf.at(j)[2]-1][2]);
    		glVertex3f(vecv[vecf.at(j)[0]-1][0], vecv[vecf.at(j)[0]-1][1], vecv[vecf.at(j)[0]-1][2]);
    		glNormal3f(vecn[vecf.at(j)[5]-1][0], vecn[vecf.at(j)[5]-1][1], vecn[vecf.at(j)[5]-1][2]);
    		glVertex3f(vecv[vecf.at(j)[3]-1][0], vecv[vecf.at(j)[3]-1][1], vecv[vecf.at(j)[3]-1][2]);
    		glNormal3f(vecn[vecf.at(j)[8]-1][0], vecn[vecf.at(j)[8]-1][1], vecn[vecf.at(j)[8]-1][2]);
    		glVertex3f(vecv[vecf.at(j)[6]-1][0], vecv[vecf.at(j)[6]-1][1], vecv[vecf.at(j)[6]-1][2]);
    	}
     
    	glEnd();

    And my new code essentially constructs std::vector<GLfloat> using the same loop and same elements.

    Even if there was errors in the construction, I'd expect to see at least something.
    Last edited by mavavilj; 07-29-2018 at 03:16 AM.

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2018
    Posts
    10
    Okay I found the issue.

    I had to replace

    Code :
    glEnableClientState(0);
    glEnableClientState(1);
    ...
    glDisableClientState(0);
    glDisableClientState(1);

    with

    Code :
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    ...
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);

    Does this mean that one cannot have multiple GL_VERTEX_ARRAYs identified using different identifiers?

  6. #6
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    6,021
    Does this mean that one cannot have multiple GL_VERTEX_ARRAYs identified using different identifiers?
    So long as you're using the old fixed-function vertex attributes, no, you cannot have multiple GL_VERTEX_ARRAYs at the same time.

    However, since you're using fixed-function rendering, that wouldn't make sense anyway. GL_VERTEX_ARRAY has a very well-defined meaning: the array containing the position of the vertices. Vertices cannot have more than one position in fixed-function rendering, so it doesn't make sense to allow multiple such arrays. Similarly, vertices cannot have more than one normal.

  7. #7
    Junior Member Newbie
    Join Date
    Jul 2018
    Posts
    10
    Quote Originally Posted by Alfonse Reinheart View Post
    So long as you're using the old fixed-function vertex attributes, no, you cannot have multiple GL_VERTEX_ARRAYs at the same time.

    However, since you're using fixed-function rendering, that wouldn't make sense anyway. GL_VERTEX_ARRAY has a very well-defined meaning: the array containing the position of the vertices. Vertices cannot have more than one position in fixed-function rendering, so it doesn't make sense to allow multiple such arrays. Similarly, vertices cannot have more than one normal.

    So is the idea that one puts all one's objects to a single array and then renders them from it using index ranges?

  8. #8
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,887
    Quote Originally Posted by mavavilj View Post
    So is the idea that one puts all one's objects to a single array and then renders them from it using index ranges?
    That's one option. Another is to switch between arrays by calling glVertexPointer() etc between draw calls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •