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

Author Topic: openGL issue + window waits to draw  (Read 1692 times)

0 Members and 1 Guest are viewing this topic.

scyth3s

  • Newbie
  • *
  • Posts: 20
    • View Profile
openGL issue + window waits to draw
« on: May 16, 2011, 07:49:40 pm »
I'm currently having two issues, and I think that they are related to each other.

1st, the SFML window I create does not draw/display anything until I move the mouse over it.

Second, whenever I move the mouse, my openGL primitives get both smaller and closer together-- as long as the mouse is moving (any direction, and INSIDE the SFML window) until they disappear completely. I'll post the relevant code here, because I am currently stumped and I have no idea what to google for on this issue.

Code: [Select]


///////////////////////////////////
/// Main starts the program,
/// creates a GameManager and
/// has it loop until completion
///////////////////////////////////
int main(int argc, char** argv)
{
//constructor game manager and start looping
GameManager Manager("Template");
Manager.Loop();
}



Code: [Select]
GameManager::GameManager(string title)
{
//the dimentions of the window
int size[2] = {800, 600} ;

//creates the game window, sized to the two elements of the array declared above
window.Create(sf::VideoMode(size[0],size[1]), title);
this->initGL();
this->resize(size[0], size[1]);
}


GameManager::~GameManager(void)
{

}

const sf::Input& GameManager::GetInput()
{
return window.GetInput();
}

void GameManager::Loop()
{
//constantly loop through the window
while(window.IsOpened())
{
//go through the event processing loop
sf::Event Event;
while(window.GetEvent(Event))
{
//process event based on type
switch(Event.Type)
{
//resize if resized
case sf::Event::Resized:
resize(Event.Size.Width, Event.Size.Height);
break;

//close if closed
case sf::Event::Closed:
window.Close();
break;
}

this->update();
this->draw();

window.Display();
}
}
}

void GameManager::draw()
{
//clear the screen
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
gluLookAt(0,0,1,     0,0,0,     0,1,0);
//viewport code here
//////////////////////
//////////////////////
//////////////////////
///end viewport code

//Drawing code here

glMatrixMode(GL_MODELVIEW);
        //glLoadIdentity();
//for demo purposes, we draw a triangle
glPointSize(100);
glBegin(GL_TRIANGLES);
{
glVertex2f(-1,0);
glVertex2f(1,0);
glVertex2f(0,1);
}
glEnd();

//end drawing code

glFlush();
}

void GameManager::update()
{

}

void GameManager::initGL()
{
glClearColor(0,0,1,0);
glColor3f(0,1,0);
}

void GameManager::resize(float w, float h)
{
glViewport(0,0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, w /h, 1, 200);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(0,0,5, 0,0,0, 0,1,0);
}


Code: [Select]


class GameManager
{
public:
//////////////////
/// Creates the game window
/// And calls the resize method to properly set the window attributes and glViewport
/// \param The title of the game's window
//////////////////
GameManager(string title);

///////////////////
///
//////////////////
~GameManager(void);

//////////////////
/// Has the GameManager loop through the window cycle,
/// as well as updating and drawing stuff
//////////////////
void Loop();

///////////////////////////
/// Gets a reference to the window's input struct
///////////////////////////
const sf::Input& GetInput();

private:
////////////////////////
/// The render window
////////////////////////
sf::RenderWindow window;

//////////////////
/// The portion of the game currently running.
/// IE: MainMenu, OptionsMenu, etc
//////////////////
GameState state;

///////////////
/// Initializes the openGL states
//////////////
void initGL();

/////////////
/// Draws the game
/////////////
void draw();

////////////////
/// Updates everythign in need of updating
///////////////
void update();

///////////////
/// Resizes the window
///////////////
void resize(float w, float h);
};


The general gist is this:

Manager creates window. Manager updates and draws window repeatedly. Whenever the mouse is moved, the primitives (the vertex2f calls) get closer to the origin, and smaller, until they disappear completely.

Any help would be AMAZING.

I have tried:

[list=]
removing, moving, and in general experimenting with the commented out calls to glLoadIdentity() in the draw method.

debugging. The code stays in these 3 files, plus of course the openGL ones which I cannot see in VS, the entire time.

Changing the primitives from GL_POINTS to GL_LINES, and GL_TRIANGLES.

Removing GL_FLUSH.
[/list]
"My church is not full of..."

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
openGL issue + window waits to draw
« Reply #1 on: May 16, 2011, 09:08:36 pm »
Quote
1st, the SFML window I create does not draw/display anything until I move the mouse over it.

Normal, your drawing code is inside the event loop. So it won't be executed until an event occurs.
Laurent Gomila - SFML developer

scyth3s

  • Newbie
  • *
  • Posts: 20
    • View Profile
openGL issue + window waits to draw
« Reply #2 on: May 17, 2011, 06:46:25 am »
Quote from: "Laurent"
Quote
1st, the SFML window I create does not draw/display anything until I move the mouse over it.

Normal, your drawing code is inside the event loop. So it won't be executed until an event occurs.


Oh wow. I feel stupid now.

That still didn't fix the 2nd problem, which I think is probably rooted in some faulty openGL commands I've called.

Well, I did fix it. TUrns out I was not properly changing the z buffer with gluPerspective to show points as far as 5 units away. -_-
"My church is not full of..."