Need help with C++ OpenGL

I need help to make a program that works so that when you press the mouse on the object to the object destroyed

sry im bad with english

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <windows.h>
#include <stdlib.h>
#include <GL/glut.h>

bool destroy = false;

int x = 0;
int y = 0;

void reshape (int w, int h)
{
    if(h == 0)
    h = 1;

    float ratio = 1.0 * w/h;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glViewport(0,0,w,h);

    gluPerspective(45,ratio,1,1000);
    gluLookAt(0.0,0.0,20.0,
              0.0,0.0,0.0,
              0.0f,1.0f,0.0f);
    glMatrixMode(GL_MODELVIEW);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    if(destroy == false)
    {
        glPushMatrix();
        glTranslated(x,y,0);
        glBegin(GL_QUADS);
        glVertex2f(0.0,0.0);
        glVertex2f(2.0,0.0);
        glVertex2f(2.0,2.0);
        glVertex2f(0.0,2.0);
        glEnd();
        glPopMatrix();
    }

    glutSwapBuffers();
}

void mouse(int b,int s,int x,int y)
{
    ////////// ?????????????????????????????????????????????
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("Mouse");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutReshapeFunc(reshape);

    glutMainLoop();
}
Last edited on
Get the position of the mouse and the position of the thing you want to test against. If the mouse position is inside the area enclosed by the object, make destroy true.
Yes i know,but how to get mouse position if i try this

1
2
3
4
5
void mouse(int b,int s,int x,int y)
{
    cout << x << endl;
    cout << y << endl;
}


i get big numbers
Last edited on
What values are you expecting to get? The coordinates you'll be getting will be in screen coordinates, relative to the top left corner of the window.
Yes but i dont get valuse like 1.0,1.1,1.2,2.0,i get example 259
Last edited on
You won't get values like that. The mouse can only be at integer locations (implied by the int type of the parameter). You can easily expect values 0-640 for x and 0-480 for y.
No,no i try to make mouse cordinates in game cordinates x,y,z
Something like Minecraft(if you know what is minecraft),when you move mouse you select other block
but i just need x,y becaus i making 2d game
Last edited on
You'll have to make a way to translate the coordinates, because you will only be given the (x, y) coordinates in screen coordinates.
Sorry,but how to translate ? :D
The guide/tutorial that you're following should provide a way to do that. How you translate depends partly on how you set up your game coordinates system.

If you really only need to do 2D, I recommend using SFML: http://www.sfml-dev.org/tutorials/2.4/

It is far easier to use than writing the raw OpenGL yourself. I use it extensively for my 2D graphics.
I dont know SFML,maybe on c++ ?
SFML is an OpenGL wrapper, written in C++. It is incredibly easy to use. The page I linked above is a link to a page full of tutorials on how to use it.
Ok i will try
For example, this is all the code needed to make a window and draw a green circle.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 (taken from  http://www.sfml-dev.org/tutorials/2.4/start-vc.php)
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

There is no need to translate coordinates, as everything is done in screen-space coordinates. There is no need to register callbacks to a window manager. SFML handles events for you and you can get them from SFML.
You can use 3D stuff with SFML, it was built to be compatible with that, and SFML has a guide on how to do that.
It also has a few other things available to help, like an audio module and classes for networking.
I have question, i need learn SMFL first or if i know c++ i dont need learn ?
SFML is a library. If you know C++, then you only need to learn how to use the library, similar to how you'd learn to use other libraries.
Ok ty
If you are going to try find cordiantes on opengl youl have to first get mouse cordiantes in opengl window and divide those by window paramaters for example variable from 0-1 then to make it telative to zoom stretch etc opengl parameters need correcting formulas in algorythm . To make those first it easyest to make some indicator in opengl so you can verify that formulas good . But much easyer just use some premade lib and learn comands.
Opengl gives lot of control but also lot more typing to do.
Last edited on
Where can i find algorythm ?
Last edited on
Topic archived. No new replies allowed.