PDA

View Full Version : app not redrawing?



ussyless
10-02-2010, 04:42 AM
Hi, so im pretty new to the whole c++ thing, im not sure if this is the codes fault or something i missed in gl/glut
anyway here it is


#include <glut.h>
#include "init_elements.h"
GLfloat spin=0.0;
GLshort xx = 10;

void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glBegin(GL_POINTS);
for (GLshort ii = 0; ii<=xx; ii++)
{
glVertex2f(ii,25);
}
glEnd();
glutSwapBuffers();

}

void step()
{
xx+=1 ;
if (xx==12)
{xx=0;}
glutPostRedisplay();
}

void reshape(int w, int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,512.0,512.0,0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse(int button,int state,int x,int y)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
if (state==GLUT_DOWN) break;

case GLUT_RIGHT_BUTTON: if (state==GLUT_DOWN) break;
}
}
int main(int argc, char **argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(512,512);
glutInitWindowPosition(100,100);
glutCreateWindow("Glut App");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
glutIdleFunc(step);
return 0;
}

it draws the first 10 pixels but it doesn't redraw it into an animation, of sorts
basically just want a line to grow to the right, then reset and start again
i've also tried swapping the GLshort's for int shorts

another thing, do i have to use GLshorts for this? or will normal int shorts work with the gl functions

sorry im pretty new to this c++ scene, its a lot more brainhurty than what i am used to


final note:
i canibalised this from an online example, just to use until i get used to the gl functions

marshats
10-02-2010, 10:29 AM
I tricky point about glut for people new to GLUT is that glutMainloop never returns. In fact you will never even reach the return 0; portion of the code although you need it to make C compilers happy. Try putting glutIdleFunc before glutMainLoop as



glutIdleFunc(step); // move to before
glutMainLoop();


Also once I fixed that, the code ran so fast so I added a delay in step just so I could see it updating. In linux, I used the usleep function (in windows there is sleep() instead if I remember correctly).



void step()
{
usleep(1e5);
xx+=1 ;
if (xx==12)
{xx=0;}
printf("%d\n",xx);
glutPostRedisplay();
}


As for GLshort for xx and ii -- no you don't have to use GLshort. You could use GLfloat which would be more compatible with glVertex2f which takes floating points anyhow. The C-compiler is casting ii from a short to a float behind the scenes as your code stands now. The compiler is actually doing glVertex2f((GLfloat)ii,25);

ussyless
10-02-2010, 08:13 PM
i thought i was using glvertex2s- thats what i will be using
anyway thanks, ill give your suggestions a try and report back