177 lines
3.7 KiB
C++
Executable file
177 lines
3.7 KiB
C++
Executable file
#include <cstdlib>
|
|
|
|
// for mac osx
|
|
#ifdef __APPLE__
|
|
#include <OpenGL/gl.h>
|
|
#include <OpenGL/glu.h>
|
|
#include <GLUT/glut.h>
|
|
#else
|
|
// only for windows
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
// for windows and linux
|
|
#include <GL/gl.h>
|
|
#include <GL/glu.h>
|
|
#include <GL/freeglut.h>
|
|
#include <math.h>
|
|
#endif
|
|
|
|
#include <iostream>
|
|
using namespace std;
|
|
|
|
#define SPEED 0.1 // OpenGL unit
|
|
#define ANG_SPEED 0.01 // degrees
|
|
|
|
double x_cam = 0.0;
|
|
double y_cam = 0.0;
|
|
double z_cam = 5.0;
|
|
|
|
double angleX_cam = 0.0;
|
|
double angleY_cam = -M_PI / 2;
|
|
|
|
void display()
|
|
{
|
|
/* define the projection transformation */
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
gluPerspective(60, 1, 1, 10);
|
|
|
|
/* define the viewing transformation */
|
|
glMatrixMode(GL_MODELVIEW);
|
|
glLoadIdentity();
|
|
gluLookAt(
|
|
x_cam, y_cam, z_cam,
|
|
x_cam + cos(angleY_cam), y_cam + sin(angleX_cam), z_cam + sin(angleY_cam) * cos(angleX_cam),
|
|
0, cos(angleX_cam), 0);
|
|
|
|
/* clear window */
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
// set current matrix as GL_MODELVIEW
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
// draw scene
|
|
|
|
// add a copy of the curr. matrix to the stack
|
|
glPushMatrix();
|
|
|
|
glPushMatrix();
|
|
// translate by -3 on the z
|
|
glTranslatef(0, 0, -3);
|
|
// set drawing color to red
|
|
glColor3f(1.0f, 0.0f, 0.0f);
|
|
// middle red teapot
|
|
glutWireTeapot(1);
|
|
// translate by 2 on the y
|
|
glTranslatef(0, 2, 0);
|
|
// set drawing color to blue
|
|
glColor3f(0.0f, 1.0f, 0.0f);
|
|
// rotate 90 deg around x
|
|
glRotatef(90, 1.0f, 0.0f, 0.0f);
|
|
// top green teapot
|
|
glutWireTeapot(1);
|
|
// pop the current matrix
|
|
glPopMatrix();
|
|
|
|
// translate -2 on y and -1 on z
|
|
glTranslatef(0, -2, -1);
|
|
// set drawing color to blue
|
|
glColor3f(0.0f, 0.0f, 1.0f);
|
|
// bottom blue teapot
|
|
glutWireTeapot(1);
|
|
|
|
// pop the current matrix
|
|
glPopMatrix();
|
|
|
|
/* flush drawing routines to the window */
|
|
glFlush();
|
|
}
|
|
|
|
// Function called everytime a key is pressed
|
|
void key(unsigned char key, int x, int y)
|
|
{
|
|
switch (key)
|
|
{
|
|
case 'z':
|
|
z_cam -= SPEED;
|
|
break;
|
|
case 's':
|
|
z_cam += SPEED;
|
|
break;
|
|
case 'q':
|
|
x_cam -= SPEED;
|
|
break;
|
|
case 'd':
|
|
x_cam += SPEED;
|
|
break;
|
|
case 'a':
|
|
y_cam -= SPEED;
|
|
break;
|
|
case 'e':
|
|
y_cam += SPEED;
|
|
break;
|
|
case 27:
|
|
exit(EXIT_SUCCESS);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void specialkey(int key, int x, int y)
|
|
{
|
|
switch (key)
|
|
{
|
|
case GLUT_KEY_LEFT:
|
|
angleY_cam -= ANG_SPEED;
|
|
break;
|
|
case GLUT_KEY_RIGHT:
|
|
angleY_cam += ANG_SPEED;
|
|
break;
|
|
case GLUT_KEY_DOWN:
|
|
angleX_cam -= ANG_SPEED;
|
|
break;
|
|
case GLUT_KEY_UP:
|
|
angleX_cam += ANG_SPEED;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void reshape(int width, int height)
|
|
{
|
|
/* define the viewport transformation */
|
|
glViewport(0, 0, width, height);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
/* initialize GLUT, using any commandline parameters passed to the
|
|
program */
|
|
glutInit(&argc, argv);
|
|
|
|
/* setup the size, position, and display mode for new windows */
|
|
glutInitWindowSize(500, 500);
|
|
glutInitWindowPosition(0, 0);
|
|
glutInitDisplayMode(GLUT_RGB);
|
|
|
|
/* create and set up a window */
|
|
glutCreateWindow("hello, navigator!");
|
|
// Set up the callback functions
|
|
// for display
|
|
glutDisplayFunc(display);
|
|
// for the keyboard
|
|
glutKeyboardFunc(key);
|
|
// for special keys
|
|
glutSpecialFunc(specialkey);
|
|
// for reshaping
|
|
glutReshapeFunc(reshape);
|
|
|
|
/* tell GLUT to wait for events */
|
|
glutMainLoop();
|
|
}
|