Thursday, September 29, 2011

Figure 35


$40 off Craftsman 5 Drawer Homeowner Tool Center with Riser and Parts Bins $99.99 on sale 10/2-10/8
Sears Deal of the Day: Thule Car Top Carrier, Quest
Wholesale Fleece Blankets
400950_BuddhaGroove.com-Culture Infused Living: Home Accents, Jewelry, Accessories from around the world!
400948_Culture Infused Living: Home Accents, Jewelry, and accessories from around the world. CulturalElemen
504427_Save up to 75% on batteries and accessories at Brooklyn Battery Works!
504080_10% OFF Orders of $75 or More from Journelle.com! Use Promo Code: NOV11 (Valid 11/1 - 11/30/11)
502239_Brand names, bargain prices at shoezoo.com. If you don't love your new shoes, return them for free!

400949_GaneshMall.com-Culture Infused Living: Home Accents, Jewelry, and accessories from around the world!



Figure 35
========================

/*
//Author: Cheikh Drame
//To support, please do one of the following
//- Visit irap123.com many times a day
//- Join irap123.com
//- Buy my app irap123 (rhyming dictionary) available for android, and iphone. On windows phone, it is called iLyrics. If not, you can get the desktop version at www.irap123.com/apps/webstore/
//- Include irap123.com in your blogs, forums, facebook walls, email footers, comments
//- send a donation at my paypal address at cbdsigned.vevo@gmail.com
//- Buy a story at http://shop.pparzygnat.com
*/
// Mountains.cpp : Defines the entry point for the console application.
//


#include
#include
#include


GLfloat pos[]={-2,6,5,0};//directional light from sun
GLfloat amb[]={0.2, 0.2, 0.2, 1.0};
GLfloat front_amb_diff[]={0.7, 0.75, 0.75, 1.0};
float theta=0, dt=0.4, p=3.141593/180; //parameters for animation control
bool flag=true;
int landscape[400][400];// counter array for elevation profile

void vxv(float *n, float *v1, float *v2) //compute n=v1*v2
{
n[0]=v1[1]*v2[2]-v1[2]*v2[1];
n[1]=v1[2]*v2[0]-v1[0]*v2[2];
n[2]=v1[0]*v2[1]-v1[1]*v2[0];
}
void elevation(int seed, int n)//creation elevation profile
{
int i, j, k, centerx=200, centery=200, stepx, stepy, xmin, xmax, ymin, ymax, xsq, ysq, radius=25, radius_sq=radius*radius, maxh=0;
// srand(seed);
for(int k=0; k stepx=(rand()>RAND_MAX/2)?3:-3;
stepy=(rand()>RAND_MAX/2)?3:-3;
centerx+=stepx;
centery+=stepy;
if(centerx<0 || 400<=centerx) centerx=200;//re-center disk
if(centery<0 || 400<=centery) centery=200;
xmin=(0<=centerx-radius)?centerx-radius:0;
xmax=(400<=centerx+radius)?399:centerx+radius;
ymin=(0 ymax=(400<=centery+radius)?399:centery+radius;
for(i=xmin; i<=xmax; ++i)//look for cells covered by disk
for(j=ymin; j<=ymax; ++j){
xsq=i-centerx;
xsq*=xsq;
ysq=j-centery;
ysq*=ysq;
if( (xsq+ysq)<=radius_sq) landscape[i][j]++;
}
}
for(i=0; i<400; ++i)//scale height values to [0, 255.0]
for(j=0; j<400; ++j) landscape[i][j]*=0.3;
}
void display()
{
float v1[3], v2[3], v3[3], n[3];
int step=3;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(400*sin(theta*p), 400, 400*cos(theta*p), 0, 50, 0,0,1,0);
if(flag) glLightfv(GL_LIGHT0, GL_POSITION, pos);//static light
glTranslated(-200,0,200); //center terrain at origin by translation
v1[0]=step; v1[2]=0; //ste up the x and z components of three
v2[0]=step; v2[2]=-step;//vectors along the edges and the main
v3[0]=0.0; v3[2]=-step;

glBegin(GL_TRIANGLES);
for(int i=0; i<400-step; i+=step){
for(int j=0; j<400-step; j+=step){//draw a quadrilateral
v1[1]=landscape[i+step][j]-landscape[i][j];//set up y component
v2[1]=landscape[i+step][j+step]-landscape[i][j];
v3[1]=landscape[i][j+step]-landscape[i][j];
vxv(n, v1,v2);//compute surface normal for 1st triangle
glNormal3fv(n);//draw first triangle
glVertex3i(i, landscape[i][j], -j);
glVertex3i(i+step, landscape[i+step][j], -j);
glVertex3i(i+step, landscape[i+step][j+step], -j-step);
vxv(n, v2, v3); //compute surface normal for 2nd triangle
glNormal3fv(n); //draw second triangle
}
}
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void idle(void)
{
if(theta>=360) flag=!flag;
theta=(theta<360)?theta+dt:dt;//switch betw static and moving light
glutPostRedisplay();
}


void main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutInitWindowPosition(200,100);
glutCreateWindow("Mountains");
glClearColor(0.0, 0.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, front_amb_diff);
glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
glEnable(GL_NORMALIZE);//let the sys normalize normal vectors
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 1.0, 10, 800);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutDisplayFunc(display);
//glutIdleFunc(idle);
elevation(2345, 5000); //generate elevation profile
glutMainLoop();
}

No comments:

Post a Comment