PDA

View Full Version : need help to draw 3D square mesh

04-21-2010, 02:54 AM
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[10000][12];
int in_pts[12]={-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][0]=x;
points[stack][1]=y;
points[stack][2]=z+zinc;
points[stack][3]=x;
points[stack][4]=y+yinc;
points[stack][5]=z+zinc;
points[stack][6]=x+xinc;
points[stack][7]=y+yinc;
points[stack][8]=z+zinc;
points[stack][9]=x+xinc;
points[stack][10]=y;
points[stack][11]=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][0],points[i][1],0);
glVertex3i(points[i][3],points[i][4],0);
glVertex3i(points[i][6],points[i][7],0);
glVertex3i(points[i][9],points[i][10],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(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
glutCreateWindow("3D Viewing System");
myInit();
initialize();

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

glutMainLoop();
}

Fugitive
04-21-2010, 01:56 PM
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][0],points[i][1],0);
glVertex3i(points[i][3],points[i][4],0);
glVertex3i(points[i][6],points[i][7],0);
glVertex3i(points[i][9],points[i][10],0);
glEnd();

should be:

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