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

Author Topic: sf::VertexArray and glTranslate  (Read 2417 times)

0 Members and 1 Guest are viewing this topic.

sfml_fan

  • Newbie
  • *
  • Posts: 8
    • View Profile
sf::VertexArray and glTranslate
« on: September 22, 2013, 04:12:13 am »
1) I just started using the sf::VertexArray class.  From what I can tell glTranslate is not "compatible".  I cannot seem to move the vertex array object around the screen using glTranslate.  Should I be using sf::Transform to reposition the object? 

2) In order to draw a triangle using raw opengl while also drawing an object with sf::VertexArray, I *have* to wrap the VA call with window.pushGLStates(); window.popGLStates(); lines.  Does anybody know why this is the case?

#include <SFML/Graphics.hpp>
#include <GL/gl.h>
#include <GL/glu.h>

int main() {
  std::vector<sf::VideoMode> VModes = sf::VideoMode::getFullscreenModes();
  sf::RenderWindow window(sf::VideoMode(VModes[0].width, VModes[0].height), "SFML window", sf::Style::Fullscreen);
  window.setFramerateLimit(60);

  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClearDepth(1.0);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glShadeModel(GL_SMOOTH);
 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0f,(GLfloat)VModes[0].width/(GLfloat)VModes[0].height,0.1f,100.0f);
 
  glMatrixMode(GL_MODELVIEW);
   
  while (1) {
    sf::Event event;
    while(window.pollEvent(event)) {
      if(event.type == sf::Event::Closed)                  { goto CLEANUP; }
      if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { goto CLEANUP; }
    }
    window.clear();
 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
 
    glTranslatef(0.0f,0.0f,-6.0f);
 
    // triangle
    glBegin(GL_POLYGON);
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glVertex3f( 1.0f,-1.0f, 0.0f);
    glVertex3f(-1.0f,-1.0f, 0.0f);
    glEnd();

    window.pushGLStates();
    glTranslatef(3.0f, 0.0f, 0.0f);
 
    // another triangle that ignores glTranslatef on my machine from what i can tell
    sf::VertexArray triangle(sf::Triangles, 3);
    triangle[0].position = sf::Vector2f(100, 100);
    triangle[1].position = sf::Vector2f(200, 100);
    triangle[2].position = sf::Vector2f(200, 200);
    triangle[0].color    = sf::Color::Red;
    triangle[1].color    = sf::Color::Blue;
    triangle[2].color    = sf::Color::Green;
    window.draw(triangle);
 
    window.popGLStates();
     
    window.display();
  }
  if(0) {
CLEANUP:
    window.close();
    return EXIT_SUCCESS;
  }
  return EXIT_FAILURE;
}
 

sfml_fan

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: sf::VertexArray and glTranslate
« Reply #1 on: September 22, 2013, 06:59:59 am »
Well for #1 sf::Transform is what I had to use in order to shift the vertex array object around the screen.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::VertexArray and glTranslate
« Reply #2 on: September 22, 2013, 08:03:54 am »
1) I just started using the sf::VertexArray class.  From what I can tell glTranslate is not "compatible".  I cannot seem to move the vertex array object around the screen using glTranslate.  Should I be using sf::Transform to reposition the object? 
Yes, of course you have to use the API. I don't know what gave you the idea that glTransform() would work, already because the storage of sf::VertexArray itself is software-based.


2) In order to draw a triangle using raw opengl while also drawing an object with sf::VertexArray, I *have* to wrap the VA call with window.pushGLStates(); window.popGLStates(); lines.  Does anybody know why this is the case?
Because SFML maintains its own OpenGL states that can conflict with your own rendering.

Is there a reason why you need OpenGL at all? Don't make your life harder than necessary...


    goto CLEANUP;

  if(0) {
CLEANUP:
    window.close();
    return EXIT_SUCCESS;
  }
This is really not how you should program :o

The window.close() is unnecessary. Because of RAII, C++ objects are able to cleanup themselves. You can therefore replace the goto with return statements.


if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
Don't mix events and real-time input. Have a look at the tutorials to see how to handle events correctly.
« Last Edit: September 22, 2013, 08:05:26 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sfml_fan

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: sf::VertexArray and glTranslate
« Reply #3 on: September 23, 2013, 08:53:22 am »
Unless of course my program has memory management it needs to do at the end.  Sure this one doesn't, but I am a bit picky about doing my best to clean things up.  Sorry that I left that (in your mind) disturbing code around.  Feel free to see similar examples of "bad" programming in the openssl library :)

I went and read the Events tutorial as you suggested, but it seems to me that whether or not one should "mix events and real-time input" is really up to the programmer.  The example in which one should *not* do such things is when expecting to have smooth movement/high level responsiveness take place.  Clearly the sf::Keyboard::Escape has nothing to do with anticipating or expecting smooth and timely responsiveness. 

I don't think you were trying to be this way, but I found your tone slightly pretentious.  I would recommend in the future that you refrain from telling people "how to code" and try to stay focused on the task of helping them understand SFML itself.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: sf::VertexArray and glTranslate
« Reply #4 on: September 23, 2013, 09:19:23 am »
This might sound kinda harsh, but Nexus is absolutely right.  There is such a thing as good and bad coding style, and snippets like what Nexus commented on is most certainly bad coding style.  It does work, but if you do things like that regularly your code will eventually become error-prone and/or unreadable, to the point where no one including yourself will be able to modify or debug it easily once it gets complicated.

Besides, you don't even need to take the cleanup code out of the event loop to avoid the sin of duplication (though even that isn't much of a sin for a mere two lines).  You could just do this:
Quote
if(event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
    window.close();
    return EXIT_SUCCESS;
 }

Of course, as he said you should avoid mixing events and real-time because again, when things get complicated, having them mixed up will start to cause very strange behavior that no one wants.  It does work here, but the bad habit will screw you over eventually, and it's not like you get any benefit from doing it this way.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::VertexArray and glTranslate
« Reply #5 on: September 23, 2013, 09:53:16 am »
Unless of course my program has memory management it needs to do at the end.  Sure this one doesn't, but I am a bit picky about doing my best to clean things up.
Modern C++ programs don't have manual memory management. They use the RAII idiom, which ensures resources (including memory) are cleaned up automatically, making the use of goto unnecessary. This makes code much simpler and less error-prone, there is really no reason to not use it ;)

I went and read the Events tutorial as you suggested, but it seems to me that whether or not one should "mix events and real-time input" is really up to the programmer.
No, it's not a matter of taste. Events are used for state changes, and real-time input to check the current state. But it's unusual to check real-time input inside the event loop.

Sorry that I left that (in your mind) disturbing code around. [...] I would recommend in the future that you refrain from telling people "how to code" and try to stay focused on the task of helping them understand SFML itself.
Sorry if it sounded this way, it was not my intention. I really meant my post as good advice, and I hope it helps you to improve your code style.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: