opengl2drenderglut.obj

Render vertex data of .obj file with openGL


so i want to render a 2D Curve. The data for this is provided through Blender as an .obj File. I also have an object loader which retrieves succesfully the vertices of the file.

Here is my window/display limits:

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0,0);
glutCreateWindow("examplke");
gluOrtho2D(0.0, 600.0, 0.0, 600.0) // Params: Left, right, bottom, top
glutDisplayFunc(renderDisplay);

And my display function:

void renderDisplay() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < vertices.size(); i += 3) {
    glVertex2f(vertices[i], vertices[i+2]);
}
glEnd();
glFlush();

And my .obj file Data:

v -6.010289 0.000000 0.980260
v -5.443917 0.000000 0.413888
v -4.899328 0.000000 0.000000
v 4.899328 0.000000 0.000000
v 5.487484 0.000000 0.326753
v 5.814237 0.000000 0.740641
l 1 2
l 2 3
l 3 4
l 4 5
l 5 6

So the question here is how exactly do i have to transform the vertex data to render my object correctly in relation to my display limits (Example: 600 x 600 Pixel). Im aware i have to do some GL_PROJECTION and GL_MODELVIEW Transformations. But even after reasearching i dont really understand how this can be done. Or might there be a way doing this via the Blender software?


Solution

  • You need to set the projection depending on the geometry. gluOrtho2D defines a 2 dimensional area that is projected onto the viewport. Coordinates in a different range require a different projection. The projected area should contain the minimum and maximum of the vertex coordinates.
    Your coordinates are in range -6 to 6. So change the projection, e.g.:

    gluOrtho2D(0.0, 600.0, 0.0, 600.0);

    gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
    

    Alternatively you can scale and translate the coordinates.