Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Sour-Lemon

Pages: [1]
1
General / Correct mouse co-ordinates from SFML and using OpenGL
« on: October 26, 2010, 12:34:58 am »
My problem is related to getting the correct mouse co-ordinates from SFML while using OpenGL.

Basically I am making a polygon rotate on the Z axis to look at the current cursor position.

You can also move the polygon around the screen with the WASD keys.

If the polygon stays in the center of the screen everything works great, but my problem appear when I move the polygon to, for example, the top left of the screen.

Basically it is like it is getting the incorrect mouse co-ordinates and is overshooting the position of the actual mouse cursor.

I used GL_LINES to create a kind of crosshair to see where my variables thought the mouse cursor was, and it is overshooting the actual position.

To get the current mouse co-ordinates I am using this:

Code: [Select]
mouseX = Input.GetMouseX()-App.GetWidth()/2.f;
mouseY = Input.GetMouseY()-App.GetHeight()/2.f;


Does anybody know what my problem could be?
I have been looking at this almost all day and have lost my ability to think properly.

For the sake of providing all the information I can, below is my entire source code.
Also, if I provide my source then for those who are interested in helping, you can compile and see what I mean, since it is a bit hard for me to explain.

Sorry for it being so messy - I am new to this after all :)

Code: [Select]
#include <SFML/Window.hpp>
#include <iostream>
#include <cmath>

const float PI = 3.14159265f;

int main() {
    float angle = 0.f;

    sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");

    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.2f, 1.f, 500.f);

    GLfloat currentPosX = 0.f;
    GLfloat currentPosY = 0.f;

    float mouseX = 0.f;
    float mouseY = 0.f;

    const sf::Input &Input = App.GetInput();

    App.SetFramerateLimit(30);

    while (App.IsOpened()) {
        sf::Event Event;
        while (App.GetEvent(Event)) {
            if (Event.Type == sf::Event::Closed)
                App.Close();

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) {
                App.Close();
            }

            if (Event.Type == sf::Event::Resized) {
                glViewport(0, 0, Event.Size.Width, Event.Size.Height);
            }
        }

        App.SetActive();

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        if(Input.IsKeyDown(sf::Key::W)) {
            currentPosY += 3.f;
        }
        if(Input.IsKeyDown(sf::Key::S)) {
            currentPosY -= 3.f;
        }
        if(Input.IsKeyDown(sf::Key::D)) {
            currentPosX += 3.f;
        }
        if(Input.IsKeyDown(sf::Key::A)) {
            currentPosX -= 3.f;
        }

        mouseX = Input.GetMouseX()-App.GetWidth()/2.f;
        mouseY = Input.GetMouseY()-App.GetHeight()/2.f;

        // I don't know any better way to flip the Y axis
        if(mouseY >= 0) {
            mouseY = -(mouseY);
        }
        else {
            mouseY = abs(mouseY);
        }

        // Calculate the angle which the polygon needs to rotate at
        angle = atan2(mouseY - currentPosY, mouseX - currentPosX)*180/PI;

        // Print variables to console to try and figure out what I'm doing wrong
        std::cout << mouseX << "(" << currentPosX << ")" << ", " << mouseY << "(" << currentPosY << ")" << " - " << angle <<  std::endl;

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(currentPosX, currentPosY, -200.f);
        glRotatef(angle, 0.f, 0.f, 1.f);

        // Polygon
        glBegin(GL_QUADS);

            glVertex3f(-25.f, -25.f, -50.f);
            glVertex3f(25.f, -25.f, -50.f);
            glVertex3f(25.f, 25.f, -50.f);
            glVertex3f(-25.f, 25.f, -50.f);

        glEnd();

        glLoadIdentity();
        glTranslatef(currentPosX, currentPosY, -200.f);

        // Axis on polygon
        glBegin(GL_LINES);

            glVertex3f(-70.f, 0.f, -50.f);
            glVertex3f(70.f, 0.f, -50.f);

            glVertex3f(0.f, -70.f, -50.f);
            glVertex3f(0.f, 70.f, -50.f);

        glEnd();

        glLoadIdentity();
        glTranslatef(currentPosX, currentPosY, -200.f);
        glRotatef(angle, 0.f, 0.f, 1.f);

        // Line to indicate the direction of the polygon
        glBegin(GL_LINES);

            glVertex3f(0.f, 0.f, -50.f);
            glVertex3f(50.f, 0.f, -50.f);

        glEnd();

        glLoadIdentity();
        glTranslatef(0.f, 0.f, -200.f);

        // Screen axis
        glBegin(GL_LINES);

            glVertex3f(-400.f, 0.f, -60.f);
            glVertex3f(400.f, 0.f, -60.f);

            glVertex3f(0.f, 300.f, -60.f);
            glVertex3f(0.f, -300.f, -60.f);

        glEnd();

        glLoadIdentity();
        glTranslatef(mouseX, mouseY, -200.f);

        // Cursor position
        glBegin(GL_LINES);

            glVertex3f(-10.f, 0.f, -60.f);
            glVertex3f(10.f, 0.f, -60.f);

            glVertex3f(0.f, 10.f, -60.f);
            glVertex3f(0.f, -10.f, -60.f);

        glEnd();

        App.Display();
    }

    return 0;
}

2
General / Code::Blocks and SFML2
« on: October 21, 2010, 10:03:14 pm »
Thank you for your reply :)
I will have a go at using CMake and see what happens (I don't have much experience with this kind of stuff)

3
General / Code::Blocks and SFML2
« on: October 21, 2010, 11:16:36 am »
I am having trouble building the dll's from the files in the SMFL2 snapshot (Downloaded today from the downloads page)

I always just download the latest stable version of things like this, but this time I decided to see if I could get SFML2 working for me.

I have the latest version of Code::Blocks and as I already said I downloaded the SFML2 snapshot from the downloads page today.

This is what I did (Being completely new to this I'm guessing I made a simple mistake somewhere):

1) Download SFML snapshot and extract to my desktop

2) Go to the extracted folder and go to, sfml2\build\codeblocks\batch-build - Then use build.bat

3) Open the SFML.workspace file in the sfml2\build\codeblocks directory

4) Select Debug DLL_Win32 as my build target

5) Go to build and select Build Workspace

It creates the libsfml2-system-d.dll.a and libsfml2-system-d.dll files fine, then it hits the following problem on sfml-window

Code: [Select]
-------------- Build: Debug DLL_Win32 in sfml-window ---------------

Linking dynamic library: ..\..\lib\mingw\sfml2-window-d.dll
mingw32-g++.exe: ..\..\lib\mingw\libsfml2-system-d.a: No such file or directory
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings


I looked in my sfml2\lib\mingw directory and the build.bat had created the following:

libsfml2-audio-s.a
libsfml2-audio-s-d.a
libsfml2-graphics-s.a
libsfml2-graphics-s-d.a
libsfml2-network-s.a
libsfml2-network-s-d.a
libsfml2-window-s.a
libsfml2-window-s-d.a

But I see no libsfml2-system-d.a file

So I guess I'm just doing it wrong.
But like I said I am new to this so I am not sure exactly what to do.

Any help would be appreciated :)

Pages: [1]
anything