0

here's the problem: my triangle not rotating! Why? What I've done wrong?

Here's the code:

main.cpp

#include <iostream>
#include <GL/glut.h>
#include <GL/gl.h>
#include "myobject.h"

using namespace std;

Vector3f position = Vector3f(0.0f,0.0f,0.0f);

myobject *Ob = new myobject(position);

float _angle = 0.1f;

void render(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();      

    _angle += 0.4f;

    glPushMatrix();  
          glTranslatef (0.0f, -0.0f, -0.2f);
          glRotatef(_angle, 1.0f, 0.0f, 0.0f);
          Ob->draw();
    glPopMatrix();

    glutSwapBuffers();



}

void init(){
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(640, 480);
    glutCreateWindow("test");

    glutDisplayFunc(render);

    glutMainLoop();
}


int main(int argc, char * argv[]) {

    glutInit(&argc, argv);
    init();
    return 0;
}

myobject.cpp

#include <iostream>
#include <GL/glut.h>
#include <GL/gl.h>
#include "myobject.h"

using namespace std;

myobject::myobject(Vector3f pos)
{
    _position = pos;
    _size = 0.0f;
}

myobject::~myobject()
{
}


void myobject::draw()
{
    glBegin(GL_TRIANGLES);
     glColor3f(0.2f,0.2f,0.5f);
     glVertex3f( 0.0f, 0.0f, 0.5f);  
     glVertex3f(-1.0f,-0.5f, 0.0f);  
     glVertex3f( 0.5f,-0.5f, 0.0f);  
    glEnd();
}
DanilGholtsman
  • 2,354
  • 4
  • 38
  • 69

1 Answers1

1

Are you sure you have animation running?

probably you just have to call glutPostRedisplay() your render() function. That way GLUT will update the window and the animation.

Better option is to use:

glutIdleFunc(myIdle);

and:

void myIdle()
{
    updateScene(deltaTime);
    renderScene();
    glutSwapBuffers();
}

Another thing: try to use modern opengl... not the old one...

fen
  • 9,835
  • 5
  • 34
  • 57
  • oh, thanks! i put `glutPostRedisplay()` in `render()` and now is working! – DanilGholtsman Sep 21 '13 at 11:26
  • well, i am pretty new to OGL, so shaders looks more complicated for me, thats why I use the old one. – DanilGholtsman Sep 21 '13 at 11:29
  • 1
    The original GLUT documentation discourages drawing from the idle function. Instead the idle handler should call glutPostRedisplay. Or if you're doing the frame timing in the render code you can register glutPostRedisplay itself as idle function. – datenwolf Sep 21 '13 at 11:35
  • @datenwolf does this apply to freeGlut as well? I've found only this: http://www.lighthouse3d.com/tutorials/glut-tutorial/glutpostredisplay-vs-idle-func/. From my experience CPU is onl 4% using onIdle... – fen Sep 22 '13 at 16:00
  • 2
    @fen: The problem is not so much CPU utilization, but the internal structure of (the original) GLUT could be "confused" by calling glutPostRedisplay while being called in the display function itself. I don't know if it's a problem for FreeGLUT, but I'd err on the save side. – datenwolf Sep 22 '13 at 16:06