05 Mayıs 2006

Basit 3 boyutlu grafik animasyon programlama

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <dos.h>

#define mx 320
#define my 20
#define lvh 300 //levha uzaklığı(Arttıkça
                //perspektif büyüklük artar)
#define pi 3.14159265
#define gozz 50 //göz yüksekliği
#define yrcp 100 //göz noktasının merkez etrafında
                 //döndüğü yarıçap değeri
#define ys 12 //yüzey sayısı
struct yzy{
   int x[3],y[3],z[3];} yuzey[ys];
int i; long detyuzey[ys],detyuzeyx[ys],detyuzeyy[ys], detyuzeyz[ys],yuzeytoplam[ys]; float cstb[360],sntb[360]; void ciz(int aci) { int j,xx[3],yy[3],zz[3],px[3],py[3],pldizi[ys][8]; for (j=0;j<ys;j++) { for (i=0;i<3;i++) { xx[i]=yuzey[j].y[i]*sntb[aci] +yuzey[j].x[i]*cstb[aci]; yy[i]=yuzey[j].y[i]*cstb[aci] -yuzey[j].x[i]*sntb[aci]+yrcp; px[i]=(int)(mx+lvh*xx[i]/yy[i]); py[i]=(int)(my-lvh*(yuzey[j].z[i]-gozz)/yy[i]); } pldizi[j][0]=px[0]; pldizi[j][1]=py[0]; pldizi[j][2]=px[1]; pldizi[j][3]=py[1]; pldizi[j][4]=px[2]; pldizi[j][5]=py[2]; pldizi[j][6]=px[0]; pldizi[j][7]=py[0]; yuzeytoplam[j]= (float) detyuzeyx[j]*yrcp*sntb[aci] -detyuzeyy[j]*yrcp*cstb[aci]+detyuzeyz[j]*gozz; if (yuzeytoplam[j]>=detyuzey[j]) fillpoly(4,pldizi[j]); } } void main() { int gdriver = 9, gmode=1, errorcode; int aci=0; for (i=0;i<360;i++) { cstb[i]=cos(i*pi/180); sntb[i]=sin(i*pi/180); } //Üst yüzey yuzey[0].x[0]= 40; yuzey[0].y[0]= 30; yuzey[0].z[0]= 20; yuzey[0].x[1]=-40; yuzey[0].y[1]= 30; yuzey[0].z[1]= 20; yuzey[0].x[2]=-40; yuzey[0].y[2]=-30; yuzey[0].z[2]= 20; //Sağ yüzey yuzey[1].x[0]= 40; yuzey[1].y[0]= 30; yuzey[1].z[0]= 20; yuzey[1].x[1]= 40; yuzey[1].y[1]=-30; yuzey[1].z[1]= 20; yuzey[1].x[2]= 40; yuzey[1].y[2]=-30; yuzey[1].z[2]= 0; //Ön yüzey yuzey[2].x[0]= 40; yuzey[2].y[0]=-30; yuzey[2].z[0]= 20; yuzey[2].x[1]=-40; yuzey[2].y[1]=-30; yuzey[2].z[1]= 20; yuzey[2].x[2]=-40; yuzey[2].y[2]=-30; yuzey[2].z[2]= 0; //Sol yüzey yuzey[3].x[0]=-40; yuzey[3].y[0]=-30; yuzey[3].z[0]= 20; yuzey[3].x[1]=-40; yuzey[3].y[1]= 30; yuzey[3].z[1]= 20; yuzey[3].x[2]=-40; yuzey[3].y[2]= 30; yuzey[3].z[2]= 0; //Arka yüzey yuzey[4].x[0]=-40; yuzey[4].y[0]= 30; yuzey[4].z[0]= 20; yuzey[4].x[1]= 40; yuzey[4].y[1]= 30; yuzey[4].z[1]= 20; yuzey[4].x[2]= 40; yuzey[4].y[2]= 30; yuzey[4].z[2]= 0; //Alt yüzey yuzey[5].x[0]= 40; yuzey[5].y[0]=-30; yuzey[5].z[0]= 0; yuzey[5].x[1]=-40; yuzey[5].y[1]=-30; yuzey[5].z[1]= 0; yuzey[5].x[2]=-40; yuzey[5].y[2]= 30; yuzey[5].z[2]= 0; //Üst yüzey 2 yuzey[6].x[0]= 40; yuzey[6].y[0]= 30; yuzey[6].z[0]= 20; yuzey[6].x[1]=-40; yuzey[6].y[1]=-30; yuzey[6].z[1]= 20; yuzey[6].x[2]= 40; yuzey[6].y[2]=-30; yuzey[6].z[2]= 20; //Sağ yüzey 2 yuzey[7].x[0]= 40; yuzey[7].y[0]= 30; yuzey[7].z[0]= 20; yuzey[7].x[1]= 40; yuzey[7].y[1]=-30; yuzey[7].z[1]= 0; yuzey[7].x[2]= 40; yuzey[7].y[2]= 30; yuzey[7].z[2]= 0; //Ön yüzey 2 yuzey[8].x[0]= 40; yuzey[8].y[0]=-30; yuzey[8].z[0]= 20; yuzey[8].x[1]=-40; yuzey[8].y[1]=-30; yuzey[8].z[1]= 0; yuzey[8].x[2]= 40; yuzey[8].y[2]=-30; yuzey[8].z[2]= 0; //Sol yüzey 2 yuzey[9].x[0]=-40; yuzey[9].y[0]=-30; yuzey[9].z[0]= 20; yuzey[9].x[1]=-40; yuzey[9].y[1]= 30; yuzey[9].z[1]= 0; yuzey[9].x[2]=-40; yuzey[9].y[2]=-30; yuzey[9].z[2]= 0; //Arka yüzey 2 yuzey[10].x[0]=-40; yuzey[10].y[0]= 30; yuzey[10].z[0]= 20; yuzey[10].x[1]= 40; yuzey[10].y[1]= 30; yuzey[10].z[1]= 0; yuzey[10].x[2]=-40; yuzey[10].y[2]= 30; yuzey[10].z[2]= 0; //Alt yüzey 2 yuzey[11].x[0]= 40; yuzey[11].y[0]=-30; yuzey[11].z[0]= 0; yuzey[11].x[1]=-40; yuzey[11].y[1]= 30; yuzey[11].z[1]= 0; yuzey[11].x[2]= 40; yuzey[11].y[2]= 30; yuzey[11].z[2]= 0; for (i=0;i<ys;i++) { detyuzeyx[i]= yuzey[i].y[1]*yuzey[i].z[2] +yuzey[i].y[2]*yuzey[i].z[0] +yuzey[i].y[0]*yuzey[i].z[1] -yuzey[i].y[1]*yuzey[i].z[0] -yuzey[i].y[2]*yuzey[i].z[1] -yuzey[i].y[0]*yuzey[i].z[2]; detyuzeyy[i]= yuzey[i].x[0]*yuzey[i].z[2] +yuzey[i].x[1]*yuzey[i].z[0] +yuzey[i].x[2]*yuzey[i].z[1] -yuzey[i].x[2]*yuzey[i].z[0] -yuzey[i].x[0]*yuzey[i].z[1] -yuzey[i].x[1]*yuzey[i].z[2]; detyuzeyz[i]= yuzey[i].x[0]*yuzey[i].y[1] +yuzey[i].x[1]*yuzey[i].y[2] +yuzey[i].x[2]*yuzey[i].y[0] -yuzey[i].x[2]*yuzey[i].y[1] -yuzey[i].x[0]*yuzey[i].y[2] -yuzey[i].x[1]*yuzey[i].y[0]; detyuzey[i]= (float) yuzey[i].x[0]*yuzey[i].y[1]*yuzey[i].z[2] +yuzey[i].x[1]*yuzey[i].y[2]*yuzey[i].z[0] +yuzey[i].x[2]*yuzey[i].y[0]*yuzey[i].z[1] -yuzey[i].x[2]*yuzey[i].y[1]*yuzey[i].z[0] -yuzey[i].x[0]*yuzey[i].y[2]*yuzey[i].z[1] -yuzey[i].x[1]*yuzey[i].y[0]*yuzey[i].z[2]; } initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); errorcode = graphresult(); if (errorcode != 0) { //Bir hata oluşmuşsa printf("Grafik hatası: %s\n", grapherrormsg(errorcode)); printf("Sonlandirmak icin bir tusa basin:"); getch(); exit(1); } //Grafik komutlarının başlangıcı - Alan 640x350 setfillstyle(10,4); while(!kbhit()) { setactivepage(1); cleardevice(); ciz(aci); aci++; if (aci>=360) aci-=360; setvisualpage(1); delay(100); setactivepage(1); cleardevice(); ciz(aci); aci++; if (aci>=360) aci-=360; setvisualpage(1); delay(100); } // Grafik modundan çıkış getch(); closegraph(); }
2 günlük bir çalışma sonucunda hazırladığım basit bir grafik animasyon programı. Her yüzey bir üçgen polygondan oluşuyor. Program hızlandırma için herhangi bir assembly parçaçığı kullanmıyor sadece BGI grafikleri kullanılıyor. Ayrıca Turbo C'nin kurulduğu varsayılan dizin olarak "c:/tc" yi ve EGAVGA.BIN dosyasının bulunduğu klasörü de "c:/tc/bgi" olarak kabul ettim.