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