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

Author Topic: OpenGL object position and rotation not changing realtime  (Read 7321 times)

0 Members and 1 Guest are viewing this topic.

theodor

  • Newbie
  • *
  • Posts: 17
    • View Profile
OpenGL object position and rotation not changing realtime
« on: August 06, 2012, 10:32:56 pm »
When I add rotation to the object, or when I tell it to translate when I press a specific button, it just doesn't show up and the object stays the same. It does show up however if I define it in code.

Here's the smaller code as requested by Laurent:
(opengl related stuff)
Code: [Select]
int InitGL() // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 220.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return true; // Initialization Went OK
}

void ResizeGLScene(int width, int height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glViewport(0,0,width,height); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,1000.0f);


glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
float angle = 0.0f;
int DrawGLScene() // Here's Where We Do All The Drawing
{
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f,0.0f,-200.0f);
  glRotatef(angle, 0.0f, 0.0f, 1.0f); // Move Left 1.5 Units And Into The Screen 6.0
       glBegin(GL_QUADS);

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

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

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

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

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

            glVertex3f(-50.f, 50.f,  50.f);
            glVertex3f(-50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f,  50.f);
glEnd();
angle += 0.5f;
if (angle >= 360.f)
        angle = 0.0f; // Done Drawing The Quad
return true; // Keep Going
}
sfml drawing,  DrawGLScene goes before the following snippet.
Code: [Select]
if (IsMoving == false)
        {
             App.pushGLStates();
             if (Dir == 0)
             {
                 App.draw(idleUp);
             }
             else if (Dir == 1)
             {
                  App.draw(idleDown);
             }
             else if (Dir == 2)
             {
                   App.draw(idleLeft);
             }
             else if (Dir == 3)
             {
                 App.draw(idleRight);
             }
             App.popGLStates();
        }
        else if (IsMoving == true)
        {
            App.pushGLStates();
            if (Dir == 0)
            {
                App.draw(runningLeft);
            }
            else if (Dir == 1)
            {
                App.draw(runningLeft);
            }
            else if (Dir == 2)
            {
                   App.draw(runningLeft);
            }
            else if (Dir == 3)
            {
                App.draw(runningRight);
            }
            App.popGLStates();
        }

No idea what might be causing it because for instance I ported a program from the 1.3 sfml tutorial (rotating cube) to 2.0 sfml and it worked.
« Last Edit: August 07, 2012, 01:01:58 am by theodor »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL object position and rotation not changing realtime
« Reply #1 on: August 06, 2012, 10:59:40 pm »
Could you post a smaller code (but still complete), that focuses on the problem? It would really help people that want to help you ;)
Laurent Gomila - SFML developer

theodor

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: OpenGL object position and rotation not changing realtime
« Reply #2 on: August 07, 2012, 01:02:11 am »
done!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL object position and rotation not changing realtime
« Reply #3 on: August 07, 2012, 08:03:07 am »
This code is not complete. This is important so that we can test it.

http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368
Laurent Gomila - SFML developer

theodor

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: OpenGL object position and rotation not changing realtime
« Reply #4 on: August 07, 2012, 12:48:46 pm »
After two hours, I managed to trim it down and isolate the problem while keeping the code testable.

The cube rotates fine after removing this:
           App.pushGLStates();

                 App.draw(idleUp);

             App.popGLStates();
 
If I only remove the push and pop state functions, then the cube doesn't show up.

Full complete... minimal... working code:
 
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <GL/glew.h>
#include <SFML/OpenGL.hpp>

#include <GL/glu.h>
#define M_PI           3.14159265358979323846

////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////

void NextSprite(sf::Sprite &Sprite, int SpriteSizeY, int rectHorizontal, int rectVertical);

int InitGL()                                                                            // All Setup For OpenGL Goes Here
{
        glShadeModel(GL_SMOOTH);                                                        // Enable Smooth Shading
        glClearColor(0.0f, 0.0f, 220.0f, 0.5f);                         // Black Background
        glClearDepth(1.0f);                                                                     // Depth Buffer Setup
        glEnable(GL_DEPTH_TEST);                                                        // Enables Depth Testing
        glDepthFunc(GL_LEQUAL);                                                         // The Type Of Depth Testing To Do
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);      // Really Nice Perspective Calculations
        return true;                                                                            // Initialization Went OK
}

