Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include "extras.h"
- #include <math.h>
- #include <iostream>
- using namespace std;
- // Globals
- int width = 1000;
- int height = 500;
- float rotationX = 0.0, rotationY = 0.0;
- int last_x, last_y;
- float x=0, y=0, a=400;
- bool R = true, L = false, troca1 = true, troca2 = true, troca3 = true;
- void idle(){
- float t, dt;
- static float tLast = 0.0;
- /* Get elapsed time and convert to s */
- t = glutGet(GLUT_ELAPSED_TIME);
- t /= 1000.0;
- /* Calculate delta t */
- dt = t - tLast;
- if (R)
- x = x + dt;
- if (L)
- x = x - dt;
- if (x > 10){
- R = false;
- L = true;
- }
- if (x>180.0){
- a=500;
- }
- if (x<0){
- R = true;
- L = false;
- }
- y = a*cos(x);
- if (y<0){
- y = y * (-1);
- }
- if (x > 90*(3.14/180) && troca1 == true){
- a = 300;
- troca1 = false;
- }
- if (x > 270*(3.14/180) && troca2 == true){
- a = 200;
- troca2 = false;
- }
- if (x > 450*(3.14/180) && troca3 == true){
- a = 100;
- troca3 = false;
- }
- printf ("x=%f y=%f a=%f\n", x*100, y*400, a);
- tLast = t;
- glutPostRedisplay();
- }
- void init(void)
- {
- glClearColor (0.5, 0.5, 0.5, 0.0);
- glShadeModel (GL_SMOOTH);
- glEnable(GL_DEPTH_TEST); // Habilita Z-buffer
- initLight(width, height);
- }
- void display(void)
- {
- float sphereSize = 30.0;
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPopMatrix();
- glBegin(GL_LINES);
- glColor3f (1.0, 1.0, 0.0);
- glVertex2f (0.0, 400.0);
- glVertex2f (1000.0, 400.0);
- glEnd();
- glPushMatrix();
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity ();
- glOrtho(0.0, width, 0.0, height, -sphereSize, sphereSize);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity ();
- setMaterial();
- glTranslatef(x*100, y, 0.0); // Posicionamento inicial da esfera
- glutSolidSphere(20.0, 100, 100);
- glutSwapBuffers();
- }
- void reshape (int w, int h)
- {
- width = w;
- height = h;
- glViewport (0, 0, (GLsizei) w, (GLsizei) h);
- }
- void keyboard (unsigned char key, int x, int y)
- {
- if(tolower(key) == 27) exit(0);
- }
- // Motion callback
- void motion(int x, int y )
- {
- glutPostRedisplay();
- }
- int main(int argc, char** argv){
- int b;
- cin >> b;
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize (width, height);
- glutInitWindowPosition (100, 100);
- glutCreateWindow("Animation Base");
- init ();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMotionFunc( motion );
- glutKeyboardFunc(keyboard);
- glutIdleFunc(idle);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement