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 2 of 2

Thread: Want to learn how to use the mouse

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    433
    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:
    http://www.glfw.org/documentation.html

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

    after that, you register your callback functions (you have to provide)
    scroll example: http://www.glfw.org/docs/latest/input.html#scrolling
    Code :
    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:

    Code :
    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/
    Code :
    #include <GL/glew.h>
    #include <GLFW/glfw3.h>


    here full code:
    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)
    Last edited by john_connor; 05-28-2016 at 09:40 AM.

Tags for this Thread

Posting Permissions

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