1

sorry for the bad phrasing of the question, i don't know what i'm talking about.. i'm making a project for my graphics class and i'm kinda stuck with an issue.

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

void display();
void init();
void reshape(int, int);
void mouseFunc(int, int, int, int);

    const int XMAX = 600;
    const int YMAX = 600;
    const int GRID_SIZE = 20;

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(XMAX, YMAX);
    glutCreateWindow("test pgm");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouseFunc);
    init();
    glutMainLoop();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glPointSize(14);            //POINT SIZE

#pragma region draw_here
    glBegin(GL_POINTS);

    float grid_x = GRID_SIZE - 1;
    float grid_y = GRID_SIZE - 1;
    for (float x = -grid_x; x < GRID_SIZE; x++)
    {
        for (float y = grid_y; y > -GRID_SIZE; y--)
        {
            glVertex2f(x, y);
        }
    }

glEnd();
#pragma endregion
    glFlush();
}

void init()
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-GRID_SIZE, GRID_SIZE, -GRID_SIZE, GRID_SIZE);
    glMatrixMode(GL_MODELVIEW);
}
void mouseFunc(int button, int state, int mouse_x, int mouse_y)
{
    int x = -GRID_SIZE - (XMAX / (mouse_x + 1) * 20);
    int y =  GRID_SIZE - (YMAX / (mouse_y + 1) * 20);
    if(state == 0)
        std::cout << "button:" << button << "\nstate:" << state << "\n(x,y):(" << x << "," << y << ")\n------\n";
}

Output Window Console Window

my desired output would be the coordinates i used to draw the pixels in the loops in the draw_here region for eg: clicking the yellow box on top left of output window should give the coordinates (-19, 19). i'm trying to demonstrate a path finding algorithm and i want to draw the obstacles and specify starting and ending locations using mouse clicks. i would eventually want to store the coordinates and relevant data in a array object and iterate through them as needed, i'm only in the prototyping phase right now.

1 Answers1

2

Compute the normalized device space coordinate of the mouse in range [-1, 1]:

double ndc_x = (double)mouse_x / XMAX * 2.0 - 1.0;
double ndc_y = 1.0 - (double)mouse_y / YMAX * 2.0;

Map the result to the range to the grid range:

int x = (int)(ndc_x * GRID_SIZE + GRID_SIZE + 0.5) - GRID_SIZE;
int y = (int)(ndc_y * GRID_SIZE + GRID_SIZE + 0.5) - GRID_SIZE;

(-19, -19) is at the bottom left and (19, 19) is at the top right.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    that worked well, thanks a lot. can you suggest some resource which i can refer to understand this concept better. – Mathew Reji Sep 24 '20 at 05:41
  • @MathewReji There are a lot of resources e.g. [LearnOpenGL - camera](https://learnopengl.com/Getting-started/Camera) and [OpenGL - Mouse coordinates to Space coordinates](https://stackoverflow.com/questions/46749675/opengl-mouse-coordinates-to-space-coordinates/46752492#46752492) – Rabbid76 Sep 24 '20 at 05:53