Jump to content
  • Advertisement

Soeholm

Member
  • Content Count

    5
  • Joined

  • Last visited

Community Reputation

118 Neutral

About Soeholm

  • Rank
    Newbie
  1. Thanks again, that actually makes a lot more sense when you think about it :-)
  2. Thank you so much!   Changing the number to 3 (pos + normal + color) gets rid of the Segfault, and the cube is now rendered :-)   I've removed some magic numbers, as suggested.   Good point about the buffers. I've added a reference count to the Mesh class, which is incremented and decremented by the MeshRenderer class. When the ref count becomes 0, the Mesh deletes the ibo and vbo.
  3. Thanks, I've fixed that now :-)   Unfortunately my original problem persists.
  4. Thanks for the fast reply!   There's six indices, it's just the same triangle being repeated (0,1,2), which should still work right? I tried changing it to: 0, 1, 2, 2, 3, 4 which still doesn't work.   I have to agree with you on the magic numbers part. Things are being done very manually here though, because it's a utility method that creates a primitive. The idea being that all the magic numbers are hidden inside a method call. I don't think it would makes sense to introduce a constant called NUM_VERTICES_IN_A_TRIANGLE or something like that.
  5. Hello there!   I am currently making my own little game engine with OpenGL, but I have been stuck with this problem for a while now. I can render a triangle with no problems, but I get a segfault from glDrawElements with a more complex shape. I'll try and post the relevant code snippets.   Here is how I send mesh data to the buffer: void Mesh::sendToBuffer() { GLuint vbo = 0; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, verticesBufferSize(), vertices, GL_STATIC_DRAW); GLuint ibo; glGenBuffers(1, &ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBufferSize(), indices, GL_STATIC_DRAW); vao = 0; glGenVertexArrays(1, &vao); glBindVertexArray(vao); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); // Position glVertexAttribPointer(0, numVertices, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr); // Normals glVertexAttribPointer(1, numVertices, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void *)3); } Here is the code for creating a triangle, this works flawlessly: Mesh Primitives::createTriangle() { Mesh tri; tri.numVertices = 3; Vertex vertices[] = { glm::vec3(0.0f, 0.5f, 0.0f), // Position glm::vec3(0.0f, 0.0f, 1.0f), // Normal glm::vec3(1.0f, 1.0f, 1.0f), // Color glm::vec3(0.5f, -0.5f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(-0.5f, -0.5f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(1.0f, 1.0f, 1.0f) }; tri.vertices = new Vertex[tri.numVertices]; memcpy(tri.vertices, vertices, sizeof(vertices)); tri.numIndices = 3; GLushort indices[] = { 0, 1, 2 }; tri.indices = new GLushort[tri.numIndices]; memcpy(tri.indices, indices, sizeof(indices)); tri.sendToBuffer(); return tri; } However rendering a cube, or part of a cube as shown here, fails: Mesh Primitives::createCube() { Mesh cube; cube.numVertices = 6; Vertex vertices[] = { glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(-2.5f, -0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(-2.5f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(-1.0f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(5.0f, 5.0f, 5.0f), }; cube.vertices = new Vertex[cube.numVertices]; memcpy(cube.vertices, vertices, sizeof(vertices)); cube.numIndices = 6; GLushort indices[] = { 0, 1, 2, 0, 1, 2 // Front //4, 5, 6, 6, 7, 4, // Back //8, 9, 10, 10, 11, 8, // Bottom //12, 13, 14, 14, 15, 12, // Top //16, 17, 18, 18, 19, 16, // Left //20, 21, 22, 22, 23, 20 // Right }; cube.indices = new GLushort[cube.numIndices]; memcpy(cube.indices, indices, sizeof(indices)); cube.sendToBuffer(); return cube; } And to draw the mesh I simply call: mesh.bind(); glDrawElements(GL_TRIANGLES, mesh.getNumIndices(), GL_UNSIGNED_SHORT, 0); I think this is the relevant code, the full source is on GitHub: https://github.com/mathiassoeholm/OpenGLPlayground/tree/master/GameEngine   I'm sorry for posting so much code here, but I'm at a loss for what causes the Segmentation fault to happen.   Any help is much appreciated.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!