Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __linux__
- #include <windows.h>
- #endif
- #include <GL/gl.h>
- #include <GL/glu.h>
- #define GLUT_DISABLE_ATEXIT_HACK
- #include <GL/glut.h>
- #include <time.h>
- #include <math.h>
- #include <stdio.h>
- int cameraposx = 0;
- int cameraposy = 0;
- int cameraposz = 3;
- int rotateX = 0;
- int rotateY = 0;
- int rotateZ = 0;
- GLuint texture;
- float rotationamount = 0;
- int lastx = 0, lasty = 0;
- void drawCube1(){ // Simple hollow cube
- glColor3f(1,1,1);
- glBegin(GL_LINE_STRIP); // lower square
- glVertex3f(0,0,0);
- glVertex3f(1,0,0);
- glVertex3f(1,0,-1);
- glVertex3f(0,0,-1);
- glVertex3f(0,0,0);
- glEnd();
- glBegin(GL_LINE_STRIP); // Upper square
- glVertex3f(0,1,0);
- glVertex3f(1,1,0);
- glVertex3f(1,1,-1);
- glVertex3f(0,1,-1);
- glVertex3f(0,1,0);
- glEnd();
- glBegin(GL_LINES);
- glVertex3f(0,0,0);
- glVertex3f(0,1,0);
- glVertex3f(1,0,0);
- glVertex3f(1,1,0);
- glVertex3f(1,0,-1);
- glVertex3f(1,1,-1);
- glVertex3f(0,0,-1);
- glVertex3f(0,1,-1);
- glEnd();
- }
- //
- //void readtexture()
- //{
- //GLuint *TextureImage[1];
- //
- //if (LoadBitmap(TextureImage[0], "a.bmp"))
- //{
- // printf("a.bmp!\n");
- //{
- //glGenTextures(1, &texture[0]);
- //glBindTexture(GL_TEXTURE_2D, texture[0]);
- //glTexImage2D(GL_TEXTURE_2D,
- // 0,
- // 3,
- // TextureImage[0]->sizeX,
- // TextureImage[0]->sizeY,
- // 0,
- // GL_RGB,
- // GL_UNSIGNED_BYTE,
- // TextureImage[0]->data);
- //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- //}
- //}
- void Init()
- {
- // readtexture();
- glEnable(GL_TEXTURE_2D);
- glShadeModel(GL_SMOOTH); // Enable Smooth Shading
- glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
- glClearDepth(1.0f); // Depth Buffer Setup
- glEnable(GL_DEPTH_TEST); // Depth Buffer Setup
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
- }
- void drawNiceCube(){
- glBegin(GL_QUADS); // Draw A Quad
- glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
- glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
- glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
- glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
- glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
- glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
- glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
- glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
- glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
- glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
- glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
- glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
- glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
- glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
- glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
- glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
- glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
- glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
- glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
- glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
- glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
- glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
- glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
- glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
- glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
- glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
- glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
- glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
- glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
- glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
- glEnd();
- }
- void drawCube2(){ // Lit cube
- glColor3f(1,1,0);
- glBegin(GL_QUADS); // bottom
- glVertex3f(0,0,0);
- glVertex3f(1,0,0);
- glVertex3f(1,0,-1);
- glVertex3f(0,0,-1);
- glEnd();
- glColor3f(1,1,1);
- glBegin(GL_QUADS); // top
- glVertex3f(0,1,0);
- glVertex3f(1,1,0);
- glVertex3f(1,1,-1);
- glVertex3f(0,1,-1);
- glEnd();
- glColor3f(1,0,1);
- glBegin(GL_QUADS); // front
- glVertex3f(0,0,0);
- glVertex3f(1,0,0);
- glVertex3f(1,1,0);
- glVertex3f(0,1,0);
- glEnd();
- glColor3f(1,0,.5);
- glBegin(GL_QUADS); // back
- glVertex3f(1,0,-1);
- glVertex3f(0,0,-1);
- glVertex3f(0,1,-1);
- glVertex3f(1,1,-1);
- glEnd();
- glColor3f(1,0,.5);
- glBegin(GL_QUADS); // left
- glVertex3f(0,0,0);
- glVertex3f(0,0,-1);
- glVertex3f(0,1,-1);
- glVertex3f(0,1,0);
- glEnd();
- glColor3f(.5,1,.5);
- glBegin(GL_QUADS); // right
- glVertex3f(1,0,0);
- glVertex3f(1,0,-1);
- glVertex3f(1,1,-1);
- glVertex3f(1,1,0);
- glEnd();
- // glColor3f(1,1,0);
- // glBegin(GL_QUAD_STRIP);
- // glVertex3f(0, 0, 0);
- // glVertex3f(1, 0, 0);
- // glVertex3f(0, 0, 1);
- // glVertex3f(1, 0, 1);
- //
- // glVertex3f(0, 1, 1);
- // glVertex3f(1, 1, 1);
- // glVertex3f(0, 1, 0);
- // glVertex3f(1, 1, 0);
- // glEnd();
- //
- // glColor3f(1,0,0);
- // glBegin(GL_QUAD_STRIP);
- // glVertex3f(1, 1, 1);
- // glVertex3f(1, 0, 1);
- // glVertex3f(1, 1, 0);
- // glVertex3f(1, 0, 0);
- //
- // glVertex3f(0, 1, 0);
- // glVertex3f(0, 0, 0);
- // glVertex3f(0, 1, 1);
- // glVertex3f(0, 0, 1);
- // glEnd();
- }
- void drawCube3() // Textured cube
- {
- }
- //keeps track of location of primitive and rotation of it
- void lookat()
- {
- // glLoadIdentity();
- }
- void drawScene() // Draw the actual scene
- {
- // glLineWidth(10);
- glColor3ub(255,0,0);
- glBegin(GL_LINES);
- glVertex3f(0,20,0);
- glVertex3f(0,-20,0);
- glEnd();
- glColor3ub(0,0,255);
- glBegin(GL_LINES);
- glVertex3f(20,0,0);
- glVertex3f(-20,0,0);
- glEnd();
- glColor3ub(0,255,0);
- glBegin(GL_LINES);
- glVertex3f(0,0,20);
- glVertex3f(0,0,-20);
- glEnd();
- }
- void draw(void) // Reshape function and idle rendering function
- {
- GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
- GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
- glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
- glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
- // Init();
- glViewport (0, 0, 640, 480);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity ();
- gluPerspective(45, 640/480.0, .5, 100);
- glMatrixMode(GL_MODELVIEW); //select the modelview matrix.
- glLoadIdentity ();
- gluLookAt(0,0,5,
- 0,0,0,
- 0,1,0);
- glRotatef(rotateX,1,0,0);
- glRotatef(rotateY,0,1,0);
- glRotatef(rotateZ,0,0,1);
- drawScene();
- //rotate this cube around. :P
- glPushMatrix();
- // glRotatef(rotationamount,0,0,1);
- // drawCube2();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(-2,1,0);
- // glutSolidCube(1);
- drawNiceCube();
- glPopMatrix();
- glPushMatrix();
- glColor3f(1.0f,0.0f,0.0f);
- glutWireCube(1);
- glPopMatrix();
- glutSwapBuffers();
- }
- void processKeyboard(unsigned char key, int x, int y) // Receive keyboard inputs
- {
- switch (key){
- case 'w':
- rotateX -= 30;
- break;
- case 's' :
- rotateY -= 30;
- break;
- case 'p' :
- rotateX += 30;
- break;
- case 'z' :
- rotateZ -= 30;
- break;
- case 'm' :
- rotateZ += 30;
- break;
- case 'l' :
- rotateY += 30;
- break;
- default: break;
- }
- }
- void processMouse(int button, int state, int x, int y)
- {
- if(state == GLUT_DOWN && button == GLUT_LEFT){
- //do computations
- rotationamount = x-lastx ;
- lastx = x;
- lasty = y;
- }
- }
- main(int argc, char **argv) {
- //Initialization functions
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- Init();
- glutInitWindowPosition(100,100); //Set window position
- glutInitWindowSize(640,480); //Set window size
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glutCreateWindow("Lighting and Textures"); //Create the window
- glutDisplayFunc(draw); // Call reshape function
- glutIdleFunc(draw); // The idle rendering function
- glutKeyboardFunc(processKeyboard);
- glutMouseFunc(processMouse);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement