PDA

View Full Version : 2D mountain need help



badass
04-16-2010, 06:07 PM
trying to draw a 2D mountain using midpoint displacement method...my problem is that im getting the midpoints and storing them but along side the older lines....i need some way to store only recently obtained midpoints and delete the points of the line they were obtained from...here's my code

#include <stdlib.h>
#include <iostream>

#include <GL/glut.h>
int in_ax=100,in_ay=100,in_bx=300,in_by=100;
int verts[500][4]; // main array for storing points
int stack=0; // number of lines stored in array
int whichline=0; // line to be broken
int iter=10;


void storeline(int ax,int ay,int bx,int by)
{
verts[stack][0]=ax;
verts[stack][1]=ay;
verts[stack][2]=bx;
verts[stack][3]=by;
stack++;

}



using namespace std;

GLint windowHeight = 500.0;
GLint windowWidth = 500.0;
GLint displayHeight = 500.0;
GLint displayWidth = 500.0;

//init function holds all initializations and related one-time parameter settings
void init (void)
{
// Set the background color to be white
glClearColor (1.0, 1.0, 1.0, 0.0);
glPointSize(4.0);

//Set the current matrix to be the projection matrix, another parameter can be GL_MODELVIEW
glMatrixMode (GL_PROJECTION);

/*
Set projection parameters.
the clipping window is of size windowWidth x windowHeight
*/
gluOrtho2D (0.0, windowWidth, 0.0, windowHeight);
}

void draw(void)
{
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glFlush ( ); // Process all OpenGL routines as quickly as possible.

};

void point(int a,int b)
{
glBegin(GL_POINTS);
glVertex2i(a,b);
glEnd();
}


void lineSegment (void)
{
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.

glColor3f (0.0, 0.0, 1.0); // Set line segment color to red.
glBegin (GL_LINES);
for(int i=stack;i>=0;i-=1) { // prints a single line
glVertex2i(verts[i][0],verts[i][1]);
glVertex2i(verts[i][2],verts[i][3]);
}

glEnd ( );



glFlush ( ); // Process all OpenGL routines as quickly as possible.
}

void winReshapeFcn (int newWidth, int newHeight)
{
glViewport (0, 0, (GLsizei) newWidth, (GLsizei) newHeight);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
gluOrtho2D (0.0, windowWidth, 0.0, windowHeight);

glClear (GL_COLOR_BUFFER_BIT);
}


void drawlines() // call back function
{
lineSegment();
}

void calmidpoint(int x1,int y1,int x2, int y2, int &amp;amx,int &amp;amy)
{
int xoff,yoff;
yoff=30;
amx=(x1+x2)/2;
amy=(y1+y2+yoff)/2;
}
void lines(int a,int aa,int b,int bb)
{
int amx,amy;

calmidpoint(a,aa,b,bb,amx,amy);

storeline(a,aa,amx,amy);
storeline(b,bb,amx,amy);

}

void main (int argc, char** argv)
{

glutInit (&amp;argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (50, 100); // Set top-left display-window position.

for(int i=0;i<iter;i++) {
lines(in_ax,in_ay,in_bx,in_by);

in_ax=verts[whichline][0];
in_ay=verts[whichline][1];
in_bx=verts[whichline][2];
in_by=verts[whichline][3];

whichline++;
}

glutInitWindowSize (displayWidth, displayHeight); // Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.


init ( ); // Execute initialization procedure.

glutDisplayFunc (drawlines); // Send graphics to display window.

glutMainLoop ( ); // Display everything and wait.
}

cheers
Raza,

strattonbrazil
04-17-2010, 04:18 AM
Is this really an OpenGL question? So you have a set of points, which you use to calculate these lines and put them on the stack and if you have new points coming in, you want to get rid of everything else? Why don't you just set stack to zero?