void ResizeGLScene(int width, int height)               // Resize And Initialize The GL Window
{
        if (height==0)                                                                          // Prevent A Divide By Zero By
        {
                height=1;                                                                               // Making Height Equal One
        }

        glViewport(0,0,width,height);                                           // Reset The Current Viewport

        glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
        glLoadIdentity();                                                                       // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window
        gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,1000.0f);


        glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
        glLoadIdentity();                                                                       // Reset The Modelview Matrix
}

int main()
{

    // Create the main rendering window
    sf::ContextSettings settings;
    settings.depthBits = 32;
    settings.stencilBits = 8;
    settings.antialiasingLevel = 8;
    settings.majorVersion = 3;
    settings.minorVersion = 0;

    sf::RenderWindow App(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, settings);
    App.setVerticalSyncEnabled(true);
    ResizeGLScene(App.getSize().x, App.getSize().y);
    InitGL();
    // Load the sprite image from a file

    sf::Texture IdleUp;

    if (!IdleUp.loadFromFile("idleUp.png"))
    {
        return EXIT_FAILURE;
    }

    sf::Sprite idleUp(IdleUp);
    idleUp.setPosition(50, 100);


    bool running = true;
    while (running == true)
    {
        // Process events
        sf::Event Event;

        while (App.pollEvent(Event))
        {
            // Close window : exit
            if (Event.type == sf::Event::Closed)
            {
                running = false;
            }
            else if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape))
            {
                running = false;
            }
            else if (Event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, Event.size.width, Event.size.height);
            }

        }


        // Clear screen
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
          static float angle = 0.0f;
         //DrawGLScene();
           glLoadIdentity();                                                                    // Reset The Current Modelview Matrix
                 glTranslatef(0.0f,0.0f, -260.0f);
                  glRotatef(angle, 0.0f, 1.0f, 1.0f);                                   // Move Left 1.5 Units And Into The Screen 6.0
               glBegin(GL_QUADS);

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

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

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

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

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

            glVertex3f(-50.f, 50.f,  50.f);
            glVertex3f(-50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f,  50.f);
                glEnd();
                angle += 0.5f;




             App.pushGLStates();

                 App.draw(idleUp);

             App.popGLStates();



        // Display window contents on screen
        App.display();
    }

    return EXIT_SUCCESS;
}
« Last Edit: August 07, 2012, 01:13:25 pm by Laurent »

theodor

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: OpenGL object position and rotation not changing realtime
« Reply #5 on: August 08, 2012, 02:39:46 pm »
Anybody? Why is the sfml drawing conflicting when the pop and push functions were made specifically to avoid that?
« Last Edit: August 08, 2012, 02:56:29 pm by theodor »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL object position and rotation not changing realtime
« Reply #6 on: August 08, 2012, 02:56:06 pm »
Since your program is very similar to the OpenGL example provided in the SFML source repository, you should:
- try it directly (it should work)
- transform it little by little to your own code
- see when it stops to work as expected
Laurent Gomila - SFML developer

theodor

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: OpenGL object position and rotation not changing realtime
« Reply #7 on: August 08, 2012, 03:23:28 pm »
As funny as it is... Just one small thing made the difference, nothing else. Doesn't even make sense though.

If I put this code anywhere in the running loop: glMatrixMode(GL_MODELVIEW);
It will work. Even though I had already set it in the initialization. It seems like this "matrix mode", gets
reset every time I draw using sfml. So programs that don't loop and only draw once, will render correctly.

I suppose this can be considered a bug? Or at least something very much worth mentioning in tutorials that pop and push glstates does not take care of the matrixmode.

Excuse me if what I said is partially nonsense, but that comes from the fact that I really do not understand what a matrixmode in opengl is. I just use it when it is necessary and it seems like it is necessary every time both opengl and sfml drawing/rendering is used.


