Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #if defined(__APPLE__)
- #include <OpenGL/OpenGL.h>
- #include <GLUT/GLUT.h>
- #else
- #include <GL/gl.h>
- #include <GL/freeglut.h>
- #endif
- using namespace std;
- bool boto_f = false;
- bool boto_v = false;
- double y_anterior_t = 0.0;
- double x_anterior_t = 0.0;
- double alfa_t = 0.0;
- double beta_t = 0.0;
- double y_anterior_f = 0.0;
- double x_anterior_f = 0.0;
- double alfa_f = 0.0;
- double beta_f = 0.0;
- void teclat(unsigned char c, int x, int y) {
- switch(c) {
- case 'h': cout << "Ayuda: " << endl;
- cout << "h: consultar ayuda" << endl;
- cout << "f: rotacio" << endl;
- cout << "v: scalar" << endl;
- cout << "ESC: salir del programa" << endl;
- break;
- case 'f': boto_f = true;
- break;
- case 'v': boto_v = true;
- break;
- case 27: exit(0);
- break;
- }
- }
- void iniMovRaton(int x, int y) {
- if (boto_f) {
- alfa_f += x - x_anterior_f;
- x_anterior_f = alfa_f;
- beta_f += y - y_anterior_f;
- y_anterior_f = beta_f;
- glutPostRedisplay();
- }
- if (boto_v) {
- double x_nueva = x;
- double y_nueva = y;
- x_nueva = ((2.0*double(x))/glutGet(GLUT_WINDOW_WIDTH))-1.0;
- y_nueva = 1.0-((2.0*double(y))/glutGet(GLUT_WINDOW_HEIGHT));
- alfa_t += x_nueva - x_anterior_t;
- x_anterior_t = alfa_t;
- beta_t += y_nueva - y_anterior_t;
- y_anterior_t = beta_t;
- glutPostRedisplay();
- }
- }
- void iniRaton(int button, int state, int x, int y) {
- if (state == GLUT_UP) {
- boto_f = false;
- boto_v = false;
- }
- glutPostRedisplay();
- }
- void reshape(int w, int h) {
- if (w > h) glViewport((w-h)/2,0,h,h);
- else glViewport(0,(h-w)/2,w,w);
- }
- void pinta_ninot() {
- glPushMatrix();
- glTranslated(0,0,0);
- glutWireSphere(0.4,16,16);
- glPopMatrix();
- glPushMatrix();
- glTranslated(0,0.6,0);
- glutWireSphere(0.2,16,16);
- glPopMatrix();
- glPushMatrix();
- glColor3f(0.0,0.0,1.0);
- glTranslated(0.1,0.6,0);
- glRotatef(90,0,1.0,0);
- glutWireCone(0.1,0.2,8,8);
- glPopMatrix();
- }
- void refresh(void) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- //glLoadIdentity();
- glPushMatrix();
- glColor3d(1,1,1);
- glRotated(alfa_f,0,1,0);
- glRotated(beta_f,1,0,0);
- //glScaled(1.0-alfa_t,1.0-beta_t,0);
- pinta_ninot();
- glPopMatrix();
- glutSwapBuffers();
- }
- void initGL() {
- glClearColor(0,0,0,1);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-1,1,-1,1,-1,1);
- glMatrixMode(GL_MODELVIEW);
- }
- int main(int argc, const char *argv []) {
- //inicializaciones
- glutInit(&argc, (char**)argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(600,600);
- glutCreateWindow("IDI: Practiques OpenGL");
- initGL();
- //callbacks
- glutDisplayFunc(refresh);
- glutMouseFunc(iniRaton);
- glutMotionFunc(iniMovRaton);
- glutKeyboardFunc(teclat);
- glutReshapeFunc(reshape);
- //bucle procesamiento eventos
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement