 # viewing system problem

hi, im trying to generate a terrain…

i have a base square mesh called points and a randomly elevated mesh called mesh…
the points grid is on x-z plane with all heights set to 0…mesh is basically a copy of points but with random heights between 100 and 250…

my viewing system is perspective view…when i draw the mesh it seems as if the x and z coordinates go beyond minx and maxx and minz and maxz… i dont know if the problem is with my viewing system or the coords because the x and z coords for mesh are exact copy of points…

below is the code

``````
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <ctime>
int points;
int mesh;
int in_pts={-200,-200,-200,-200,-100,-200,-170,-100,-200,-170,-200,-200};
int minx=-400,maxx=400,minz=-400,maxz=400,xinc=20,zinc=20;
int stack=0;

using namespace std;

void initialize()
{
int x=minx,z=minz;
for(z=minz;z<=maxz;z+=zinc) {
for(int x=minx;x<=maxx;x+=xinc) {
points[stack]=x;
points[stack]=0;
points[stack]=z;
points[stack]=x;
points[stack]=0;
points[stack]=z+zinc;
points[stack]=x+xinc;
points[stack]=0;
points[stack]=z+zinc;
points[stack]=x+xinc;
points[stack]=0;
points[stack]=z;
stack++;

}
}
}

void elevate()
{
int yoff=30,d1y=100,d2y=250;

for(int i=0;i<stack;i++) {
yoff=d1y+ rand()/(RAND_MAX/(d2y-d1y+1)+1);
mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];
mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];
mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];
mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];
cout << "Stack " << i << " " << mesh[i] << "
";
}

}

void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(4.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(00,450,-450.0,0.0,0.0,-400.0,0.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glFrustum(-400.0,400.0,-400.0,400.0,100.0,1000.0);
}

void axis()
{
glBegin(GL_LINES);
glColor3f(1,0,0); // x-axis red
glVertex3i(-400,0,0);
glVertex3i(400,0,0);
glColor3f(0,1,0); // y-axis green
glVertex3i(0,-400,0);
glVertex3i(0,400,0);
glColor3f(0,0,0); // z-axis black
glVertex3i(0,0,-400);
glVertex3i(0,0,400);
glEnd();
}
void draw()
{

for(int i=0;i<stack;i++){

glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3i(points[i],points[i],points[i]);
glVertex3i(points[i],points[i],points[i]);
glVertex3i(points[i],points[i],points[i]);
glVertex3i(points[i],points[i],points[i]);
glEnd();
}

for(int i=0;i<stack;i++){

glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glEnd();
}

/*for(int i=350;i<380;i+=3){
glColor3f(0,0,0);
glBegin(GL_LINES);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i+3],mesh[i+3],mesh[i+3]);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i+3],mesh[i+3],mesh[i+3]);
glEnd();
}*/

axis();

}

void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);

draw();
glFlush();

}

void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
glutCreateWindow("3D Viewing System");
myInit();
initialize();
elevate();

cout << "
Stack = " << stack;
glutDisplayFunc(myDisplay);

glutMainLoop();
}

``````

thanks
Raza

ok heres a better version of code with the same problem…

ive elevated 3 rows…

the elevated mesh should be sitting right on top of the base mesh…but instead its a bit drifted to the sides…

note x and z for mesh[][] are copies from points[][] so im pretty sure its not the coords

``````

#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <ctime>
int points;
int mesh;
int in_pts={-200,-200,-200,-200,-100,-200,-170,-100,-200,-170,-200,-200};
int minx=-400,maxx=400,minz=-400,maxz=400,xinc=20,zinc=20;
int stack=0; // count for squares

using namespace std;

void initialize()
{
int x=minx,z=minz;
for(z=minz;z<=maxz;z+=zinc) {  // row
for(int x=minx;x<=maxx;x+=xinc) {  // coloums
points[stack]=x;
points[stack]=0;
points[stack]=z;
points[stack]=x;
points[stack]=0;
points[stack]=z+zinc;
points[stack]=x+xinc;
points[stack]=0;
points[stack]=z+zinc;
points[stack]=x+xinc;
points[stack]=0;
points[stack]=z;
stack++;

}

}
}

void elevate()
{
int yoff=0,d1y=100,d2y=150;
int j=0;

for(int i=615;i<615+41;i++) {     // middle row elevate
yoff=d1y+ rand()/(RAND_MAX/(d2y-d1y+1)+1);

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

//cout << "Stack " << i << " " << mesh[i] << "
";

if(d1y>=10)
d1y-=3;d2y-=3;
}

d1y=150;d2y=200;
for(int i=1641;i<=1681;i++) {   // last row elevate
yoff=d1y+ rand()/(RAND_MAX/(d2y-d1y+1)+1);

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

//cout << "Stack " << i << " " << mesh[i] << "
";

if(d1y>=10)
d1y-=3;d2y-=3;
}

d1y=100;d2y=150;
for(int i=0;i<=40;i++) {  // first row elevate
yoff=d1y+ rand()/(RAND_MAX/(d2y-d1y+1)+1);

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

mesh[i]=points[i];
mesh[i]=points[i]+yoff;
mesh[i]=points[i];

//cout << "Stack " << i << " " << mesh[i] << "
";

if(d1y>=10)
d1y-=3;d2y-=3;
}

}

void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(4.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,250,-450.0,0.0,0.0,-400.0,0.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glFrustum(-400.0,400.0,-400.0,400.0,100.0,500.0);
}

void axis()
{
glBegin(GL_LINES);
glColor3f(1,0,0); // x-axis red
glVertex3i(-400,0,0);
glVertex3i(400,0,0);
glColor3f(0,1,0); // y-axis green
glVertex3i(0,-400,00);
glVertex3i(0,400,00);
glColor3f(0,0,0); // z-axis black
glVertex3i(0,0,-400);
glVertex3i(0,0,400);
glEnd();
}
void draw()
{

for(int i=0;i<stack;i++){ // base mesh

glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);

glVertex3i(points[i],points[i],points[i]);
glVertex3i(points[i],points[i],points[i]);
glVertex3i(points[i],points[i],points[i]);
glVertex3i(points[i],points[i],points[i]);
glEnd();
}

for(int i=615;i<615+41;i++) { // middle elevated mesh

glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glEnd();
}

for(int i=1641;i<=1681;i++) { // last elevated mesh

glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glEnd();
}

for(int i=0;i<=40;i++) { // first elevated mesh

glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(mesh[i],mesh[i],mesh[i]);
glEnd();
}

for(int i=615;i<615+41;i++){ // connect base mesh to elevated mesh
glColor3f(0,0,0);
glBegin(GL_LINES);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glEnd();
}

for(int i=1641;i<=1681;i++){ // connect base mesh to elevated mesh
glColor3f(0,0,0);
glBegin(GL_LINES);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glEnd();
}

for(int i=0;i<=40;i++){ // connect base mesh to elevated mesh
glColor3f(0,0,0);
glBegin(GL_LINES);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glVertex3i(mesh[i],mesh[i],mesh[i]);
glVertex3i(points[i],points[i],points[i]);

glEnd();
}

axis();

}

void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);

draw();
glFlush();

}

void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
glutCreateWindow("3D Viewing System");
myInit();
initialize();
elevate();

cout << "
Stack = " << stack;
glutDisplayFunc(myDisplay);

glutMainLoop();
}

``````

thanks Raza