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 3 of 3

Thread: Vertex Buffer works for 16 vertices only

  1. #1
    Newbie Newbie
    Join Date
    Aug 2018
    Posts
    2

    Vertex Buffer works for 16 vertices only

    Hello everyone,
    after some hours of trying to fix it, i come to the conclusion, that i have no clue what causes the problem.
    The situation:
    Setup: OpenGL + C++
    GraphicsCard: NVIDIA GTX1060 6GB, Vendor: NVIDIA , version: 3.3.0 NVIDIA 398.82
    Libaries: GLFW3, glm, glad

    I have a main class that calls the draw function of my chunk class. The chunk class currently has 20 vertices defined, and a set of 4 form a plane. To draw the planes i use an index buffer with DrawElements.
    And everything works fine for the first 16 vertices, but when i add another 4 vertices to the vertex buffer, the triangles of the corresponding 4 vertices do not get drawn.
    If i switch the order in which i push the vertices into the vertex buffer, then the triangles do get rendered, but again the last 4 pushed do not.
    (and yes i do know that i could reduce the amount of vertices, because of the index buffer, but for practise reason i wanan use them in that way atm. but the vertex and index buffer should be able to handle way more vertices)
    But the indexbuffer is working fine, meaning, i can draw more triangles between the first 16 vertices.
    The code of the chunk class:
    Code :
    #include "chunk.h"
     
    chunk::chunk(float Seed, float Size, std::vector<float> Heightmap, float XPosition, float ZPosition, float HighestHeightValue, float LowestHeightValue){
    	seed				= Seed;
    	size				= Size;
    	heightmap			= Heightmap;
    	xPosition			= XPosition;
    	zPosition			= ZPosition;
    	highestHeightValue	= HighestHeightValue;
    	lowestHeightValue	= LowestHeightValue;
    	//generateVertices();
    	vertices.push_back({ 0.0, 1.0, 0.0, 0.0 });
    	vertices.push_back({ 0.0, 0.0, 0.0, 0.0 });
    	vertices.push_back({ 1.0, 1.0, 0.0, 0.0 });
    	vertices.push_back({ 1.0, 0.0, 0.0, 0.0 });
     
    	vertices.push_back({ 0.0, 1.0, -1.0, 0.0 });
    	vertices.push_back({ 0.0, 0.0, -1.0, 0.0 });
    	vertices.push_back({ 1.0, 1.0, -1.0, 0.0 });
    	vertices.push_back({ 1.0, 0.0, -1.0, 0.0 });
     
    	vertices.push_back({ 1.0, 1.0, 0.0, 0.0 });
    	vertices.push_back({ 1.0, 1.0, -1.0, 0.0 });
    	vertices.push_back({ 1.0, 0.0,  0.0, 0.0 });
    	vertices.push_back({ 1.0, 0.0, -1.0, 0.0 });
     
    	vertices.push_back({ 0.0, 1.0, 0.0, 0.0 });
    	vertices.push_back({ 0.0, 1.0, -1.0, 0.0 });
    	vertices.push_back({ 0.0, 0.0,  0.0, 0.0 });
    	vertices.push_back({ 0.0, 0.0, -1.0, 0.0 });
     
    	vertices.push_back({ 0.0, 1.0, 1.0, 0.0 });
    	vertices.push_back({ 0.0, 0.0, 1.0, 0.0 });
    	vertices.push_back({ 1.0, 1.0, 1.0, 0.0 });
    	vertices.push_back({ 1.0, 0.0, 1.0, 0.0 });
     
    	indices.push_back(0);
    	indices.push_back(2);
    	indices.push_back(1);
    	indices.push_back(1);
    	indices.push_back(2);
    	indices.push_back(3);
     
    	indices.push_back(4);
    	indices.push_back(5);
    	indices.push_back(6);
    	indices.push_back(6);
    	indices.push_back(5);
    	indices.push_back(7);
     
    	indices.push_back(8);
    	indices.push_back(9);
    	indices.push_back(10);
    	indices.push_back(10);
    	indices.push_back(9);
    	indices.push_back(11);
     
     
     
    	indices.push_back(12);
    	indices.push_back(14);
    	indices.push_back(15);
    	indices.push_back(15);
    	indices.push_back(13);
    	indices.push_back(12);
     
     
    	indices.push_back(16); //these do not get rendered
    	indices.push_back(18);
    	indices.push_back(17);
    	indices.push_back(17);
    	indices.push_back(18);
    	indices.push_back(19);
     
    	std::cout << vertices.size();
    	setUpChunk();
     
    }
     
    chunk::~chunk()
    {
    }
     
    void chunk::setUpChunk() {
    	glGenVertexArrays(1, &VAO);
    	glGenBuffers(1, &VBO);
    	glGenBuffers(1, &EBO);
     
    	glBindVertexArray(VAO);
    	glBindBuffer(GL_ARRAY_BUFFER, VBO);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) * sizeof(vertex), &vertices[0], GL_STATIC_DRAW);
     
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
     
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) 0);
    	glEnableVertexAttribArray(0);
     
     
    	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) (3 * sizeof(float)));
    	glEnableVertexAttribArray(1);
     
    	glBindVertexArray(0);
    }
     
    void chunk::draw(Shader shader) {
    	glBindVertexArray(VAO);
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    	glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
     
    }

    Here is a picture of it:

    Click image for larger version. 

Name:	openGlBug.jpg 
Views:	22 
Size:	11.8 KB 
ID:	2829


    Hope someone can help me =)
    Greetings Luca
    Last edited by GilgusMaximus; 08-28-2018 at 07:54 AM.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,926
    Quote Originally Posted by GilgusMaximus View Post
    Code :
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) * sizeof(vertex), &vertices[0], GL_STATIC_DRAW);
    sizeof(vertices) is a compile-time constant, equivalent to sizeof(std::vector<T>). You should probably be using vertices.size() instead.

  3. #3
    Newbie Newbie
    Join Date
    Aug 2018
    Posts
    2
    Ahh okay, i somehow missed that info back when i learned C++ !
    Now it works fine, thank you very much ! =)

    Greeting Luca

Posting Permissions

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