You are on page 1of 18

Render.

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
//#include <GL/gl.h>
//#include <GL/glut.h>
#include <OpenGl/glu.h>
#include <OpenGl/gl.h>
#include <GLUT/GLUT.h>
#include "render.h"
#include "shape.h"

//angle of rotation
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0, alpha, beta, a, inc2;
float Autorun = 15;
int inc;

int animationState;
int ANIMATION_RUNNING = 1;
int ANIMATION_STOPPED = 0;

void initVariable() {

alpha = -(float) 20.0;


beta = (float) 20.0;
a = -(float) 5.0;

inc = 1;
inc2 = 0.1;

animationState = ANIMATION_RUNNING;
}

void camera (void) {

glRotatef(xrot,0.5,0.0,0.0); //rotate our camera on the x-axis (left and right)


glRotatef(yrot,0.0,0.5,0.0); //rotate our camera on the y-axis (up and down)
glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera

}
void bubble1() {

glPushMatrix();

a = a + (float) inc2;
if(a > (float) 20.0)
a = -(float) 5.0;

glTranslatef(-5, a, 5);
glColor3f(0,0, 1);
glutSolidSphere(0.25,20,20);
glPopMatrix();
}

void bubble2() {

glPushMatrix();

if(a > (float) 20.0)


a = -(float) 5.0;

glTranslatef(-3, a, -5);
glColor3f(0,0, 1);
glutSolidSphere(0.25,20,20);
glPopMatrix();
}
void bubble3() {

glPushMatrix();

if(a > (float) 20.0)


a = -(float) 5.0;

glTranslatef(0, a, 0);
glColor3f(0,0, 1);
glutSolidSphere(0.25,20,20);
glPopMatrix();
}

void bubble4() {
glPushMatrix();

if(a > (float) 20.0)


a = -(float) 5.0;

glTranslatef(3, a, -5);
glColor3f(0,0, 1);
glutSolidSphere(0.25,20,20);
glPopMatrix();
}

void drawBubbles() {

bubble1();
bubble2();
bubble3();
bubble4();
}

void drawGround() {

glPushMatrix();
glColor3f( 0.137255, 0.556863, 0.419608);
glTranslatef(0,-5,0);
glBegin(GL_QUADS);

glVertex3f(-8.0,0,8.0);
glVertex3f(8,0,8.0);
glVertex3f(8.0,0,-8.0);
glVertex3f(-8.0,0,-8.0);

glEnd();
glPopMatrix();
}

void drawPyramid () {

glPushMatrix();

glTranslatef(-4, -2, 0);


glScaled(3, 3, 3);
glBegin(GL_TRIANGLES); // Begin drawing the pyramid with 4 triangles

// Front
glColor3f(0.81, 0.71, 0.23);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.81, 0.71, 0.23);
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.81, 0.71, 0.23);
glVertex3f(1.0f, -1.0f, 1.0f);

// Right
glColor3f(0.81, 0.71, 0.23);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.81, 0.71, 0.23);
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.81, 0.71, 0.23);
glVertex3f(1.0f, -1.0f, -1.0f);

// Back
glColor3f(0.81, 0.71, 0.23);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.81, 0.71, 0.23);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.81, 0.71, 0.23);
glVertex3f(-1.0f, -1.0f, -1.0f);

// Left
glColor3f(0.81, 0.71, 0.23);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.81, 0.71, 0.23);
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.81, 0.71, 0.23);
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd();

glPopMatrix();

void drawChest() {

glPushMatrix();

glColor3f(0.55, 0.47, 0.14);


glTranslatef(3, -4.4, 0);
glutSolidCube(1.0f);

glPopMatrix();
}

void drawHuman() {

glPushMatrix();

Autorun = Autorun + (float) inc2;

if(Autorun > (float) 20.0)


Autorun -= (float) 30.0;

glTranslated(-Autorun, 0, 0);

glPushMatrix();
glScaled(0.5, 0.5, 0.5); //Scaling human

glPushMatrix(); //Bubble
glTranslatef(-1.5, a + 6 , 1.7);
glColor3f(0,0, 1);
glutSolidSphere(0.25,20,20);
glPopMatrix();

glPushMatrix(); //Bubble
glTranslatef(-1, a + 8 , 1.7);
glColor3f(0,0, 1);
glutSolidSphere(0.25,20,20);
glPopMatrix();

glPushMatrix(); //Bubble
glTranslatef(-0.5, a + 10 , 1.7);
glColor3f(0,0, 1);
glutSolidSphere(0.25,20,20);
glPopMatrix();

glPushMatrix(); //Draw head


glScaled(0.5, 0.5, 0.5);
glTranslatef(-2, 2, 1.7);
glColor3f(1,1, 1);
createcircle(0.25,1,0.25);
glPopMatrix();
glBegin(GL_LINES); //Draw pipe
glVertex3f(-1, 1, 1);
glVertex3f( -0.5, 2, 1);
glEnd();

glBegin(GL_LINES); //Draw body


glVertex3f(1, 1, 1);
glVertex3f( -0.5, 1, 1);
glVertex3f(1, 1, 1);
glVertex3f( 2.5, 1.2, 1);
glVertex3f(1, 1, 1);
glVertex3f( 2.5, 0, 1);
glEnd();

glPushMatrix();
glTranslatef(2,2.5,1);
glScaled(-0.5, -0.5, -0.5);
drawOval( 0, 1, 4, 2);
drawOval( 1, 1, 4, 2);
glPopMatrix();

glPopMatrix(); //Pop scaling


glPopMatrix();
}

void drawFlower() {

glPushMatrix();
glTranslatef( 6, -2, 5);

glPushMatrix(); //Animation start

glPushMatrix(); //Bubble
glTranslatef(-0.5, a + 6 , 1.7);
glColor3f(0,0, 1);
glutSolidSphere(0.25,20,20);
glPopMatrix();

glPushMatrix(); //Bubble
glTranslatef(1, a + 8 , 1.7);
glColor3f(0,0, 1);
glutSolidSphere(0.25,20,20);
glPopMatrix();

glPushMatrix(); //Bubble
glTranslatef(1.5, a + 10 , 1.7);
glColor3f(0,0, 1);
glutSolidSphere(0.25,20,20);
glPopMatrix();

alpha = alpha + (float) inc;

if(alpha > (float) 360.0)


alpha -= (float) 360.0;

glRotatef(alpha, (float) 0.0, (float) 0.0, (float) 1.0);

glPushMatrix(); //Paddle 1
glScaled(-0.5, -0.5, -0.5);
glTranslatef( -1, -2, -0.25);
drawOval( 1, 1, 4, 2);
glPopMatrix();

glPushMatrix(); //Paddle 2
glScaled(-0.5, -0.5, -0.5);
glRotatef(90, 0, 0,1 );
glTranslatef( -1, -2, -0.25);
drawOval( 1, 1, 4, 2);
glPopMatrix();

glPushMatrix(); //Paddle 3
glScaled(-0.5, -0.5, -0.5);
glRotatef(180, 0, 0,1 );
glTranslatef( -1, -2, -0.25);
drawOval( 1, 1, 4, 2);
glPopMatrix();

glPushMatrix(); //Paddle 4
glScaled(-0.5, -0.5, -0.5);
glRotatef(270, 0, 0,1 );
glTranslatef( -1, -2, -0.25);
drawOval( 1, 1, 4, 2);
glPopMatrix();

glPushMatrix(); //Draw middle


glColor3f(0,1,1);
glutSolidSphere(0.25,20,20);
glPopMatrix();

glPopMatrix();//Animation pop

glPushMatrix(); //Draw stick


glColor3f(0.91, 0.76, 0.65);
glRotatef(90, 1, 0, 0 );
drawCylinder( 2, 5 );
glPopMatrix();

glPopMatrix();

void drawTurtle() {

glPushMatrix();
glTranslatef(0,-5,-4);
beta = beta + (float) inc2;

if(beta > (float) 20.0)


beta -= (float) 30.0;

glTranslated(0, 0, beta);

glPushMatrix(); //Shell
glScaled(-0.5, -0.5, -0.5);
glRotatef(89, 1, 0, 0 );
glTranslatef(-6,0,1);
drawOval( 1, 1, 5, 7);
glPopMatrix();

glPushMatrix();
glColor3f(0.71, 0.65, 0.26);
glBegin(GL_LINES); //Draw Lines on shell
glVertex3f(0.8, 0.5, -1);
glVertex3f( 2.3, 0.5, -1);
glVertex3f(0.5, 0.5, -1.5);
glVertex3f( 2.5, 0.5, -1.5);
glVertex3f(0.5, 0.5, -2);
glVertex3f( 2.5, 0.5, -2);
glVertex3f(0.5, 0.5, -2.5);
glVertex3f( 2.5, 0.5, -2.5);
glVertex3f(0.8, 0.5, -3);
glVertex3f( 2.3, 0.5, -3);
glVertex3f(2, 0.5, -0.8);
glVertex3f( 2, 0.5, -3.3);
glVertex3f(1.5, 0.5, -0.5);
glVertex3f( 1.5, 0.5, -3.5);
glVertex3f(1, 0.5, -0.8);
glVertex3f( 1, 0.5, -3.3);
glEnd();
glPopMatrix();

glPushMatrix(); //Head
glColor3f(0,1,0);
glTranslatef(1.5,0.75,-0.5);
glutSolidSphere(0.4,20,20);
glPopMatrix();

//Four legs
glPushMatrix();
glColor3f(0.71, 0.65, 0.26);
glTranslatef(2,0.25,-1);
glutSolidSphere(0.25,20,20);
glPopMatrix();

glPushMatrix();
glColor3f(0.71, 0.65, 0.26);
glTranslatef(2,0.25,-3);
glutSolidSphere(0.25,20,20);
glPopMatrix();

glPushMatrix();
glColor3f(0.71, 0.65, 0.26);
glTranslatef(1,0.25,-3);
glutSolidSphere(0.25,20,20);
glPopMatrix();

glPushMatrix();
glColor3f(0.71, 0.65, 0.26);
glTranslatef(1,0.25,-1);
glutSolidSphere(0.25,20,20);
glPopMatrix();

glPopMatrix();
}

void fastenAnimation() {

inc += 1;
inc2 += 0.03;
}

void slowAnimation() {

inc -= 1;
inc2 -= 0.03;

void stopAnimation() {

inc = 0;
inc2 = 0;
}

void resumeAnimation() {
inc = 1;
inc2 = 0.1;
}

void input(unsigned char key, int x, int y) {

switch (key) {
case '`':
exit(0);
break;
case 't':
if( animationState == ANIMATION_RUNNING ) {
stopAnimation();
animationState = ANIMATION_STOPPED;
}
break;
case 'c':
if( animationState == ANIMATION_STOPPED ) {
resumeAnimation();
animationState = ANIMATION_RUNNING;
}
break;
case 'f':
fastenAnimation();
break;
case 's':
slowAnimation();
break;
case 'x':
xrot -= 0.1; //Rotate x
break;
case 'z':
gluLookAt(0, 0, 0.1, 0, 0, -1, 0, 1, 0);
break;
case 'Z':
gluLookAt(0, 0, -0.1, 0, 0, -1, 0, 1, 0);
break;
case 'y':
yrot -= 0.1; //Rotate y
break;
case 'p':
glShadeModel(GL_FLAT);
break;
case 'P':
glShadeModel(GL_SMOOTH);
break;
case 'a':
if( animationState == ANIMATION_RUNNING ) {
stopAnimation();
animationState = ANIMATION_STOPPED;
} else if( animationState == ANIMATION_STOPPED ) {
resumeAnimation();
animationState = ANIMATION_RUNNING;
}
break;
default:
break;
}

void output( char *array, float yAxis ) {

int i, len;
float xAxis = 5.0;
glColor3f( 1, 1, 1 );
glRasterPos2f( xAxis, yAxis );
len = strlen(array);

for (i = 0; i < len; i++) {


glutBitmapCharacter(GLUT_BITMAP_8_BY_13, array[i]);
}

void prepareOutput() {

float yAxis = 7;
char zoomIn[12] = {'<', 'Z', '>', '-', 'Z', 'o', 'o', 'm' , ' ', 'i', 'n', '\0'};
output( zoomIn, yAxis );
yAxis-= 0.5;

char zoomOut[13] = {'<', 'z', '>', '-', 'Z', 'o', 'o', 'm' , ' ', 'o', 'u', 't', '\0'};
output( zoomOut, yAxis);
yAxis-= 0.5;

char rotateX[13] = {'<', 'x', '>', '-', 'R', 'o', 't', 'a' , 't', 'e', ' ', 'X', '\0'};
output( rotateX, yAxis );
yAxis-= 0.5;

char rotateY[13] = {'<', 'y', '>', '-', 'R', 'o', 't', 'a' , 't', 'e', ' ', 'Y', '\0'};
output( rotateY, yAxis );
yAxis-= 0.5;

char predefinedAnimation[26] = {'<', 'a', '>', '-', 'P', 'r', 'e', '-' , 'd', 'e', 'f', 'i', 'n', 'e', 'd', ' ', 'A', 'n',
'i', 'm', 'a', 't', 'i' , 'o', 'n', '\0'};
output( predefinedAnimation, yAxis );
yAxis-= 0.5;

char fasterAnimation[21] = {'<', 'f', '>', '-', 'F', 'a', 's', 't', 'e', 'r', ' ', 'A', 'n', 'i', 'm', 'a', 't', 'i' , 'o', 'n',
'\0'};
output( fasterAnimation, yAxis );
yAxis-= 0.5;

char slowerAnimation[21] = {'<', 'f', '>', '-', 'S', 'l', 'o', 'w', 'e', 'r', ' ', 'A', 'n', 'i', 'm', 'a', 't', 'i' , 'o', 'n',
'\0'};
output( slowerAnimation, yAxis);
yAxis-= 0.5;
char pauseAnimation[20] = {'<', 't', '>', '-', 'P', 'a', 'u', 's', 'e', ' ', 'A', 'n', 'i', 'm', 'a', 't', 'i' , 'o', 'n', '\
0'};
output( pauseAnimation, yAxis );
yAxis-= 0.5;

char resumeAnimation[21] = {'<', 'c', '>', '-', 'R', 'e', 's', 'u', 'm', 'e', ' ', 'A', 'n', 'i', 'm', 'a', 't', 'i' , 'o',
'n', '\0'};
output( resumeAnimation, yAxis );
yAxis-= 0.5;

char renderFlat[30] = {'<', 'p', '>', '-', 'R', 'e', 'n', 'd', 'e', 'r', ' ', 'F', 'l', 'a', 't', ' ', 'S', 'h' , 'a', 'd', 'e',
'd', '\0'};
output( renderFlat, yAxis );
yAxis-= 0.5;

char renderSmooth[30] = {'<', 'P', '>', '-', 'R', 'e', 'n', 'd', 'e', 'r', ' ', 'F', 'l', 'a', 't', ' ', 'S', 'm' , 'o', 'o',
't', 'h', '\0'};
output( renderSmooth, yAxis );
yAxis-= 0.5;

Render.h

#include <stdio.h>

void initVariable();
void camera (void);
void bubble1();
void bubble2();
void bubble3();
void bubble4();
void drawBubbles();
void drawGround();
void drawPyramid();
void drawChest();
void drawHuman();
void drawFlower();
void drawTurtle();
void fastenAnimation();
void slowAnimation();
void stopAnimation();
void resumeAnimation();
void input(unsigned char key, int x, int y);
void output( char *array, float yAxis );
void prepareOutput();

Shape.h

#include <stdio.h>

void createcircle (int k, int r, int h);


void drawOval(int x, int y, int width, int height);
void drawCylinder(float radius, float height);

Shape.c

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
//#include <GL/gl.h>
//#include <GL/glut.h>
#include <OpenGl/glu.h>
#include <OpenGl/gl.h>
#include <GLUT/GLUT.h>
#include "shape.h"

typedef struct
{
float x;
float y;
}CIRCLE;

CIRCLE circle;

void createcircle (int k, int r, int h) {

int i;
glBegin(GL_LINES);
for ( i = 0; i < 180; i++)
{
circle.x = r * cos(i) - h;
circle.y = r * sin(i) + k;
glVertex3f(circle.x + k,circle.y - h,0);
circle.x = r * cos(i + 0.1) - h;
circle.y = r * sin(i + 0.1) + k;
glVertex3f(circle.x + k,circle.y - h,0);
}
glEnd();
}

void drawOval(int x, int y, int width, int height) {

float theta;
float angle_increment;
float x1;
float y1;
float PI = 3.14;

angle_increment = (float) PI / 500;


glPushMatrix();

glTranslatef(x+(width/2), y+(height/2), 0);

glColor3f(1.0,1.0,0.6);

glBegin(GL_POLYGON);
for(theta = 0.0f; theta < 2 * PI; theta += angle_increment) {
x1 = (float) (width/2 * cos(theta));
y1 = (float) (height/2 * sin(theta));

glVertex2f(x1, y1);
}
glEnd();

glPopMatrix();
}

void drawCylinder(float radius, float height) {

GLUquadricObj *quadratic;
quadratic = gluNewQuadric();

gluCylinder(quadratic,0.1f,0.1f,3.0f,32,32);

}
Main.c
include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
//#include <GL/gl.h>
//#include <GL/glut.h>
#include <OpenGl/glu.h>
#include <OpenGl/gl.h>
#include <GLUT/GLUT.h>
#include "main.h"
#include "shape.h"
#include "render.h"

#define WINDOW_WIDTH 800


#define WINDOW_HEIGHT 800

void init() {
// Create light components
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };

glEnable (GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);

// Assign created components to GL_LIGHT0


glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);

initVariable();
gluLookAt(0, 0, 20, 0, 0, -1, 0, 1, 0);

void display(void) {
/* clear the buffer of all data */
glClearColor (0.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

camera();

prepareOutput();
drawHuman();
drawFlower();
drawTurtle();
drawBubbles();
drawGround();
drawPyramid();
drawChest();

glFlush();
glutSwapBuffers();

void reshape(int width, int height) {


glViewport(0, 0, (GLsizei) width, (GLsizei) height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);
}

void idle(void) {
glutPostRedisplay();
}

int main(int argc, char **argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(400, 400);

glutCreateWindow("CG252 Under The Sea");


init();

glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc(input);
glutIdleFunc(idle);

glutMainLoop();
return 0;
}

Main.h

#include <stdio.h>

void init();
void display(void);
void reshape(int width, int height);
void idle(void);
int main(int argc, char **argv);

You might also like