Results 1 to 3 of 3

Thread: Points clicked by user not rendering

  1. #1
    Newbie Newbie
    Join Date
    Feb 2017
    Posts
    4

    Points clicked by user not rendering

    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):

    Code :
     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):

    Code :
    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) :



    Code :
    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:

    Code :
      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


    Code :
    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:

    Code :
        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:

    Code :
     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!

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,654
    Quote Originally Posted by PepeBeef View Post
    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. 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.

  3. #3
    Newbie Newbie
    Join Date
    Feb 2017
    Posts
    4
    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.

Similar Threads

  1. Rendering Points - which method
    By thinks in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 12-11-2006, 08:06 AM
  2. glDrawPixels + get pixel user clicked on
    By mark-w in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 05-10-2005, 08:39 AM
  3. glDrawPixels + get pixel user clicked on
    By mark-w in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 05-08-2005, 08:05 PM
  4. Rendering to a buffer not visible to the user
    By pmatos in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-24-2003, 01:31 PM
  5. Rendering a picture that is composed from points
    By renana_m in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-18-2003, 03:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean