Need help with C++ OpenGL

Aug 26, 2016 at 11:00pm
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 Aug 26, 2016 at 11:00pm
Aug 26, 2016 at 11:05pm
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.
Aug 26, 2016 at 11:06pm
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 Aug 26, 2016 at 11:09pm
Aug 26, 2016 at 11:34pm
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.
Aug 26, 2016 at 11:43pm
Yes but i dont get valuse like 1.0,1.1,1.2,2.0,i get example 259
Last edited on Aug 26, 2016 at 11:43pm
Aug 26, 2016 at 11:55pm
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.
Aug 26, 2016 at 11:59pm
No,no i try to make mouse cordinates in game cordinates x,y,z
Aug 27, 2016 at 12:00am
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 Aug 27, 2016 at 12:01am
Aug 27, 2016 at 12:02am
You'll have to make a way to translate the coordinates, because you will only be given the (x, y) coordinates in screen coordinates.
Aug 27, 2016 at 12:18am
Sorry,but how to translate ? :D
Aug 27, 2016 at 2:15am
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.
Aug 27, 2016 at 4:17pm
I dont know SFML,maybe on c++ ?
Aug 27, 2016 at 4:19pm
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.
Aug 27, 2016 at 4:41pm
Ok i will try
Aug 27, 2016 at 4:48pm
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.
Aug 27, 2016 at 5:00pm
I have question, i need learn SMFL first or if i know c++ i dont need learn ?
Aug 27, 2016 at 5:14pm
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.
Aug 27, 2016 at 5:20pm
Ok ty
Aug 28, 2016 at 4:08pm
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 Aug 28, 2016 at 4:10pm
Aug 28, 2016 at 10:14pm
Where can i find algorythm ?
Last edited on Aug 28, 2016 at 10:14pm
Topic archived. No new replies allowed.