Want to learn how to use the mouse

I’ve been trying to learn how to use the mouse through google which is not working out so well right now.

Could someone link me to some tutorials that explain everything that I will need to know to use the mouse(storing mouse coordinates, picking, ect…)?

All of what I’m seeing on google is very confusing to me so I would prefer if the tutorial went into detail but I wont be picky.

dont use google, use the library you’re using (SDL, glfw, etc…)

example:
http://www.glfw.org/docs/latest/input.html#input_mouse

this is how you create a window:

after “glfwMakeContextCurrent(window);” you call glewInit(); to initialize opengl functions

after that, you register your callback functions (you have to provide)
scroll example: GLFW: Input guide


glfwSetScrollCallback(window, scroll_callback);

that means every time you scrolled the cursor, the function named “scroll_callback(…)” will be called
the function “scroll_callback(…)” must look like that:


void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
// here your response to cursor scrolling ...
// yoffset > 0 means you scrolled up, i think
}

you can directly query the cursor position by doing that:
http://www.glfw.org/docs/latest/input.html#cursor_pos

one thing you have to be aware of is that you need to include glew first:
http://glew.sourceforge.net/


#include <GL/glew.h>
#include <GLFW/glfw3.h>

here full code:




#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <iostream>

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);

void render();


int main(void)
{
	GLFWwindow* window;

	/* Initialize the library */
	if (!glfwInit())
		return -1;

	/* Create a windowed mode window and its OpenGL context */
	window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
	if (!window)
	{
		glfwTerminate();
		return -1;
	}

	/* Make the window's context current */
	glfwMakeContextCurrent(window);
	glfwSetKeyCallback(window, key_callback);
	glfwSetCursorPosCallback(window, cursor_position_callback);
	glfwSetScrollCallback(window, scroll_callback);


	/* Init GLEW* */
	if (glewInit() != GLEW_OK)
	{
		glfwTerminate();
		return -2;
	}

	/* Loop until the user closes the window */
	while (!glfwWindowShouldClose(window))
	{
		/* Render here */
		render();

		/* Swap front and back buffers */
		glfwSwapBuffers(window);

		/* Poll for and process events */
		glfwPollEvents();
	}

	glfwTerminate();
	return 0;
}



void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
	if (action == GLFW_PRESS)
	{
		if (key == GLFW_KEY_W)
			std::cout << "key W pressed: " << std::endl;
		if (key == GLFW_KEY_A)
			std::cout << "key A pressed: " << std::endl;
		if (key == GLFW_KEY_S)
			std::cout << "key S pressed: " << std::endl;
		if (key == GLFW_KEY_D)
			std::cout << "key D pressed: " << std::endl;
	}
}


void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
	std::cout << "cursor moved:   " << xpos << " | " << ypos << std::endl;
}


void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
	if (yoffset > 0)
		std::cout << "scrolled up" << std::endl;
	else
		std::cout << "scrolled down" << std::endl;
}


void render()
{
	glClearColor(0, 0, 1, 0);	// blue
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


	// ...
}


regarding picking:
that’s what you have to implement, 1 easy method it the so-called “color picking”:
–> you render your scene twice:
–> first you replace the actual color value by an unique ID (e.g. integer)
–> second you get the current cursor position, and query the integer from the framebuffer at that position
–> convert that integer back
–> render your scene as you would normally do (with colors)

tere is a way to merge both steps of rendering, called “MRT” (multi-render-target)
but to implement that you need to create your own framebuffer (double-layered) and a suitable shader (which renders at both layers of the framebuffer)