Back to July Calendar Previous Entry Next Entry→

July, 18, 2005

 

Wanna build a mountain? 

 

The idea here is to modify our tessellation program from last week to recurse triangles so that their top edges form the top edge of a random curve that emulates a kind of mountain-scape. 

 

My first implementation has proved scary from the get-go.  I got the error message

 

stdlib.h(256) : error C2381: 'exit' : redefinition; __declspec(noreturn) differs

 

Apparently the stdlib.h file defines exit() as

 

_CRTIMP __declspec(noreturn) void   __cdecl exit(int);

 

which conflicts with the glut.h definition:

 

extern _CRTIMP void __cdecl exit(int);

 

which I found out by reading the lists message at

 

https://lists.dce.harvard.edu/pipermail/cscie234/2003-October/000137.html

 

which suggests just replacing the glut.h definition with the stdlib.h definition. 

 

Needless to say, this is very antsy-inducing, but I forage forward with a foray as follows:

 

#if defined(_WIN32)

# ifndef GLUT_BUILDING_LIB

_CRTIMP __declspec(noreturn) void   __cdecl exit(int);

//extern _CRTIMP void __cdecl exit(int);

# endif

 

where I’ve commented out the glut.h definition and replaced it with the stdlib.h definition.  Scary stuff.

 

But it works!

 

After a number of uninteresting wrong results, I’ve gotten some interesting wrong results.  Here is the code that leads to the following with n = 2 levels of recursion, just to suss out the pattern, which will have to wait ‘til tomorrow (gotta make sure it’s right so forever, goodnight…)

 

// for srand

#include <ctime>

#include <cstdlib>

 

void triangle(GLfloat *a, GLfloat *b, GLfloat *c) {

      glBegin(GL_POLYGON);

         glEdgeFlag(GL_TRUE);

         glVertex2fv(a);

         glVertex2fv(b);

         glVertex2fv(c);

    glEnd();

}

 

void divide_triangle(GLfloat *a, GLfloat *b, GLfloat *c, int m) {

      //Triangle subdivision using vertices

      GLfloat v[2][2];  //these are the two new random mountaintop points we'll compute

      float x;

      if(m>0) {

            v[0][0] = 0.5*(a[0] + c[0]);

            x = (float) rand()/RAND_MAX;

            v[0][1] = a[1]+x*(c[1] - a[1]);

            v[1][0] = 0.5*(b[0] + c[0]);

            x = (float) rand()/RAND_MAX;

            v[1][1] = b[1]+x*(c[1] - b[1]);

            // recursive calls

            glColor3f(1.0,0.0,0.0);

            divide_triangle(a, c, v[0], m-1);              

            glColor3f(0.0,0.0,1.0);

            divide_triangle(b, c, v[1], m-1);

      }

      else

            triangle(a,b,c);

}

 

 

// The original triangle

GLfloat v[3][2] = {{0.0,0.0},{2.0,0.0},{1.0,1.0}};

void display(void) {

      glClear(GL_COLOR_BUFFER_BIT);

      glPolygonMode(GL_FRONT, GL_LINE);

      int n = 2;

      glColor3f(1.0, 0.0, 0.0);

      divide_triangle(v[0], v[1], v[2], n);    

      glFlush();

}