creating a roadway

Hi guys,

I was wondering if anyone could point me in the right direction or offer some help to create a roadway (3d).(examples)

I have a MFC dialog with a table that contains coordinates(3d points) for every lane of a roadway in it. When a user presses a certain button an opengl program should be launched that creates a new window with a 3d roadway in it determined from the points in the mfc dialog table. I don’t need any texturing at all. Wireframe would be great.

I have tried the simple 3d tutorials for creating 3d objects (like the rotating cube example), but these all had their points hardcoded in vertex arrays or something.
Now, my problem is that I generate all the points of the roadway at runtime.How do I tell the opengl program:here you go, these are my new points, draw me a roadway???
I can get the part to launch an opengl program working.
I was thinking of writing these points from the table into a temporary file. The opengl program,when launched, would read these points from that temporary file and create the roadway. Is this the way to go or am I just lost???

Anyone done something like this at all???
What is the best way do accomplish this task???

Any help is very very much appreciated.

Does it have to be an external program?
If yes, then using files can be good enough. You can also choose to communicate with the other program using messages, shared memory, TCP/IP or any other protocol you like. Since you haven’t provided any info about operating system, then I can’t be more specific. Also, this is atually not OpenGL related topic.

Os is windows XP. Actually, it is an opengl related question since my problem is to “write an OpenGL program” that can draw a road. Communication with other programs is not a problem at all. That part works fine. I just need to know how and what format to store these points to a file so that the OpenGL program can manipulate them…
Cheers for your response

Ok, as for all stuff related to initializing OpenGL and setting up perspective, I’m sure you’ll find lots of tutorials.
So the only remaining thing is how to draw a geometry.
First of all the question is what data you have - I assume it’s a set of points that represent the center of a road and you need to find a set of points that define edges of a road - is that right?
First of all start with an application that renders line strip from given array of points:

  for (i = 0; i < vertexCount; i++)
    glVertex3f(xarray[i], yarray[i], zarray[i]);

When you get that (or something similar) working then we can move on to GL_TRIANGLE_STRIP - this will require a little bit of simple calculations.
Also, if you would like that road to be smoothed, then we can work on it, too. :slight_smile:

Yes, I have all the points for left lane edge, center and right lane edge.

I would save these points into a file in some kind of a vertex array structure (like the teapot example (vertices.h)). I would then include this .h file in the opengl main program. The opengl program would then draw all the geometry. I am able to do the line strip that you suggest.

I was thinking of another approach…
Lets say I was able to cut my road into cube segments (like cutting a cake)and I was able to calculate these points of the cubes, would it be possible to create a structure that represents a set of these cubes and then draw the geometry? I can see that this would create duplicate points when next cube is joined on but this is a tradeoff I am willing to take since there would never be a set of cubes that large that would cause performance issues. Can you suggest a structure for this with emphasis on code please…

Many thanks for all your help so far.

As for placing vertex data in .h files. I think it would be better just to save vertex array to a file (could be binary) and you program would just load that file instead of having all data hardcoded in yor program.

As for cubes - you could use a very common approach: vertex array and index array.

int vertexCount;
float* vertexArray;
int indexCount;
unsigned short* indexArray;

Let’s assume you want to render one quad that is made of two triangles:
| /|
|/ |
The structure would have to contain the following data:

vertexCount = 4;
vertexArray[] = {-1,1,0, 1,1,0, 1,-1,0, -1,-1,0};
indexCount = 6; //2 triangles * 3 points
indexArray[] = {0,1,3, 1,2,3};

So, to draw first triangle you need to connect points 0-1-3 and for second triangle you connect 1-2-3. Vertices 1 and 3 are used twice.

This code draws triangles:

int i, index;
for (i = 0; i < indexCount; i++)
  index = indexArray[i];
  glVertex3f(vertexArray[3 * index + 0], vertexArray[3 * index + 1], vertexArray[3 * index + 2]);
  //or just: glVertex3fv(vertexArray + 3 * index);

It would be even faster to render it if you use vertex arrays, but I’ll leave that for later.

Sorry, I just noticed I was talking nonsense there about including a .h file…

I had a lot of confusion of what I wanted to do exactly. I think I have it now:

I want my openGL program to read vertices from a normal .txt file.From those vertices I want it to draw all the geometry concerning a roadway.

.txt contents

…and so on

So, what I need is a sweet function that can read such a file and store them in (GLfloat arrays???). I can write this. Then in the for loop I would draw a polygon intstead of lines and increment loop by 4 every time I draw a polygon. All I need to do is prepare the .txt file well with a correct sequence of polygons to be drawn.
Is this the way forward???

Looks good enough. Just one thing - you do not have to increment your loop by 4 - just use glBegin(GL_QUADS) and pass all vertices in your array. OpenGL will create one quad out of every 4 vertices anyway.

Ok, I have written my function to read in the data.
I store the data as follows;

GLfloat xarray; // all x points
GLfloat yarray; // all y points
GLfloat zarray; // all z points

So how should I use the glBegin(GL_QUADS) and where should it be placed (in display()???)…

Ok, I assume it’s actually:

GLfloat* xarray;
GLfloat* yarray;
GLfloat* zarray;

Rendering from such array would look like this:

for (i = 0; i < vertexCount; i++)
  glVertex3f(xarray[i], yarray[i], zarray[i]);

The rest is up to you now. If you feel like you need some examples/tutorials then be sure to visit: