6

I would like to store the user's mouse click position on two variables

float x,y;

I'm using openGL with C. I already have a mouse function using glut, but when I try to print x and y, it gives me values like x = 134; y = 150, while my screen ortho is between 0 and 1.

I want to get the exact points to draw a point there.

1
  • This is handled at a higher level, not by OpenGL. You need to check the documentation of the library that you're using to create your window / gl surface
    – hexist
    Nov 15, 2012 at 14:04

2 Answers 2

7

you need to register a mouse callback function it has the following signature:

void glutMouseFunc(void (*func)(int button, int state,
                                int x, int y));

There's a tutorial that covers some basics here

Edit: If you want the position to be normalized (0.0 - 1.0) divide by the width and height:

float x1 = x /(float) width;
float y1 = y /(float) height;
3
  • I already have a function but when I try to print x and y, it gives me values like x = 134 y = 150 while my screen ortho is between 0 and 1
    – Lily
    Nov 15, 2012 at 14:23
  • thanks but I'm getting unrealistic results for y, as far I know that in graphic the left bottom corner of the window is (0,0) so when I click next to it I get values like (0.23 , 0.97) which is realistic for x but not for y and when I go up in the window y values start to decrease
    – Lily
    Nov 17, 2012 at 6:58
  • @Lily no the top left corner is (0,0) Nov 17, 2012 at 7:01
-1

this is a simple program i wrote hope it helps everyone like it helped me.

#include <cstdlib>
#include <GL/glut.h>
#include <iostream>

#include "Vec2.h"

Vec2 pos(0.0, 0.0);
Vec2 go(1.0, 1.0);

float mouseX;
float mouseY;
float angle = 0.0f;

void changeSize(int w, int h) {

    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if (h == 0)
        h = 1;

    float ratio =  w * 1.0 / h;

    // Use the Projection Matrix
    glMatrixMode(GL_PROJECTION);

    // Reset Matrix
    glLoadIdentity();

    // Set the viewport to be the entire window
    glViewport(0, 0, w, h);

    // Set the correct perspective.
    gluPerspective(45.0f, ratio, 0.1f, 100.0f);

    // Get Back to the Modelview
    glMatrixMode(GL_MODELVIEW);
}

void renderScene(void) {

    // Clear Color and Depth Buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Reset transformations
    glLoadIdentity();
    // Set the camera
    gluLookAt(  0.0f, 0.0f, 10.0f,
                0.0f, 0.0f,  0.0f,
                0.0f, 1.0f,  0.0f);



    glPushMatrix();
    glRotatef(angle, 0.0f, 0.0f, 1.0f);
    glTranslatef(0.0, 0.0, 0.0);
    glBegin(GL_LINE_LOOP);
        glVertex3f( 0.0f, 2.0f, 0.0f);
        glVertex3f( -1.0f, -1.0f, 0.0f);
        glVertex3f( 1.0f, -1.0f, 0.0f);
    glEnd();
    glPopMatrix();

    //angle+=0.01f;

    glutSwapBuffers();
}

void mouseMove(int x, int y) 
{
    mouseX = -1.0 + 2.0 * x / 320 ;
    mouseY = 1.0 - 2.0 * y / 320 ;

    angle = 90 + atan2(pos.y-mouseY, pos.x-mouseX) * 180 / 3.1415926;

    //std::cout << mouseX << ", " << mouseY << std::endl;
    //std::cout << x << ", " << y << std::endl;
    std::cout << angle << std::endl;
}

int main(int argc, char **argv) {

    // init GLUT and create window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Lighthouse3D- GLUT Tutorial");

    // register callbacks
    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutIdleFunc(renderScene);

    glutMotionFunc(mouseMove);

    // enter GLUT event processing cycle
    glutMainLoop();

    return 1;
}
1
  • Please provide an explanation as to why this program will solve the OP's issue.
    – Luís Cruz
    Dec 13, 2014 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.