Results 1 to 10 of 10

Thread: creating a roadway

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2006
    Location
    Slovenia
    Posts
    5

    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.
    Cheers

  2. #2
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,107

    Re: creating a roadway

    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.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2006
    Location
    Slovenia
    Posts
    5

    Re: creating a roadway

    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

  4. #4
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,107

    Re: creating a roadway

    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:
    Code :
    glBegin(GL_LINE_STRIP);
      for (i = 0; i < vertexCount; i++)
        glVertex3f(xarray[i], yarray[i], zarray[i]);
    glEnd();
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2006
    Location
    Slovenia
    Posts
    5

    Re: creating a roadway

    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.

  6. #6
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,107

    Re: creating a roadway

    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.
    Code :
    int vertexCount;
    float* vertexArray;
    int indexCount;
    unsigned short* indexArray;
    Let's assume you want to render one quad that is made of two triangles:
    0--1
    | /|
    |/ |
    3--2
    The structure would have to contain the following data:
    Code :
    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:
    Code :
    int i, index;
    glBegin(GL_TRIANGLES);
    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);
    }
    glEnd();
    It would be even faster to render it if you use vertex arrays, but I'll leave that for later.

  7. #7
    Junior Member Newbie
    Join Date
    Sep 2006
    Location
    Slovenia
    Posts
    5

    Re: creating a roadway

    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
    --------------------
    NUM_POINTS;
    x1
    y1
    z1
    x2
    y2
    z2
    x3
    y3
    z3
    x4
    y4
    z4
    ....and so on
    EOF
    --------------------

    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???
    Cheers

  8. #8
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,107

    Re: creating a roadway

    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.

  9. #9
    Junior Member Newbie
    Join Date
    Sep 2006
    Location
    Slovenia
    Posts
    5

    Re: creating a roadway

    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()???)...
    cheers

  10. #10
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,107

    Re: creating a roadway

    Ok, I assume it's actually:
    Code :
    GLfloat* xarray;
    GLfloat* yarray;
    GLfloat* zarray;
    Rendering from such array would look like this:
    Code :
    glBegin(GL_QUADS);
    for (i = 0; i < vertexCount; i++)
      glVertex3f(xarray[i], yarray[i], zarray[i]);
    glEnd();
    The rest is up to you now. If you feel like you need some examples/tutorials then be sure to visit: http://nehe.gamedev.net/
    Cheers.

Similar Threads

  1. Creating JPG without a window
    By hexa in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 06-25-2008, 06:00 AM
  2. Creating a 2D Map
    By glguy in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-07-2006, 04:45 PM
  3. Creating a ChaseCamera in OGL & Creating multiple cameras
    By Nuno Miguel Silva in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-03-2004, 12:45 AM
  4. Creating a gui
    By in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 04-05-2002, 11:57 AM
  5. Need Help Creating a Scene
    By RDMustang in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 04-10-2001, 02:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean