Drawing points where you click?

removed for personal reasons

you dont need precompiled headders in your project for opengl
drawing points is relatively easy done without shaders:


	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


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

[QUOTE=john_connor;1282632]you dont need precompiled headders in your project for opengl
drawing points is relatively easy done without shaders:


	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


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[/QUOTE]

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

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 …

[QUOTE=john_connor;1282634]GLfloat point_vertex[] = {
-1, 1
};

is the upper left corner ;)[/QUOTE]

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…

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






#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));
	}
}


#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;


};