Now I will have to... OH WAIT THANK GOD!!! Phew, I have the ... do I... hmm.. No, sadly. I do not have the full source code here anymore. Now I'll have to rewrite most of my code because code::blocks decided he would keep my code in the minimalistic version even though I undid all of the changes and saved before closing it yesterday... Oh well,  at least I still have my sprite clipper :)
« Last Edit: August 08, 2012, 03:31:57 pm by theodor »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL object position and rotation not changing realtime
« Reply #8 on: August 08, 2012, 03:36:51 pm »
It totally right. push/popGLStates are supposed to take care of this state, and it's therefore a bug if they don't. I'll have a look at the souce code to see what's wrong.

Quote
I really do not understand what a matrixmode in opengl is
There are several matrices that you can play with in OpenGL: the modelview one, the projection one, and texture ones. Before using one of the matrix functions (glLoadIdentity, glLoadMatrix, glTranslate, ...), you need to tell OpenGL which matrix you want to work with. That's what the matrix mode is: it says whether you're working on the projection matrix, the modelview matrix, or one of the texture matrices.

It's a good habit to always explicitely set the matrix mode before using matrix functions, so that you're sure that you work on the intented matrix, even if another piece of code worked on another one and left it active (like SFML does, apparently).
« Last Edit: August 08, 2012, 03:38:54 pm by Laurent »
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL object position and rotation not changing realtime
« Reply #9 on: August 08, 2012, 03:40:18 pm »
Ok, I've found the bug. It will be an easy one to fix.
Laurent Gomila - SFML developer

theodor

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: OpenGL object position and rotation not changing realtime
« Reply #10 on: August 08, 2012, 03:54:10 pm »
So when the bug is fixed, I will no longer have to set the matrixmode myself anymore?
That is great. Please do tell me when the bug is fixed and how I can get the fix, or if it's something I can do, then some instructions would be nice.

It's interesting that nobody except me has stumbled upon this yet. My project, which I started a very long time ago with a friend is a 2.5D rpg which is featuring one main character. It was initially going to be just 2D, so I've gone through SDL and allegro, but it just wasn't what I needed. Right now I'm also stuck with object collision junk variables in directX 9, so I haven't even gotten to the part to implement 2D.

Right now I pretty much have the perfect environment for 2D, all I need now is to integrate opengl rendering and for starters import some 3D models and set the camera. So 2D character(s) and 3D world, that is my plan.. Hopefully I will successfully do this and in turn, promote your SFML api.

It will however take a long time, but hopefully not so long for finally getting something to show. I'm the only one programming the game. My friend is the one making the sprites. So yeah, it'll take a while.
« Last Edit: August 08, 2012, 03:56:17 pm by theodor »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL object position and rotation not changing realtime
« Reply #11 on: August 08, 2012, 04:03:13 pm »
Quote
So when the bug is fixed, I will no longer have to set the matrixmode myself anymore?
Yep.

Quote
It's interesting that nobody except me has stumbled upon this yet.
After searching, I found one similar report:
http://en.sfml-dev.org/forums/index.php?topic=7949.0

Good luck for your project :)
Laurent Gomila - SFML developer

theodor

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: OpenGL object position and rotation not changing realtime
« Reply #12 on: August 08, 2012, 04:08:31 pm »
Oh, I actually saw that report, but it was before I ran into my own bug so I didn't even realize that.
And thank you.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL object position and rotation not changing realtime
« Reply #13 on: August 08, 2012, 08:42:37 pm »
It's fixed. If you want the new version, you must download SFML from github and recompile it.
Laurent Gomila - SFML developer

theodor

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: OpenGL object position and rotation not changing realtime
« Reply #14 on: August 09, 2012, 08:49:56 pm »
I think I will just wait for a release. As far as I remember, one would have to go through lots of hassle to compile the code (download a couple of programs). Will I suffer some significant performance loss just setting the matrixmode right now myself? If it is indeed that bad, then I will consider compiling the code myself.