New to OpenGL, some issues

Started by
1 comment, last by Ian Murphy 10 years, 5 months ago

I am new to OpenGL (and graphics programming in general).

I am working through Daniel Schuller's 'C# Game Programming for Serious Game Creation'.

Some of the examples in the book are not behaving the way the book indicates. I want to figure out if I am doing something wrong or the book is.

I have a simple rotating triangle method:

float width=0.5;

Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
Gl.glPointSize(5.0f);
Gl.glRotated(_currentRotation, 0, 1, 0);
Gl.glBegin(Gl.GL_TRIANGLE_STRIP);
{
Gl.glColor4d(1.0, 0.0, 0.0, 0.5);
Gl.glVertex3d(-width, 0, 0);
Gl.glColor3d(0.0, 1.0, 0.0);
Gl.glVertex3d(width, 0, 0);
Gl.glColor3d(0.0, 0.0, 1.0);
Gl.glVertex3d(0, width, 0);
}
Gl.glEnd();
Gl.glFinish();
I also have a Setup2DGraphics function that gets called on startup and everytime the host form changes size:
private void Setup2DGraphics(double width, double height)
{
Gl.glViewport(0, 0, (int)width, (int)height);
double halfWidth = width / 2;
double halfHeight = height / 2;
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Gl.glOrtho(-halfWidth, halfWidth, -halfHeight, halfHeight, -100, 100);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
}
The problem I am having is that no matter what size I change the form to, it seems like the Triangle is being drawn to an openGL space that is -1,1,-1,1.
That is, if I set width on the triangle to 0.5, then I get a triangle that stretches up, left and right half the viewport size.
My understanding (and the way the book presents it) is that if I'm passing in the height and width of the form to Setup2DGraphics function, (using ClientSize.Height and ClientSize.Width properties), that glOrtho will essentially make a 1:1 mapping between the viewport and my form. But that is not happening. No matter the size of the form, the triangle scales to the window size even as the width stays constant at 0.5. If I make width, say, 50, then it stretches vastly off the viewport.
So what am I missing/doing wrong here?
Thanks in advance!
Advertisement

The obvious cause of the described effect is that the projection matrix isn't set. Common reasons are because the setup function isn't called, it is called before the window and the rendering context is created, or because the projection matrix is reset at some later point.

The obvious cause of the described effect is that the projection matrix isn't set. Common reasons are because the setup function isn't called, it is called before the window and the rendering context is created, or because the projection matrix is reset at some later point.

I think you are right. I've done some experimenting and it seems that is what is happening. I'll figure out why.

Thanks!

This topic is closed to new replies.

Advertisement