PDA

View Full Version : Points clicked by user not rendering



PepeBeef
02-27-2017, 11:12 PM
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(positio nsClickedByUser);
}

}

}
}

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!

Dark Photon
02-28-2017, 05:45 AM
I'm not getting any error, just a blank screen. ... I'd just like some pointers on how to make those points appear. Any would help.

Please check out The Forum Posting Guidelines (https://www.opengl.org/discussion_boards/showthread.php/176139-Forum-Posting-Guidelines?p=1230021&viewfull=1#post1230021). This isn't a debugging service. You need to try and isolate the problem yourself, and then ask specific OpenGL related questions.

My recommendation. Start with a small test program that works. Gradually build that up, saving off intermediate versions. When it stops working, you'll know you just did something wrong. Dig into that, questioning everything you just did. Ask questions here about the specific change you made if if you can't figure out what the problem is.

PepeBeef
02-28-2017, 08:34 AM
This isn't as much as debugging as I don't know where to try things. I took out my Binding methods and instantiated them before my rendering loop so it does it once, it led to nothing.

I tried rendering a simple triangle to see if it could be my camera, triangles rendered fine.

I had an issue before for not rendering ( my previous small, opengl program) and the issue was that VAO was instantiated before my VBO. That wouldn't have been something I could've found on my own because it still worked , it just didn't render.

I'm really not asking for someone to complete / re-write my code. All I'm asking is for a pointer on where to look. It's the first time I dynamically take the points from a user. Having a static vector would make it much easier for me.

Now, if you guys think I'm doing this for debugging, you can ignore the question. However, I have tried google, I have tried playing around with my program. Even looking at other githubs that could contain some code that could point me in the right direction.

Thanks anyway.