I want to be able to display points on the position that I clicked on. However, I get the error saying “segmentation fault(core dumped)” when I click on the window…
Here’s how I setup my buffer and array
//Setup vertex data, buffers and attribute pointers
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO
This is the callback function:
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
//Prints the (x,y) coordinate of the clicked position
double posX, posY;
glfwGetCursorPos(window, &posX, &posY);
std::cout << "(x,y) coordinates are: "<< posX << " " << posY << std::endl;
//Generate 3D vector point
int width, height;
glfwGetWindowSize(window, &width, &height);
float x = -1.0f + 2 * posX / width;
float y = +1.0f - 2 * posY / height;
glm::vec3 newPoint = glm::vec3(x, y, 1.0f);
std::cout << "X: " << x << std::endl;
std::cout << "Y: " << y << std::endl;
//Store vector point into vector array
vertices.push_back(newPoint);
int verticeSize = vertices.size();
std::cout << verticeSize << std::endl;
}
}
And here’s my while loop that draws the point:
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//Draw points
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glPointSize(10);
glDrawArrays(GL_POINTS, 0, vertices.size());
glBindVertexArray(0);
// Swap the screen buffers
glfwSwapBuffers(window);
}
If anyone can point me out to the right direction…It’d be great I spent like 8 hours on it and can’t seem to find what’s wrong… Although if I remove the VAO, the program works…it only draws one point at the origin…