The basic premise of my application (as of now) is just to render the points clicked by the user. This is the code I have (in snippets - I’ll try to add my “train of thought” if that can help):
First the User clicks wherever he wants the points to be displayed (after the initialization of the window):
void mouseButtonCallback(GLFWwindow* _window, int button, int action, int mods)
{
switch (button)
{
case GLFW_MOUSE_BUTTON_LEFT:
if (action == GLFW_RELEASE)
{
if (transitionalFlag == false) {
glfwGetCursorPos(window, &x, &y);
setPositionVector(x, y);
flag = true;
}
else { //please ignore the else for the moment seeing as I can't get the first one to work
glfwGetCursorPos(window, &x, &y);
setPositionVector(x, y);
pointsClickedByUserTranslational.push_back(positionsClickedByUser);
}
}
}
}
So whenever I have to take my first line I go to transitional and take the x and y of my cursor into setPositionVector ( which is the following method):
void setPositionVector(double xpos, double ypos) {
if (transitionalFlag == false) {
positionsClickedByUser = windowToWorldCoords(glm::vec2(x, y));
vectorsToFloat(positionsClickedByUser);
updatePointsVBO();
}
else {
positionsClickedByUser = glm::vec3(xpos, ypos, 0);
vectorsToFloat(positionsClickedByUser);
updatePointsVBO();
}
}
This method takes in the x and y and first changes it up to my world coordinate ( which is the following method) :
glm::vec3 windowToWorldCoords(const glm::vec2 p)
{
// Get window dimensions
int w, h;
glfwGetWindowSize(window, &w, &h);
// Transform to camera coordinates; we assume the z-coordinate to be 0
const GLfloat cameraX = 2 * p.x / w - 1;
const GLfloat cameraY = -(2 * p.y / h - 1);
// Transform to world coordinates by inverting the transformation matrix
return glm::vec3(glm::inverse(transformationMatrix) * glm::vec4(cameraX, cameraY, 0.0f, 1.0f));
}
Which in returns a vec3 that I then change to float in this method:
void vectorsToFloat(glm::vec3 vector) {
vectorOfVertices.push_back(vector.x);
vectorOfVertices.push_back(vector.y);
vectorOfVertices.push_back(vector.z);
}
Afterwards, I have my updateVBO() method which is suppose to, well as it says, update my VBO everytime the user clicks on the screen
void updatePointsVBO()
{
glBindVertexArray(pointsVAO);
glBindBuffer(GL_ARRAY_BUFFER, pointsVBO);
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);
if (vectorOfVertices.size() > 0)
{
glBufferData(GL_ARRAY_BUFFER, sizeof(vectorOfVertices[0]) * vectorOfVertices.size(), &vectorOfVertices[0], GL_STATIC_DRAW);
}
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
So all this is done in that order ( user clicks --> get cursor pos --> set my position Vector --> and lastly update my VBO with the new info)
In my main loop I got this:
int main() {
initializeOpenGL();
shader_program = shadersInitialization("vertex.shader", "fragment.shader");
//linke your matrices to your shader
modelMatrixLocation = glGetUniformLocation(shader_program, "modelMatrix");
projectionMatrixLocation = glGetUniformLocation(shader_program, "projectionMatrix");
viewMatrixLocation = glGetUniformLocation(shader_program, "viewMatrix");
//Function allows to set camera to look at object
modelMatrix = glm::lookAt(cameraPos, cameraTarget, upVector);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader_program);
//projMatrix = glm::perspective(45.0f, (float)WIDTH/(float)HEIGHT, 0.1f, 1000.0f );
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
draw();
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &pointsVAO);
glDeleteBuffers(1, &pointsVBO);
glfwTerminate();
return 0;
}
My draw function:
void draw()
{
glGenBuffers(1, &pointsVBO);
glGenVertexArrays(1, &pointsVAO);
glBindVertexArray(pointsVAO);
glDrawArrays(GL_POINTS, 0, vectorOfVertices.size());
}
I’m not getting any error, just a blank screen. Also, I’m pretty sure my code isn’t optimal the way it’s coded right now, though I’d just like some pointers on how to make those points appear. Any would help.
Thanks!