PDA

View Full Version : viewing system problem



badass
05-06-2010, 06:03 AM
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[10000][12];
int mesh[10000][12];
int in_pts[12]={-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][0]=x;
points[stack][1]=0;
points[stack][2]=z;
points[stack][3]=x;
points[stack][4]=0;
points[stack][5]=z+zinc;
points[stack][6]=x+xinc;
points[stack][7]=0;
points[stack][8]=z+zinc;
points[stack][9]=x+xinc;
points[stack][10]=0;
points[stack][11]=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][0]=points[i][0];
mesh[i][1]=points[i][1]+yoff;
mesh[i][2]=points[i][2];
mesh[i][3]=points[i][3];
mesh[i][4]=points[i][4]+yoff;
mesh[i][5]=points[i][5];
mesh[i][6]=points[i][6];
mesh[i][7]=points[i][7]+yoff;
mesh[i][8]=points[i][8];
mesh[i][9]=points[i][9];
mesh[i][10]=points[i][10]+yoff;
mesh[i][11]=points[i][11];
cout << "Stack " << i << " " << mesh[i][1] << "\n";
}


}

void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(4.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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][0],points[i][1],points[i][2]);
glVertex3i(points[i][3],points[i][4],points[i][5]);
glVertex3i(points[i][6],points[i][7],points[i][8]);
glVertex3i(points[i][9],points[i][10],points[i][11]);
glEnd();
}

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

glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3i(mesh[i][0],mesh[i][1],mesh[i][2]);
glVertex3i(mesh[i][3],mesh[i][4],mesh[i][5]);
glVertex3i(mesh[i][6],mesh[i][7],mesh[i][8]);
glVertex3i(mesh[i][9],mesh[i][10],mesh[i][11]);
glEnd();
}

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

glVertex3i(mesh[i][9],mesh[i][10],mesh[i][11]);
glVertex3i(mesh[i+3][0],mesh[i+3][1],mesh[i+3][2]);
glEnd();
}*/

axis();




}



void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);

draw();
glFlush();

}

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


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


glutMainLoop();
}



thanks
Raza

badass
05-06-2010, 08:31 PM
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[10000][12];
int mesh[10000][12];
int in_pts[12]={-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][0]=x;
points[stack][1]=0;
points[stack][2]=z;
points[stack][3]=x;
points[stack][4]=0;
points[stack][5]=z+zinc;
points[stack][6]=x+xinc;
points[stack][7]=0;
points[stack][8]=z+zinc;
points[stack][9]=x+xinc;
points[stack][10]=0;
points[stack][11]=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][0]=points[i][0];
mesh[i][1]=points[i][1]+yoff;
mesh[i][2]=points[i][2];

mesh[i][3]=points[i][3];
mesh[i][4]=points[i][4]+yoff;
mesh[i][5]=points[i][5];

mesh[i][6]=points[i][6];
mesh[i][7]=points[i][7]+yoff;
mesh[i][8]=points[i][8];

mesh[i][9]=points[i][9];
mesh[i][10]=points[i][10]+yoff;
mesh[i][11]=points[i][11];

//cout << "Stack " << i << " " << mesh[i][1] << "\n";




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][0]=points[i][0];
mesh[i][1]=points[i][1]+yoff;
mesh[i][2]=points[i][2];

mesh[i][3]=points[i][3];
mesh[i][4]=points[i][4]+yoff;
mesh[i][5]=points[i][5];

mesh[i][6]=points[i][6];
mesh[i][7]=points[i][7]+yoff;
mesh[i][8]=points[i][8];

mesh[i][9]=points[i][9];
mesh[i][10]=points[i][10]+yoff;
mesh[i][11]=points[i][11];

//cout << "Stack " << i << " " << mesh[i][1] << "\n";




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][0]=points[i][0];
mesh[i][1]=points[i][1]+yoff;
mesh[i][2]=points[i][2];

mesh[i][3]=points[i][3];
mesh[i][4]=points[i][4]+yoff;
mesh[i][5]=points[i][5];

mesh[i][6]=points[i][6];
mesh[i][7]=points[i][7]+yoff;
mesh[i][8]=points[i][8];

mesh[i][9]=points[i][9];
mesh[i][10]=points[i][10]+yoff;
mesh[i][11]=points[i][11];

//cout << "Stack " << i << " " << mesh[i][1] << "\n";




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

}

void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(4.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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][0],points[i][1],points[i][2]);
glVertex3i(points[i][3],points[i][4],points[i][5]);
glVertex3i(points[i][6],points[i][7],points[i][8]);
glVertex3i(points[i][9],points[i][10],points[i][11]);
glEnd();
}

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

glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3i(mesh[i][0],mesh[i][1],mesh[i][2]);
glVertex3i(mesh[i][3],mesh[i][4],mesh[i][5]);
glVertex3i(mesh[i][6],mesh[i][7],mesh[i][8]);
glVertex3i(mesh[i][9],mesh[i][10],mesh[i][11]);
glEnd();
}

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

glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3i(mesh[i][0],mesh[i][1],mesh[i][2]);
glVertex3i(mesh[i][3],mesh[i][4],mesh[i][5]);
glVertex3i(mesh[i][6],mesh[i][7],mesh[i][8]);
glVertex3i(mesh[i][9],mesh[i][10],mesh[i][11]);
glEnd();
}

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

glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3i(mesh[i][0],mesh[i][1],mesh[i][2]);
glVertex3i(mesh[i][3],mesh[i][4],mesh[i][5]);
glVertex3i(mesh[i][6],mesh[i][7],mesh[i][8]);
glVertex3i(mesh[i][9],mesh[i][10],mesh[i][11]);
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][0],mesh[i][1],mesh[i][2]);
glVertex3i(points[i][0],points[i][1],points[i][2]);

glVertex3i(mesh[i][3],mesh[i][4],mesh[i][5]);
glVertex3i(points[i][3],points[i][4],points[i][5]);

glVertex3i(mesh[i][6],mesh[i][7],mesh[i][8]);
glVertex3i(points[i][6],points[i][7],points[i][8]);

glVertex3i(mesh[i][9],mesh[i][10],mesh[i][11]);
glVertex3i(points[i][9],points[i][10],points[i][11]);


glEnd();
}

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

glVertex3i(mesh[i][0],mesh[i][1],mesh[i][2]);
glVertex3i(points[i][0],points[i][1],points[i][2]);

glVertex3i(mesh[i][3],mesh[i][4],mesh[i][5]);
glVertex3i(points[i][3],points[i][4],points[i][5]);

glVertex3i(mesh[i][6],mesh[i][7],mesh[i][8]);
glVertex3i(points[i][6],points[i][7],points[i][8]);

glVertex3i(mesh[i][9],mesh[i][10],mesh[i][11]);
glVertex3i(points[i][9],points[i][10],points[i][11]);


glEnd();
}

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

glVertex3i(mesh[i][0],mesh[i][1],mesh[i][2]);
glVertex3i(points[i][0],points[i][1],points[i][2]);

glVertex3i(mesh[i][3],mesh[i][4],mesh[i][5]);
glVertex3i(points[i][3],points[i][4],points[i][5]);

glVertex3i(mesh[i][6],mesh[i][7],mesh[i][8]);
glVertex3i(points[i][6],points[i][7],points[i][8]);

glVertex3i(mesh[i][9],mesh[i][10],mesh[i][11]);
glVertex3i(points[i][9],points[i][10],points[i][11]);


glEnd();
}


axis();




}



void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);

draw();
glFlush();

}

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


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


glutMainLoop();
}



thanks Raza