Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Drawing points where you click?

  1. #1
    Newbie Newbie
    Join Date
    May 2016
    Posts
    3

    Drawing points where you click?

    removed for personal reasons
    Last edited by Mettalknight; 05-24-2016 at 01:18 PM.

  2. #2
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    431
    you dont need precompiled headders in your project for opengl
    drawing points is relatively easy done without shaders:

    Code :
    	glClear(GL_COLOR_BUFFER_BIT);
     
    	glBegin(GL_POINTS);
     
    	for (unsigned int i = 0; i < m_points.size(); i++)
    		glVertex2f(m_points[i].x, m_points[i].y);
     
    	glEnd();

    therefore you need to store the points in an <vector>

    Code :
    void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
    {
    	if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
    	{
    		// cursor position
    		double xpos, ypos;
    		glfwGetCursorPos(window, &xpos, &ypos);
    		// display size
    		int width, height;
    		glfwGetWindowSize(window, &width, &height);
    		float x = -1.0f + 2 * xpos / width;
    		float y = +1.0f - 2 * ypos / height;
    		m_points.push_back(glm::vec2(x, y));   // ???
    	}
    }

    obviously you cant update the points array in the callback function so you have to use a global variable or a singelton class
    Last edited by john_connor; 05-23-2016 at 03:13 PM.

  3. #3
    Newbie Newbie
    Join Date
    May 2016
    Posts
    3
    Quote Originally Posted by john_connor View Post
    you dont need precompiled headders in your project for opengl
    drawing points is relatively easy done without shaders:

    Code :
    	glClear(GL_COLOR_BUFFER_BIT);
     
    	glBegin(GL_POINTS);
     
    	for (unsigned int i = 0; i < m_points.size(); i++)
    		glVertex2f(m_points[i].x, m_points[i].y);
     
    	glEnd();

    therefore you need to store the points in an <vector>

    Code :
    void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
    {
    	if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
    	{
    		// cursor position
    		double xpos, ypos;
    		glfwGetCursorPos(window, &xpos, &ypos);
    		// display size
    		int width, height;
    		glfwGetWindowSize(window, &width, &height);
    		float x = -1.0f + 2 * xpos / width;
    		float y = +1.0f - 2 * ypos / height;
    		m_points.push_back(glm::vec2(x, y));   // ???
    	}
    }

    obviously you cant update the points array in the callback function so you have to use a global variable or a singelton class
    Thanks for the reply but I would need a shader for what I have in mind later. If I keep the glDrawArrays then what happens is all points on screen are displayed at the top left corner of my window.

  4. #4
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    431
    Quote Originally Posted by Mettalknight View Post
    Thanks for the reply but I would need a shader for what I have in mind later. If I keep the glDrawArrays then what happens is all points on screen are displayed at the top left corner of my window.
    GLfloat point_vertex[] = {
    -1, 1
    };

    is the upper left corner

    glDrawArrays(GL_POINTS, 0, 1);
    will draw ONLY 1 point, regardless of the size of vertexarray

    furthermore, i dont see where you update the vertex buffer object after you've clicked anywhere ...
    Last edited by john_connor; 05-23-2016 at 03:53 PM.

  5. #5
    Newbie Newbie
    Join Date
    May 2016
    Posts
    3
    Quote Originally Posted by john_connor View Post
    GLfloat point_vertex[] = {
    -1, 1
    };

    is the upper left corner
    Yes but wouldn't translating them move the points? Also when I change those values they still appear on the top right corner. I feel like i'm missing something..

  6. #6
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    431
    glDrawArrays(GL_POINTS, 0, user_clicks.size());

    AND ...

    declare GLuint VAO, VBO globally, you need them later in the callback !!

    AND ...

    static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
    {
    if (button == GLFW_MOUSE_BUTTON_LEFT) {
    if (GLFW_PRESS == action)
    {
    double xpos, ypos;
    glfwGetCursorPos(window, &xpos, &ypos);
    user_clicks.push_back(glm::vec3(xpos, ypos, 0.0f));

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(point_vertex) * user_clicks.size(), user_clicks.data(), GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    }
    }


    i would also recommend you put ALL opengl code into a separate class (e.g. "Graphics()")
    and only call graphics.render() in the main loop

    Code :
     
     
     
     
    #include <gl/glew.h>
    #include <GLFW/glfw3.h>
     
    #include "Graphics.h"
     
     
    void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
     
     
    int main(void)
    {
    	/* Initialize the library */
    	if (!glfwInit())
    		return -1;
     
    	/* Create a windowed mode window and its OpenGL context */
    	GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    	if (!window)
    	{
    		glfwTerminate();
    		return -1;
    	}
     
    	/* Make the window's context current */
    	glfwMakeContextCurrent(window);
    	glfwSetMouseButtonCallback(window, mouse_button_callback);
     
    	/* Init GLEW*/
    	if (glewInit() != GLEW_OK)
    	{
    		glfwTerminate();
    		return -2;
    	}
     
    	Graphics& graphics = Graphics::Instance();
     
    	/* Loop until the user closes the window */
    	while (!glfwWindowShouldClose(window))
    	{
    		/* Render here */
    		graphics.Render();
     
    		/* Swap front and back buffers */
    		glfwSwapBuffers(window);
     
    		/* Poll for and process events */
    		glfwPollEvents();
    	}
     
    	glfwTerminate();
    	return 0;
    }
     
     
     
    void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
    {
    	if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
    	{
    		// cursor position
    		double xpos, ypos;
    		glfwGetCursorPos(window, &xpos, &ypos);
    		Graphics::Instance().AddPoint(glm::vec2(xpos, ypos));
    	}
    }

    Code :
    #pragma once
     
    #include <GL/glew.h>
    #include <glm/glm.hpp>
    #include <vector>
     
     
    class Graphics
    {
    public:
     
    	static Graphics& Instance()
    	{
    		static Graphics instance;
    		return instance;
    	}
    	~Graphics();
     
    	void AddPoint(const glm::vec2& point);
    	void Render();
     
    private:
     
    	Graphics(); // private constructor
     
    	std::vector<glm::vec2> m_points;
     
    	unsigned int m_vao, m_vbo;
    	unsigned int m_program;
     
     
    };
    Last edited by john_connor; 05-23-2016 at 04:17 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •