Jump to content
  • Advertisement
Sign in to follow this  
spaceJockey123

Animation and window width

This topic is 2641 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Chaps,
I am writing a program where a cube travels across the screen (X-axis). When it goes past the screen it should reappear back in the middle of the screen somewhere (using [color=#7A482F][font=Menlo][size=2][color=#3F217C]glutGet[color=#000000](GLUT_WINDOW_WIDTH[color=#000000]))[/font]. For some reason, it disappears past the screen and doesn't come back.

Please have a look at my code and see where I am going wrong.

[color=#d62b24][color=#7a482f]#include <stdlib.h>

[color=#d62b24][color=#7a482f]#include <GLUT/glut.h>




[color=#bf2e9d]static[color=#000000] float[color=#000000] posX = [color=#2832cf]0.0[color=#000000];

[color=#bf2e9d]static [color=#bf2e9d]float windowWidth;




[color=#bf2e9d]void[color=#000000] init(void[color=#000000])

{

[color=#3f217c][color=#000000] glClearColor[color=#000000]([color=#2832cf]0.0[color=#000000], [color=#2832cf]0.0[color=#000000], [color=#2832cf]0.0[color=#000000], [color=#2832cf]0.0[color=#000000]);

[color=#3f217c][color=#000000] glShadeModel[color=#000000]([color=#7a482f]GL_FLAT[color=#000000]);

}




[color=#bf2e9d]void display([color=#bf2e9d]void)

{



[color=#7a482f][color=#000000] [color=#4b8186]windowWidth[color=#000000] = [color=#3f217c]glutGet[color=#000000](GLUT_WINDOW_WIDTH[color=#000000]);



[color=#4b8186]posX += [color=#2832cf]0.01;



[color=#4b8186][color=#000000] [color=#bf2e9d]if[color=#000000](posX[color=#000000] > windowWidth[color=#000000])

{

[color=#4b8186][color=#000000] posX[color=#000000] -= windowWidth[color=#000000];

}



[color=#7a482f][color=#000000] [color=#3f217c]glClear[color=#000000](GL_COLOR_BUFFER_BIT[color=#000000]);

[color=#3f217c][color=#000000] glColor3f[color=#000000]([color=#2832cf]1.0[color=#000000], [color=#2832cf]1.0[color=#000000], [color=#2832cf]1.0[color=#000000]);

[color=#3f217c][color=#000000] glLoadIdentity[color=#000000]();



[color=#2832cf][color=#000000] [color=#3f217c]gluLookAt[color=#000000](0.0[color=#000000], 0.0[color=#000000], 5.0[color=#000000], 0.0[color=#000000], 0.0[color=#000000], 0.0[color=#000000], 0.0[color=#000000], 1.0[color=#000000], 0.0[color=#000000]);

[color=#2832cf][color=#000000] [color=#3f217c]glScalef[color=#000000](1.0[color=#000000], 2.0[color=#000000], 1.0[color=#000000]);

[color=#3f217c][color=#000000] glTranslatef[color=#000000]([color=#4b8186]posX[color=#000000], [color=#2832cf]0.0[color=#000000], [color=#2832cf]0.0[color=#000000]);

[color=#3f217c][color=#000000] glutWireCube[color=#000000]([color=#2832cf]1.0[color=#000000]);

[color=#3f217c][color=#000000] glFlush[color=#000000]();

[color=#3f217c][color=#000000] glutSwapBuffers[color=#000000]();

}




[color=#bf2e9d]void reshape([color=#bf2e9d]int w, [color=#bf2e9d]int h)

{

[color=#3f217c]glViewport([color=#2832cf]0, [color=#2832cf]0, ([color=#7340a3]GLsizei) w, ([color=#7340a3]GLsizei) h);

[color=#7a482f][color=#000000] [color=#3f217c]glMatrixMode[color=#000000](GL_PROJECTION[color=#000000]);

[color=#3f217c][color=#000000] glLoadIdentity[color=#000000]();

[color=#2832cf][color=#000000] [color=#3f217c]glFrustum[color=#000000](-1.0[color=#000000], 1.0[color=#000000], -1.0[color=#000000], 1.0[color=#000000], 1.5[color=#000000], 20.0[color=#000000]);

[color=#3f217c][color=#000000] glMatrixMode[color=#000000]([color=#7a482f]GL_MODELVIEW[color=#000000]);

}




[color=#bf2e9d]void idleFunc()

{

}




[color=#bf2e9d]void animate()

{

[color=#3f217c][color=#000000] glutPostRedisplay[color=#000000]();

}




[color=#bf2e9d]int main([color=#bf2e9d]int argc, [color=#bf2e9d]char** argv)

{

[color=#3f217c]glutInit(&argc, argv);

[color=#3f217c][color=#000000] glutInitDisplayMode[color=#000000]([color=#7a482f]GLUT_SINGLE[color=#000000] | [color=#7a482f]GLUT_RGB[color=#000000]);

[color=#3f217c][color=#000000] glutInitWindowSize[color=#000000]([color=#2832cf]1000[color=#000000], [color=#2832cf]500[color=#000000]);

[color=#3f217c][color=#000000] glutInitWindowPosition[color=#000000]([color=#2832cf]100[color=#000000], [color=#2832cf]100[color=#000000]);

[color=#3f217c][color=#000000] glutCreateWindow[color=#000000](argv[[color=#2832cf]0[color=#000000]]);

[color=#2e595d]init();

[color=#3f217c][color=#000000] glutDisplayFunc[color=#000000](display);

[color=#3f217c][color=#000000] glutReshapeFunc[color=#000000](reshape);

[color=#3f217c][color=#000000] glutIdleFunc[color=#000000](animate);

[color=#3f217c][color=#000000] glutMainLoop[color=#000000]();

[color=#bf2e9d][color=#000000] return[color=#000000] [color=#2832cf]0[color=#000000];

}

Share this post


Link to post
Share on other sites
Advertisement
The good news is, it eventually comes back on the screen. You just have to wait...a long, long, time.

I've never really done any OpenGL programming, so I can't really give you a full explanation on how to make it do what you're trying to do. I can, however, help point you in that direction. Someone else, more versed in OpenGL, can explain it.

In your display function, print out the posX value and monitor its value, while watching the GL window. You'll notice the cube goes off screen when the posX value is around 4.0 (you might have to set a smaller increment value to see it), yet you're checking it against a window that is 1000 pixels wide. Since I've never done OpenGL programming, I can only guess that your viewport/fulstrum/something, needs to be mapped somehow to the pixel width of your window.

Hope that helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!