Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ex2.
- #include <GL/freeglut.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- using namespace std;
- #include "camera.h"
- #include "keyboard.h"
- #include "mouse.h"
- Camera *cam;
- const GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
- const GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
- const GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
- const GLfloat light_position[] = {2.0f, 5.0f, 5.0f, 0.0f};
- void initGL(int width, int height)
- {
- cam = new Camera();
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glEnable(GL_COLOR_MATERIAL);
- glMatrixMode(GL_PROJECTION);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHTING);
- glLoadIdentity();
- gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 2.0f,100.0f);
- glMatrixMode(GL_MODELVIEW);
- }
- static void resize(int width, int height)
- {
- glViewport(0,0,width,height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 2.0f,100.0f);
- glMatrixMode(GL_MODELVIEW);
- }
- void renderBitmapString(float x, float y, void *font, char *string)
- {
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- glLoadIdentity();
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- glDisable(GL_LIGHTING);
- glColor3f(0.0f,1.0f,0.0f);
- glRasterPos2f(x,y);
- char *c;
- for(c=string; *c != '\0';c++)
- {
- glutBitmapCharacter(font, *c);
- }
- glEnable(GL_LIGHTING);
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
- glPopMatrix();
- }
- static void display(void)
- {
- static int frame, timebase=0;
- int time;
- char s[100];
- frame++;
- time=glutGet(GLUT_ELAPSED_TIME);
- if(time - timebase>1000)
- {
- sprintf(s,"[FPS:%4.2f] Lab 4: Navigare", frame*1000.0/(time-timebase));
- glutSetWindowTitle(s);
- timebase= time;
- frame=0;
- }
- static int lasttime=0;
- int rightnow = glutGet(GLUT_ELAPSED_TIME);
- float dt = ((float)(rightnow-lasttime))/1000.0f;
- lasttime = rightnow;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- cam->applyTransformation();
- cam->updateInteraction(dt);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- static float axisRot = 0.0f;
- static float globRotR = 0.0f;
- static float globRotG = 120.0f;
- static float globRotB = 240.0f;
- glColor3f(1.0f,0.0f,0.0f);
- glPushMatrix();
- glTranslatef(0.0f,0.0f,-20);
- glRotatef(globRotR, 0, 0, 1);
- glTranslatef(5.0f,0.0f,0.0f);
- glRotatef(axisRot,0,1,0);
- glutSolidCube(2);
- glPopMatrix();
- glColor3f(0.0f,1.0f,0.0f);
- glPushMatrix();
- glTranslatef(0.0f,0.0f,-20);
- glRotatef(globRotG, 0, 0, 1);
- glTranslatef(5.0f,0.0f,0.0f);
- glRotatef(axisRot,0,1,0);
- glutSolidCube(2);
- glPopMatrix();
- glColor3f(0.0f,0.0f,1.0f);
- glPushMatrix();
- glTranslatef(0.0f,0.0f,-20);
- glRotatef(globRotB, 0, 0, 1);
- glTranslatef(5.0f,0.0f,0.0f);
- glRotatef(axisRot,0,1,0);
- glutSolidCube(2);
- glPopMatrix();
- axisRot += 1.0f; axisRot=fmod(axisRot,360.0f);
- globRotR += 0.5f; globRotR=fmod(globRotR,360.0f);
- globRotG += 0.5f; globRotG=fmod(globRotG,360.0f);
- globRotB += 0.5f; globRotB=fmod(globRotB,360.0f);
- //punct de origine
- glColor3f(1.0f,1.0f,1.0f);
- glPushMatrix();
- glutSolidSphere(1,10,10);
- glPopMatrix();
- glutSwapBuffers();
- }
- static void idle(void)
- {
- glutPostRedisplay();
- }
- int main(int argc, char *argv[])
- {
- int width=640;
- int height=480;
- glutInit(&argc, argv);
- glutInitWindowSize(width, height);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("");
- glutReshapeFunc(resize);
- glutDisplayFunc(display);
- glutKeyboardFunc(Keyboard::keyDown);
- glutKeyboardUpFunc(Keyboard::keyUp);
- glutIdleFunc(idle);
- initGL(width, height);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement