using Projection in OpenGL using C#?

1.9k views Asked by At

For those who don't know, Tao.opengl, Tao.freeglut are required for the C# console application.

Now, for my problem: my tutor asked us to draw 4 rectangles, so I drew them successfully.

and copy/paste the other codes (which our tutor didn't explain due to time shortage)

here's the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.FreeGlut;
using Tao.OpenGl;

namespace Homework_1
{
    class Triangles
    {
        static void display()
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);

            Gl.glColor3f(1.0f,1.0f,1.0f);
            Gl.glBegin(Gl.GL_POLYGON);
            Gl.glVertex3f(-1.0f,-1.0f,0.0f);
            Gl.glVertex3f(1.0f, -1.0f, 0.0f);
            Gl.glVertex3f(1.0f, 1.0f, 0.0f);
            Gl.glVertex3f(-1.0f, 1.0f, 0.0f);
            Gl.glEnd();

            Gl.glColor3f(1.0f, 1.0f, 1.0f);
            Gl.glBegin(Gl.GL_POLYGON);
            Gl.glVertex3f(2.0f, -1.0f, 0.0f);
            Gl.glVertex3f(4.0f, -1.0f, 0.0f);
            Gl.glVertex3f(4.0f, 1.0f, 0.0f);
            Gl.glVertex3f(2.0f, 1.0f, 0.0f);
            Gl.glEnd();

            Gl.glColor3f(1.0f, 1.0f, 1.0f);
            Gl.glBegin(Gl.GL_POLYGON);
            Gl.glVertex3f(2.0f, 2.0f, 0.0f);
            Gl.glVertex3f(4.0f, 2.0f, 0.0f);
            Gl.glVertex3f(4.0f, 4.0f, 0.0f);
            Gl.glVertex3f(2.0f, 4.0f, 0.0f);
            Gl.glEnd();

            Gl.glColor3f(1.0f, 1.0f, 1.0f);
            Gl.glBegin(Gl.GL_POLYGON);
            Gl.glVertex3f(-1.0f, 2.0f, 0.0f);
            Gl.glVertex3f(1.0f, 2.0f, 0.0f);
            Gl.glVertex3f(1.0f, 4.0f, 0.0f);
            Gl.glVertex3f(-1.0f, 4.0f, 0.0f);
            Gl.glEnd();


            Gl.glFlush();

        }

        static void init()
        {

            Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Gl.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
        }

        public static void Main(String[] argv)
        {
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_SINGLE | Glut.GLUT_RGB);
            Glut.glutInitWindowSize(1000, 1000);
            Glut.glutInitWindowPosition(100, 100);
            Glut.glutCreateWindow("hello");
            init();
            Glut.glutDisplayFunc(display);
            Glut.glutMainLoop();
        }
    }
}

When running the application, all it shows is white, so I'm 100 % sure that the error is inside the init() method I copied and pasted, the line

 Gl.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); 

I'm 100 % sure Gl.glOrtho's values must be changed, but the problem is, I don't know how to use it.

Please correct my error, so it can show the 4 triangles I've drawn, and a small explanation will make it more obvious and acceptable.

2

There are 2 answers

3
Christian Rau On BEST ANSWER

Simply spoken the glOrtho call generally determines the area of your world that is visible. So in your case you you only see the [0,1]-square, which is completely covered by the first (white) polygon (that is the [-1,1]-square). So to see all your polygons, you should use e.g.

glOrtho(-1.0, 4.0, -1.0, 4.0, -1.0, 1.0);  //or maybe with some margin of +/- 0.1

This all gets more difficult when you use a modelview matrix that is not identity or a perspective projection (istead of an orthographic one). I advice you to look a bit more into OpenGL's transformation pipeline for more insight. Maybe the answers to this question help with that.

You also seem to have forgotten to make a call to

glViewport(0, 0, 1000, 1000);

which determines the region of the framebuffer your view is rendered into and whose extents should usually match the extents of your window. By default a context's viewport matches the size of the window it's attached to, but it is good practice to set the viewport explicitly, especially when the window gets resized, as in this case the viewport is not updated automatically.

And by the way, you don't need to set the color for every polygon if they're all colored the same, as OpenGL is a state machine.

1
pikzen On

glVertex3 is so deprecated your teacher shouldn't even acknowledge its existence, but well.

void glOrtho(GLdouble   left,
             GLdouble   right,
             GLdouble   bottom,
             GLdouble   top,
             GLdouble   nearVal,
             GLdouble   farVal);

The first two parameters specify the coordinates for the Left and Right clipping planes. The third and fourth parameters specify the coordinates for the Bottom and Top Clipping planes.

With your current glOrtho, the first rectangle covers up the entire screen, and the others cover the screen, also. Combined with your GlColor3f setting the drawing color to White, your entire screen becomes white. You don't need to respecify it, by the way.

A way of having it work would be to have a glOrtho like this :

 Gl.glOrtho(-5.0, 5.0, -5.0, -5.0, -1.0, 1.0); 

You could also modifiy your glVertex methods to draw a smaller rectangle.