Membangun Star
January 17, 2012 Leave a comment
/* Copyright (c) Mark J. Kilgard, 1994. */ /** * (c) Copyright 1993, Silicon Graphics, Inc. * ALL RIGHTS RESERVED * Permission to use, copy, modify, and distribute this software for * any purpose and without fee is hereby granted, provided that the above * copyright notice appear in all copies and that both the copyright notice * and this permission notice appear in supporting documentation, and that * the name of Silicon Graphics, Inc. not be used in advertising * or publicity pertaining to distribution of the software without specific, * written prior permission. * * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. * * US Government Users Restricted Rights * Use, duplication, or disclosure by the Government is subject to * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph * (c)(1)(ii) of the Rights in Technical Data and Computer Software * clause at DFARS 252.227-7013 and/or in similar or successor * clauses in the FAR or the DOD or NASA FAR Supplement. * Unpublished-- rights reserved under the copyright laws of the * United States. Contractor/manufacturer is Silicon Graphics, * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. * * OpenGL(TM) is a trademark of Silicon Graphics, Inc. */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> #include <GL/glut.h> //pre processor directive #undef PI /* Beberapa sistem mendefinisikan ini. */ #define PI 3.141592657 //deklarasi konstanta phi enum //mode enumerasi mode bintang { NORMAL = 0, WEIRD = 1 }; enum { STREAK = 0, CIRCLE = 1 }; #define MAXSTARS 400 #define MAXPOS 10000 #define MAXWARP 10 #define MAXANGLES 6000 typedef struct _starRec //struct bintang { GLint type; //deklarasi tipe type >> integer openGL float x[2], y[2], z[2]; //deklarasi koordinat float offsetX, offsetY, offsetR, rotation; //deklarasi offset sama rotasi }starRec; GLenum doubleBuffer; GLint windW = 300, windH = 300; //deklarasi ukuran size window GLenum flag = NORMAL; //deklarasi flag awal >> Normal GLint starCount = MAXSTARS / 2; //set jumlah bintang awal=Maxstar/2 float speed = 1.0; //set kecepatan bintang >>1 GLint nitro = 0; //set nitro tidak aktif starRec stars[MAXSTARS]; //deklarasi variable stars sebanyak maxstar dengan tipe data struct float sinTable[MAXANGLES]; //deklarasi kamus sudut dalam sinus float Sin(float angle) //fungsi sinus { return (sinTable[(GLint) angle]); } float Cos(float angle) //fungsi cosinus { return (sinTable[((GLint) angle + (MAXANGLES / 4)) % MAXANGLES]); } void NewStar(GLint n, GLint d) //fungsi bintang baru sebanyak n, dengan jarak kemajuan d { if (rand() % 4 == 0) { //tipe bintang ditentukan oleh bilangan random stars[n].type = CIRCLE; //bila dimodulo 4 == 0, hasilnya circle, kalo ngga } else //streak { stars[n].type = STREAK; } stars[n].x[0] = (float) (rand() % MAXPOS - MAXPOS / 2); //posisi x awal yg baru stars[n].y[0] = (float) (rand() % MAXPOS - MAXPOS / 2); //posisi y awal yg baru stars[n].z[0] = (float) (rand() % MAXPOS + d); //posisi z awal yg baru stars[n].x[1] = stars[n].x[0]; //copy dari x[0] ke x[1] stars[n].y[1] = stars[n].y[0]; //copy dari y[0] ke y[1] stars[n].z[1] = stars[n].z[0]; //copy dari z[0] ke z[1] if (rand() % 4 == 0 && flag == WEIRD) { //kalau tipenya circle dan modenya stars[n].offsetX = (float) (rand() % 100 - 100 / 2); //weird, maka ditambahkan offset stars[n].offsetY = (float) (rand() % 100 - 100 / 2); stars[n].offsetR = (float) (rand() % 25 - 25 / 2); } else { stars[n].offsetX = 0.0; //kalau tipenya streak dan modenya normal stars[n].offsetY = 0.0; //tidak ada offset stars[n].offsetR = 0.0; } } void RotatePoint(float *x, float *y, float rotation) //fungsi rotate dengan derajat { //sebesar rotation float tmpX, tmpY; tmpX = *x * Cos(rotation) - *y * Sin(rotation); //*x baru = x*cos(teta) - y*sin(teta) tmpY = *y * Cos(rotation) + *x * Sin(rotation); //*y baru = y*cos(teta) + x*sin(teta) *x = tmpX; *y = tmpY; } void MoveStars(void) //fungsi pergerakan bintang { float offset; //offset >> speed maju alias di sumbu z GLint n; //n= jumlh bintang offset = speed * 60.0; for (n = 0; n < starCount; n++) { //untuk setiap bintang stars[n].x[1] = stars[n].x[0]; //x1=x0 stars[n].y[1] = stars[n].y[0]; //x1=x0 stars[n].z[1] = stars[n].z[0]; //x1=x0 stars[n].x[0] += stars[n].offsetX; //x0=x0+offsetX stars[n].y[0] += stars[n].offsetY; //y0=y0+offsetY stars[n].z[0] -= offset; //z0=z0-offset >> bintang maju stars[n].rotation += stars[n].offsetR; //sudut=sudut+offsetR if (stars[n].rotation > MAXANGLES) { //kalau sudut > maxAngle, stars[n].rotation = 0.0; //sudut diset 0 } } } GLenum StarPoint(GLint n) //fungsi kemunculan bintang pada bidang gambar { float x0, y0; x0 = stars[n].x[0] * windW / stars[n].z[0]; // x0= x0*windowsize/z y0 = stars[n].y[0] * windH / stars[n].z[0]; // y0= y0*windowsize/z RotatePoint(&x0, &y0, stars[n].rotation); // bintang dirotate x0 += windW / 2.0; // x0 = x0+(width/2) y0 += windH / 2.0; // y0 = yo+(width/2) if (x0 >= 0.0 && x0 < windW && y0 >= 0.0 && y0 < windH) { //kalau x0>0,x0<width,y0>0,y0<height return GL_TRUE; //true >> bintang muncul } else { return GL_FALSE; //kalau ngga bintang mati } } void ShowStar(GLint n) { float x0, y0, x1, y1, width; GLint i; x0 = stars[n].x[0] * windW / stars[n].z[0]; //line 178-190 sama y0 = stars[n].y[0] * windH / stars[n].z[0]; //kaya starPoint(GLint n) RotatePoint(&x0, &y0, stars[n].rotation); x0 += windW / 2.0; y0 += windH / 2.0; if (x0 >= 0.0 && x0 < windW && y0 >= 0.0 && y0 < windH) { //kalau bintang muncul if (stars[n].type == STREAK) { //mode bintang streak x1 = stars[n].x[1] * windW / stars[n].z[1]; //posisi relatif x terhadap z y1 = stars[n].y[1] * windH / stars[n].z[1]; //posisi relatif y terhadap z RotatePoint(&x1, &y1, stars[n].rotation); //rotate sudut x1 += windW / 2.0; //x=x+(width/2) y1 += windH / 2.0; //y=y+(height/2) glLineWidth(MAXPOS / 100.0 / stars[n].z[0] + 1.0); //bikin garis sebesar (JumlahPosisiMax/100)/z bintang ke n + 1 glColor3f(1.0, (MAXWARP - speed) / MAXWARP, (MAXWARP - speed) / MAXWARP); //warna=merah+(derajat kuning=warp-speed)+(derajat hijau=warp-speed/warp) if (fabs(x0 - x1) < 1.0 && fabs(y0 - y1) < 1.0) { //kalau |x0-x1|<1,|y0-y1|<1 glBegin(GL_POINTS); //gambar titik di (x0,y0) glVertex2f(x0, y0); glEnd(); } else { glBegin(GL_LINES); //kalau tidak, gambar garis glVertex2f(x0, y0); //yang menghubungkan glVertex2f(x1, y1); //(x0,y0),(x1,y1) glEnd(); } } else { //kalau tipenya normal width = MAXPOS / 10.0 / stars[n].z[0] + 1.0; //lebar=PosisiMax/10/posisi z star ke n+1 glColor3f(1.0, 0.0, 0.0); //warna=merah glBegin(GL_POLYGON); //gambar poligon for (i = 0; i < 8; i++) //buat 8 poligon di x,y { //dengan ketentuan float x = x0 + width * Cos((float) i * MAXANGLES / 8.0); //x=x0+lebar*cos(i*MaxAngle/8) float y = y0 + width * Sin((float) i * MAXANGLES / 8.0); //y=y0+lebar*sin(i*MaxAngle/8) glVertex2f(x, y); }; glEnd(); } } } void UpdateStars(void) //fungsi update star { GLint n; //n=banyaknya bintang glClear(GL_COLOR_BUFFER_BIT); for (n = 0; n < starCount; n++) { //setiap n smpai jumlah bintang if (stars[n].z[0] > speed || (stars[n].z[0] > 0.0 && speed < MAXWARP)) { if (StarPoint(n) == GL_FALSE) { NewStar(n, MAXPOS); //kalau starPoint mati, posisi z > speed atau z>0,speed<maxwarp } //buat bintang baru sebanyak n } else { NewStar(n, MAXPOS); //kalau starPoint nyala, buat bintang baru sebanyak n } } } void ShowStars(void) { GLint n; glClear(GL_COLOR_BUFFER_BIT); for (n = 0; n < starCount; n++) { if (stars[n].z[0] > speed || (stars[n].z[0] > 0.0 && speed < MAXWARP)) { ShowStar(n); //untuk setiap bintang, bila z>speed atau z>0,speed<maxwarp } //jalankan fungsi showstar } } static void Init(void) { float angle; GLint n; srand((unsigned int) time(NULL)); for (n = 0; n < MAXSTARS; n++) { NewStar(n, 100); } angle = 0.0; for (n = 0; n <= MAXANGLES; n++) { sinTable[n] = sin(angle); angle += PI / (MAXANGLES / 2.0); } glClearColor(0.0, 0.0, 0.0, 0.0); glDisable(GL_DITHER); } void Reshape(int width, int height) { windW = width; windH = height; glViewport(0, 0, windW, windH); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-0.5, windW + 0.5, -0.5, windH + 0.5); glMatrixMode(GL_MODELVIEW); } void pressKey(unsigned char key, int x, int y) { // Fungsi ini akan dijalankan saat tombol keyboard ditekan dan belum dilepas // Selama tombol ditekan, variabel angle dan move diubah => kamera bergerak switch (key) { case ' ': flag = (flag == NORMAL) ? WEIRD : NORMAL; break; case 't': nitro = 1; break; case 'p': speed = 0; break; case 'o': speed = 1.0; 0; break; case 'q': exit(0); } } void Idle(void) { MoveStars(); UpdateStars(); if (nitro > 0) { //nitro aktif speed = (float) (nitro / 10) + 1.0; //speed=(nitro/10)+1 if (speed > MAXWARP) { speed = MAXWARP; } if (++nitro > MAXWARP * 10) { nitro = -nitro; } } else if (nitro < 0) { nitro++; //tambah nitro speed = (float) (-nitro / 10) + 1.0; //speed=(-nitro/10)+1 if (speed > MAXWARP) { speed = MAXWARP; } } glutPostRedisplay(); } void Display(void) { ShowStars(); if (doubleBuffer) { glutSwapBuffers(); } else { glFlush(); } } void Visible(int state) { if (state == GLUT_VISIBLE) { glutIdleFunc(Idle); } else { glutIdleFunc(NULL); } } //kaga tau static void Args(int argc, char **argv) { GLint i; doubleBuffer = GL_TRUE; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-sb") == 0) { doubleBuffer = GL_FALSE; } else if (strcmp(argv[i], "-db") == 0) { doubleBuffer = GL_TRUE; } } } int main(int argc, char **argv) { GLenum type; glutInitWindowSize(windW, windH); glutInit(&argc, argv); Args(argc, argv);//apaan? type = GLUT_RGB; type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; glutInitDisplayMode(type); glutCreateWindow("Stars"); Init(); glutReshapeFunc(Reshape); glutKeyboardFunc(pressKey); glutVisibilityFunc(Visible); glutDisplayFunc(Display); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Recent Comments