Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL.h"
- #include "stdio.h"
- #include "gl/glew.h"
- #include "gl/glut.h"
- //DALTON BURTS
- //a robot whose head moves back and forth at 90 degree angles. I noticed that the speed is different on different computers. had to lower my speed from .5 to .01
- float yLocation = 0.0f; //measure how far the y axis has rotated
- bool tooFar = false; //gauge distance head has moved
- void cubey(float size)
- {
- glBegin(GL_QUADS);
- //front face of the cube
- glVertex3f(size / 2, size / 2, size / 2);
- glVertex3f(-size / 2, size / 2, size / 2);
- glVertex3f(-size / 2, -size / 2, size / 2);
- glVertex3f(size / 2, -size / 2, size / 2);
- //left face
- glVertex3f(-size / 2, size / 2, size / 2);
- glVertex3f(-size / 2, size / 2, -size / 2);
- glVertex3f(-size / 2, -size / 2, -size / 2);
- glVertex3f(-size / 2, -size / 2, size / 2);
- //backface
- glVertex3f(size / 2, size / 2, -size / 2);
- glVertex3f(-size / 2, size / 2, -size / 2);
- glVertex3f(-size / 2, -size / 2, -size / 2);
- glVertex3f(size / 2, -size / 2, -size / 2);
- //right face
- glVertex3f(size / 2, size / 2, -size / 2);
- glVertex3f(size / 2, size / 2, size / 2);
- glVertex3f(size / 2, -size / 2, size / 2);
- glVertex3f(size / 2, -size / 2, -size / 2);
- //top
- glVertex3f(size / 2, size / 2, size / 2);
- glVertex3f(-size / 2, size / 2, size / 2);
- glVertex3f(-size / 2, size / 2, -size / 2);
- glVertex3f(size / 2, size / 2, -size / 2);
- //bottom
- glVertex3f(size / 2, -size / 2, size / 2);
- glVertex3f(-size / 2, -size / 2, size / 2);
- glVertex3f(-size / 2, -size / 2, -size / 2);
- glVertex3f(size / 2, -size / 2, -size / 2);
- glEnd();
- }
- void head()
- {
- //ROBOT HEAD
- glTranslatef(0, 2, -9);
- if (tooFar == false) //if you haven't gone too far
- {
- yLocation += .01; //rotate at this speed
- glRotatef(yLocation, 0, 1, 0); //in a positive y direction
- if (yLocation >= 90) //if greater than 90 degrees turn around
- {
- tooFar = true;
- }
- }
- if(tooFar == true)
- {
- yLocation -= .01; //subtracting rewinds the motion as long as the y axis isn't inverted
- glRotatef(yLocation, 0, 1, 0); //maintain proper y axis rotation
- if (yLocation <= -90) //if the float yLocation drops below -90 the bool triggers and resets the animation
- {
- tooFar = false;
- }
- }
- glColor3f(0, 0, 0);
- cubey(1.0); //head base
- glTranslatef(.25, 0, .5);
- glColor3f(1, 0, 0);
- cubey(.1); //eye 1
- glTranslatef(-.5, 0, 0);
- glColor3f(1, 0, 0);
- cubey(.1); //eye 2
- //translate works in relation to last location***
- }
- void torso()
- {
- glTranslatef(.25 , -1.5, -.5);
- glColor3f(0, 0, 1);
- cubey(1.5);
- glTranslatef(-1, .6, 0);
- glColor3f(0, 0, 0);
- cubey(.75);
- glTranslatef(1, 0, 0);
- cubey(.75);
- glTranslatef(1, 0, 0);
- cubey(.75);
- glTranslatef(-1, -1, 0);
- cubey(.75);
- glTranslatef(0, -.75, 0);
- cubey(.75);
- //
- glTranslatef(-.5, 0, 0);
- cubey(.75);
- glTranslatef(1, 0, 0);
- cubey(.75);
- glTranslatef(0, -.5, 0);
- cubey(.75);
- glTranslatef(-1, 0, 0);
- cubey(.75);
- glTranslatef(0,-.5,0);
- glColor3f(0, 0, 1);
- cubey(.8);
- glTranslatef(1, 0, 0);
- cubey(.8);
- }
- void init()
- {
- glClearColor(1, 1, 1, 0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //glOrtho(0, 400, 400, 1, -1, 1);
- gluPerspective(45, 640 / 480, 1, 500);
- glMatrixMode(GL_MODELVIEW);
- glEnable(GL_DEPTH_TEST);
- }
- void draw()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix(); //outer push
- glLoadIdentity(); //load identity matrix obj 1
- glPushMatrix(); //inner push obj 1
- head();
- //glPopMatrix(); //inner pop obj 1
- //glLoadIdentity(); //doesn't change anything
- //body disappears.
- //glPushMatrix(); //inner push obj 2
- torso();
- glPopMatrix(); //inner pop obj 2
- glPopMatrix(); //outer pop
- }
- int main(int argc, char *argv[])
- {
- SDL_Window *mainwindow;
- SDL_GLContext maincontext;
- SDL_Init(SDL_INIT_VIDEO);
- mainwindow = SDL_CreateWindow("Custom Robo",
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- 400, 400,
- SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
- maincontext = SDL_GL_CreateContext(mainwindow);
- bool running = true;
- SDL_Event event;
- init();
- while (running)
- {
- draw();
- while (SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_QUIT:
- running = false;
- break;
- case SDL_KEYDOWN:
- switch (event.key.keysym.sym)
- {
- case SDLK_h:
- break;
- case SDLK_q:
- SDL_Quit();
- running = false;
- break;
- }//close key switch
- }//close switch
- }//close inner while
- SDL_GL_SwapWindow(mainwindow);
- }//close outer while
- return 0;
- }//close main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement