this is my 2nd program
fish tank
=======================================
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int i, rad, titik, x_, y_;
const double PI = 3.14;
typedef struct{
float x;
float y;
} object;
typedef struct{
float x,y;
} point2D_t;
typedef struct{
float r;
float g;
float b;
} color;
typedef struct{
float r,g,b;
} color_t;
void setcolor(color col)
{
glColor3f(col.r, col.g, col.b);
}
void setColor(float r,float g,float b)
{
glColor3f(r,g,b);
}
void setColor(color_t col)
{
glColor3f(col.r,col.g,col.b);
}
void drawobject(object obj[], color col[], int n)
{
glBegin(GL_POLYGON);
for(i=0;i<n;i++)
{
setcolor(col[i]);
glVertex2f(obj[i].x,obj[i].y);
}
glEnd();
}
void drawPolyline(point2D_t p[],int n)
{
glColor3f(0,0,0);
glBegin(GL_LINE_STRIP);
for(int i=0;i<n;i++) glVertex2f(p[i].x,p[i].y);
glEnd();
}
void drawPolygon(point2D_t p[],int n)
{
glBegin(GL_LINE_LOOP);
for(int i=0;i<n;i++) glVertex2f(p[i].x,p[i].y);
glEnd();
}
void gradatePolygon(point2D_t p[],color_t color,int n)
{
setColor(color);
glBegin(GL_POLYGON);
for(int i=0;i<n;i++) {
glVertex2f(p[i].x,p[i].y);
}
glEnd();
}
void fillPolygon(point2D_t p[], int n, color_t color)
{
int i;
glBegin(GL_POLYGON);
setColor(color);
for(i=0;i<n;i++)
glVertex2f(p[i].x,p[i].y);
glEnd();
}
static void createCircle(point2D_t p[],float r)
{
for(int i=0;i<360;i++){
p[i].x=r*cos(i/57.3);
p[i].y=r*sin(i/57.3);
}
}
static void createCircle(point2D_t p[], point2D_t p0, float r, int n)
{
float a=6.28/n;
for(int i=0;i<n;i++){
p[i].x=p0.x+r*cos(i*a);
p[i].y=p0.y+r*sin(i*a);
}
}
static void createEllipse(point2D_t p[],float r1,float r2)
{
for(int i=0;i<360;i++){
p[i].x=r1*cos(i/57.3);
p[i].y=r2*sin(i/57.3);
}
}
static void createEllipse(point2D_t p[],point2D_t p0,float r1,float r2)
{
for(int i=0;i<360;i++){
p[i].x=p0.x+r1*cos(i/57.3);
p[i].y=p0.y+r2*sin(i/57.3);
}
}
void drawLine(float x1, float y1, float x2,float y2,int m,color_t col){
setColor(col);
glLineWidth(m);
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}
void write(float x, float y, float z, void *font, char *string)
{
char *c;
glRasterPos3f(x, y,z);
for (c=string; *c != '\0'; c++)
{
glutBitmapCharacter(font, *c);
}
}
void title()
{
glColor3f(0.,0.,0.);
write(-95, 90, 0, GLUT_BITMAP_HELVETICA_12 , "7609040003") ;
}
void drawdot(int x,int y)
{
glPointSize(5);
glColor3f(0,0,0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void bubble()
{
glLineWidth(1);
static point2D_t gel[1];
static int tick=0;
static float d[3];
int i;
if(tick==0)
{
for(i=0;i<1;i++)
{
gel[i].x=rand()%50;
gel[i].y=-100;
d[i]=(float)(rand()%50)+0.5;
}
}
tick++;
for(i=0;i<1;i++)
{
gel[i].y+=d[i];
if(gel[i].y>300)
{
gel[i].x=rand()%50;
gel[i].y=-100;
d[i]=(float)(rand()%100)/100+0.5;
}
}
point2D_t p[90],p0;
setColor(1,1,1);
for(i=0;i<1;i++)
{
createCircle(p,gel[i],2,90);
drawPolygon(p,90);
p0.x = gel[i].x+7;
p0.y = gel[i].y+6;
createCircle(p,p0,2,90);
drawPolygon(p,90);
p0.x = gel[i].x-2;
p0.y = gel[i].y+10;
drawPolygon(p,90);
}
}
void aqua()
{
object objaqua[4]={{-100,-100},{100,-100},{100,100},{-100,100}};
color colaqua[4]={{0,0.3,1},{0.7,0.7,0.9},{0,0.7,0.9},{0,0.7,0.9}};
drawobject(objaqua,colaqua,4);
}
void selang()
{
object objs[4]={{15,-60},{25,-60},{25,100},{15,100}};
color cols[4]={{1,1,1},{0,1,1},{0,0.9,1},{1,1,1},};
drawobject(objs,cols,4);
}
void grass()
{
color_t wrt1={0.0,0.5,0.2};
color_t wrt2={0.0,0.4,0.2};
drawLine(-90,-85,-90,-65,5,wrt1);
drawLine(-90,-85,-88,-66,5,wrt1);
drawLine(-90,-85,-86,-67,5,wrt1);
drawLine(-90,-85,-84,-68,5,wrt1);
drawLine(-90,-85,-92,-66,5,wrt1);
drawLine(-90,-85,-94,-67,5,wrt1);
drawLine(-90,-85,-96,-68,5,wrt1);
drawLine(-80,-90,-80,-70,5,wrt1);
drawLine(-80,-90,-78,-71,5,wrt1);
drawLine(-80,-90,-76,-72,5,wrt1);
drawLine(-80,-90,-74,-73,5,wrt1);
drawLine(-80,-90,-82,-71,5,wrt1);
drawLine(-80,-90,-84,-72,5,wrt1);
drawLine(-80,-90,-86,-73,5,wrt1);
drawLine(-85,-95,-85,-75,5,wrt2);
drawLine(-85,-95,-83,-76,5,wrt2);
drawLine(-85,-95,-81,-77,5,wrt2);
drawLine(-85,-95,-79,-78,5,wrt2);
drawLine(-85,-95,-87,-76,5,wrt2);
drawLine(-85,-95,-89,-77,5,wrt2);
drawLine(-85,-95,-91,-78,5,wrt2);
drawLine(-65,-85,-65,-65,5,wrt2);
drawLine(-65,-85,-63,-66,5,wrt2);
drawLine(-65,-85,-61,-67,5,wrt2);
drawLine(-65,-85,-59,-68,5,wrt2);
drawLine(-65,-85,-67,-66,5,wrt2);
drawLine(-65,-85,-69,-67,5,wrt2);
drawLine(-65,-85,-71,-68,5,wrt2);
}
void batu()
{
object objb1[8]={{-20,-87},{-18,-90},{-15,-89},{-10,-86},{-11,-85},{-13,-84},{-16,-85},{-18,-85}};
color colb1[8]={{0.1,0.2,0.2},{0.1,0.2,0.2},{0.3,0.1,0.2},{0.4,0.3,0.3},{0,0,0.2},{0.3,0.2,0.2},{0.3,0.2,0.2},{0.3,0.2,0.2},};
drawobject(objb1,colb1,8);
object objb2[8]={{-60,-87},{-58,-90},{-55,-89},{-50,-86},{-51,-85},{-53,-84},{-56,-85},{-58,-85}};
color colb2[8]={{0.1,0.2,0.2},{0.1,0.2,0.2},{0.3,0.1,0.2},{0.4,0.3,0.3},{0,0,0.2},{0.3,0.2,0.2},{0.3,0.2,0.2},{0.3,0.2,0.2},};
drawobject(objb2,colb2,8);
object objb3[8]={{-61,-89},{-59,-92},{-56,-91},{-51,-88},{-52,-87},{-54,-86},{-57,-87},{-59,-87}};
color colb3[8]={{0.1,0.2,0.2},{0.1,0.2,0.2},{0.3,0.1,0.2},{0.4,0.3,0.3},{0,0,0.2},{0.3,0.2,0.2},{0.3,0.2,0.2},{0.3,0.2,0.2},};
drawobject(objb3,colb3,8);
object objb4[8]={{-48,-87},{-46,-90},{-43,-89},{-38,-86},{-39,-85},{-41,-84},{-44,-85},{-46,-85}};
color colb4[8]={{0.1,0.2,0.2},{0.1,0.2,0.2},{0.3,0.1,0.2},{0.4,0.3,0.3},{0,0,0.2},{0.3,0.2,0.2},{0.3,0.2,0.2},{0.3,0.2,0.2},};
drawobject(objb4,colb4,8);
object objb5[8]={{-75,-89},{-73,-92},{-70,-91},{-65,-88},{-66,-87},{-69,-86},{-72,-87},{-74,-87}};
color colb5[8]={{0.1,0.2,0.2},{0.1,0.2,0.2},{0.3,0.1,0.2},{0.4,0.3,0.3},{0,0,0.2},{0.3,0.2,0.2},{0.3,0.2,0.2},{0.3,0.2,0.2},};
drawobject(objb5,colb5,8);
}
void ikan()
{
color_t kan ={1,0.4,0.2};
color_t kon ={0.8,0.3,0.2};
color_t kun ={1,0.9,0};
object objik2[11]={{-80,-10},{-75,-13},{-70,3},{-67,12},{-69,25},{-70,35},{-77,40},{-84,35},{-86,16},{-87,13},{-87,3}};
color colik2[11]={{0,0,0},{0,0,0.3},{0,0,0.3},{0,0,0.3},{0,0,0.3},{0,0,0},{0,0,0},{0,0,0.3},{0,0,0.3},{0,0,0.3},{0,0,0.3},};
drawobject(objik2,colik2,11);
object objik1[7]={{-75,-45},{-74,-40},{-74,-35},{-75,-10},{-80,-10},{-76,-36},{-76,-40}};
color colik1[7]={{0,0,0},{0,0,0.3},{0,0,0.3},{0,0,0.3},{0,0,0.3},{0,0,0},{0,0,0}};
drawobject(objik1,colik1,7);
object objik3[3]={{-80,-47},{-75,-45},{-75,-32}};
color colik3[3]={{0,0,0},{0,0,0.3},{0,0,0.3}};
drawobject(objik3,colik3,3);
object objik4[3]={{-75,-45},{-70,-47},{-75,-32}};
color colik4[3]={{0,0,0},{0,0,0.3},{0,0,0.3}};
drawobject(objik4,colik4,3);
point2D_t ikan1[360],ikann1={50,50};
createEllipse(ikan1,ikann1,15,7);
object objk1[4]={{60,50},{70,40},{67,50},{70,60}};
object objsa1[3]={{45,55},{55,55},{60,60}};
object objsb1[3]={{55,40},{53,46},{50,46}};
color colk1[4]={{1,0.4,0.2},{0,0.4,0.2},{1,0.4,0.2},{1,0.4,0.2}};
color colsa1[3]={{1,0.4,0.2},{0,0.4,0.2},{1,0.4,0.2}};
color colsb1[3]={{1,0.4,0.2},{0,0.4,0.2},{1,0.4,0.2}};
drawobject(objk1,colk1,4);
drawobject(objsa1,colsa1,3);
drawobject(objsb1,colsb1,3);
fillPolygon(ikan1,360,kan);
drawdot(40,50);
point2D_t ikan2[360],ikann2={80,-70};
createEllipse(ikan2,ikann2,15,7);
object objk2[4]={{63,-70},{60,-80},{70,-70},{60,-60}};
object objsa2[3]={{75,-65},{85,-65},{70,-60}};
object objsb2[3]={{75,-80},{78,-74},{70,-74}};
color colk2[4]={{1,0.4,0.2},{0,0.4,0.2},{1,0.4,0.2},{1,0.4,0.2}};
color colsa2[3]={{1,0.4,0.2},{0,0.4,0.2},{1,0.4,0.2}};
color colsb2[3]={{1,0.4,0.2},{0,0.4,0.2},{1,0.4,0.2}};
drawobject(objk2,colk2,4);
drawobject(objsa2,colsa2,3);
drawobject(objsb2,colsb2,3);
fillPolygon(ikan2,360,kan);
drawdot(90,-70);
point2D_t ikan3[360],ikann3={60,-60};
createEllipse(ikan3,ikann3,15,7);
object objk3[4]={{43,-60},{40,-70},{50,-60},{40,-50}};
object objsa3[3]={{55,-55},{65,-55},{50,-50}};
object objsb3[3]={{55,-70},{58,-64},{50,-64}};
color colk3[4]={{0.8,0.3,0.2},{0.8,0,0.5},{0.8,0.3,0.2},{0.8,0.3,0.2}};
color colsa3[3]={{0.8,0.3,0.2},{0.8,0,0.5},{0.8,0.3,0.2}};
color colsb3[3]={{0.8,0.3,0.2},{0.8,0,0.5},{0.8,0.3,0.2}};
drawobject(objk3,colk3,4);
drawobject(objsa3,colsa3,3);
drawobject(objsb3,colsb3,3);
fillPolygon(ikan3,360,kon);
drawdot(70,-60);
point2D_t ikan4[360],ikann4={-40,-60};
createEllipse(ikan4,ikann4,15,7);
object objk4[4]={{-30,-60},{-20,-70},{-23,-60},{-20,-50}};
object objsa4[3]={{-45,-55},{-35,-55},{-30,-50}};
object objsb4[3]={{-35,-70},{-37,-64},{-40,-64}};
color colk4[4]={{0.8,0.3,0.2},{0.5,0.7,0.2},{0.8,0.3,0.2},{0.8,0.3,0.2}};
color colsa4[3]={{0.8,0.3,0.2},{0.5,0.7,0.2},{0.8,0.3,0.2}};
color colsb4[3]={{0.8,0.3,0.2},{0.5,0.7,0.2},{0.8,0.3,0.2}};
drawobject(objk4,colk4,4);
drawobject(objsa4,colsa4,3);
drawobject(objsb4,colsb4,3);
fillPolygon(ikan4,360,kon);
drawdot(-50,-60);
point2D_t ikan5[360],ikann5={-30,70};
createEllipse(ikan5,ikann5,15,7);
object objk5[4]={{-47,70},{-50,60},{-40,70},{-50,80}};
object objsa5[3]={{-35,75},{-25,75},{-40,80}};
object objsb5[3]={{-35,60},{-32,66},{-40,66}};
color colk5[4]={{1,0.9,0.2},{0,0.4,0.2},{1,0.9,0.2},{1,0.9,0.2}};
color colsa5[3]={{1,0.9,0.2},{0,0.4,0.2},{1,0.9,0.2}};
color colsb5[3]={{1,0.9,0.2},{0,0.4,0.2},{1,0.9,0.2}};
drawobject(objk5,colk5,4);
drawobject(objsa5,colsa5,3);
drawobject(objsb5,colsb5,3);
fillPolygon(ikan5,360,kun);
drawdot(-20,70);
}
void tanah()
{
object objt[6]={{-100,-100},{100,-100},{100,-72},{-100,-80},{-100,-100},{-100,-100},};
color colt[6]={{0.5,0.2,1},{0.5,0.2,1},{0.5,1,1},{0,0.2,1},{0.5,0.2,0},{1,0.9,1},};
drawobject(objt,colt,6);
}
void kartesius()
{
glLineWidth(1);
glColor3f(0,0,0);
for(float x=-100;x<=100;x+=10)
{
glBegin(GL_LINES);
glVertex2f(x,-100);
glVertex2f(x,100);
glEnd();
glBegin(GL_LINES);
glVertex2f(-100,x);
glVertex2f(100,x);
glEnd();
}
glColor3f(0,1,1);
glBegin(GL_LINES);
glVertex2f(100,0);
glVertex2f(-100,0);
glEnd();
glBegin(GL_LINES);
glVertex2f(0,-100);
glVertex2f(0,100);
glEnd();
}
void display()
{
aqua();
tanah();
grass();
batu();
selang();
ikan();
bubble();
//kartesius();
title();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutCreateWindow("7609040003");
glClearColor(0,0,0,1);
gluOrtho2D(-100.0,100.0,-100.0,100.0);
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
here it is..
thanks.
No comments:
Post a Comment