 # need help to draw 3D square mesh

im try to draw a 3D square mesh which extends along x and z axis…than i can select random square in the middle and lift the heights to give a landscape look…run this code, it doesnt look right…plz tell me where the problem lies

heres a picture of what i want to make

http://stackoverflow.com/questions/1715560/generate-square-mesh-given-an-unordered-x-y-and-z-vectors

``````#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
int points;
int in_pts={-200,-200,-200,-200,-100,-200,-170,-100,-200,-170,-200,-200};
int minx=-400,miny=-400,maxx=400,maxy=400,minz=-500,maxz=500,xinc=10,yinc=10,zinc=30;
int stack=0;

using namespace std;

void initialize()
{
int y=miny,x=minx,z=minz;
for(y=miny;y<=maxy;y+=yinc) {
for(int x=minx;x<=maxx;x+=xinc) {
points[stack]=x;
points[stack]=y;
points[stack]=z+zinc;
points[stack]=x;
points[stack]=y+yinc;
points[stack]=z+zinc;
points[stack]=x+xinc;
points[stack]=y+yinc;
points[stack]=z+zinc;
points[stack]=x+xinc;
points[stack]=y;
points[stack]=z+zinc;
stack++;

}
z+=zinc;
}

}

void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(4.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(200.0,300,350.0,0.0,0.0,0.0,0.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glFrustum(-400.0,400.0,-400.0,400.0,150.0,800.0);
}

void draw()
{
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,1); // z-axis blue
glVertex3i(0,0,-500);
glVertex3i(0,0,500);
glEnd();

glColor3f(0,0,0);
for(int i=0;i<stack;i++){
glBegin(GL_LINE_LOOP);
glVertex3i(points[i],points[i],0);
glVertex3i(points[i],points[i],0);
glVertex3i(points[i],points[i],0);
glVertex3i(points[i],points[i],0);

glEnd();
}

}

void fill()
{

}

void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f,0.0f,0.0f);
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();

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

glutMainLoop();
}

``````

You points array is a list of 3d points x,y,z, yet your drawing loop is only drawing using x,y coords. For example:

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

``````

should be:

``````
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();

``````

tbh im quite embarassed… i spent a day trying to figure this out…i remember i put the zeros there for testing purposes anyways i lived i learned

many thanks
Raza

No worries. We have all been there…