3D Grid in OPENGL

Hi All,

I actually want to create a wall for  a room.So i wanted to make the wall the room using a grid.So that i can effiencently use the lighting effects.

So now i know the 4 edges of the grid , how can run the loop to create the grid automatically.

Who is Participating?
davebytesConnect With a Mentor Commented:
Well, simple starting approach would be a function that takes your 2x2 wall and returns back a 3x3 wall -- subdivide each edge mid-way, and find the center-point as well (mid-point between corners or between two of the new mid-way points...).

A more 'iterative' approach from there is pretty easy.  Decide how many subdivisions you want along a given dimension, get the distance vector between two edge points, and generate the iterative points along the line.  Repeat on all edges, then repeat along one dimension to generate the internal vertices to match the new internal edges...

float v1[3], v2[3]; // assuming these come into the line subdivision function

#define NUM_INT 3  // I'm hardcoding to subdivide into 3 new intermediate vertices
float v[NUM_INT][3];
int i,j;

for (i=0; i<NUM_INT; i++)
  for (j=0; j<3; j++)
    v[i][j] = v1[j] + ( (i+1) * ( (v2[j]-v1[j]) / (NUM_INT+1) ) ); // NUM_INT+1 as we're doing the number of internal edges effectively...

That will interpolate between two vertices and generate the intermediate points between the two.

OF COURSE, THIS GENERALLY ASSUMES A CO-PLANAR SET OF START VERTICES.  It'll work to some degree even if not, just won't necessarily take into account a gradual 'bend' that might be desired.  You'd need a more complex interpolator than my linear function above.

srihari1986Author Commented:
Hi dave,

Thanks for the support.After taking a look at your example i have come out a similiar one that can draw a 10 X 10 grid.I took me a day to understand ur method.Sorry for that i am new to opengl and not too good in maths.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.