PDA

View Full Version : Map of US states



nastyn8
09-11-2012, 12:11 AM
Hi everyone,

I am new to OpenGL, and I need some advice for a project I am working on for one of my classes. My objective is to create a map of new england. It should look something like this picture:
http://wikitravel.org/upload/shared//thumb/c/c3/Map-USA-New_England01.png/300px-Map-USA-New_England01.png

I've been able to create some flags for the first part of our assignments, however I accomplished this by only using normal shapes.
http://s16.postimage.org/ny3rmt0tx/Screen_Shot_2012_09_11_at_3_04_31_AM.png

This is my code for the flag:



#include <iostream>
//#include <GL/glut.h>
#include <glut/glut.h>

using namespace std;

void display()
{
double sin(double), cos(double);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(0.0, 0.80, 0.40);
glVertex2f(-1.0, -1.0);
glVertex2f(-1.0, 1.0);
glVertex2f(0.0, 1.0);
glVertex2f(0.0, -1.0);
glEnd();

glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
for (int i=0; i<100; i++)
{
glVertex2f(0.29*sin(2*3.1415926*i/100), 0.5*cos(2*3.1415926*i/100));
}
glEnd();

glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
for (int i=0; i<100; i++)
{
glVertex2f(0.09 + 0.22*sin(2*3.1415926*i/100), 0.37*cos(2*3.1415926*i/100));
}
glEnd();


glColor3f(0.0, 0.80, 0.40);
glBegin(GL_POLYGON);
for (int i=0; i<100; i++)
{
glVertex2f(0.15*sin(-3.1415926*i/100), 0.33*cos(-3.1415926*i/100));
}
glEnd();


glColor3f(1.0, 0.0, 0.0);

glBegin(GL_TRIANGLES);

glVertex2f(0.0, 0.14);

glVertex2f(0.28, 0.0);
glVertex2f(0.1, -0.1);
glEnd();

glBegin(GL_TRIANGLES);

glVertex2f(0.17, 0.25);

glVertex2f(0.1, 0.05);
glVertex2f(0.17, 0.02);
glEnd();

glBegin(GL_TRIANGLES);

glVertex2f(0.0, -0.18);

glVertex2f(0.1, 0.0);
glVertex2f(0.12, -0.1);
glEnd();

glBegin(GL_TRIANGLES);

glVertex2f(0.17, 0.0);

glVertex2f(0.1, -0.1);
glVertex2f(0.16, -0.25);
glEnd();





glFlush();

}

void init()
{
glClearColor (1.0, 1.0, 1.0, 0.0);

glColor3f(1.0, 1.0, 1.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);



}


int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 300);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
glutDisplayFunc(display);
init();
glutMainLoop();


}


My question is how can I create this map of New England, because most states are not normal shapes? Is there a tutorial someone could direct me too that would help me? Any advice would be greatly appreciated.

Thank you!

Dark Photon
09-12-2012, 05:17 PM
My question is how can I create this map of New England, because most states are not normal shapes? Is there a tutorial someone could direct me too that would help me? Any advice would be greatly appreciated.
If you want to draw a filled 2D area of a state, then ultimately the GPU is going to fill it with triangles. So you might as well feed triangles to OpenGL. To do that, you take the outline of the state and tessellate it into triangles (or find such a pre-tessellated representation online).

If you just want to draw the outline, just take the outline of the state and draw each segment with lines primitives.

Carmine
09-13-2012, 10:23 AM
My question is how can I create this map of New England, because most states are not normal shapes? Is there a tutorial someone could direct me too that would help me? Any advice would be greatly appreciated.

Does the map HAVE to be made up of polygons?
Have you considered texture mapping the image of New England onto a quad?
This is how I usually do maps in OpenGL.

Rosario Leonardi
09-13-2012, 04:18 PM
Draw your shape in some 2d vector graphic editor (inkscape, illustrator) end export in illustrator format
http://mercator.elte.hu/~saman/hu/okt/AI7FileFormat.pdf
illustrator is trivial to parse if you only need only point shape.
Also .ps format is easy to parse.

Then, apply some ear cutting algorithm to triangulate the shape
http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/97/Ian/cutting_ears.html
Save somewhere the triangles.

Load the triangle in your program to create the map. I suggest to load the flags as texture, is quite difficult to render them as polygon. Obviously the illustrator format let you export color too.