Search code examples
c#wpfopenglgraphicssharpgl

Simple code for transformation in OpenGL/SharpGL (without matrix theory lessons)


I have an OpenGL .NET WPF (SharpGL) app which draws small squares in immediate mode, they are really 3D:

float z = -200f;
float farz = 400f;
...

// init
gl.ShadeModel(OpenGL.GL_SMOOTH);
gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.ClearDepth(1.0f);
gl.Disable(OpenGL.GL_DEPTH_TEST);
gl.Enable(OpenGL.GL_BLEND);
gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE);
gl.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_NICEST);
gl.Hint(OpenGL.GL_POINT_SMOOTH_HINT, OpenGL.GL_NICEST);

// resize
gl.MatrixMode(OpenGL.GL_PROJECTION);
gl.LoadIdentity();
gl.Perspective(45.0, ratio, 0.1, farz);
gl.MatrixMode(OpenGL.GL_MODELVIEW);
gl.LoadIdentity();

// draw
gl.Begin(OpenGL.GL_TRIANGLE_STRIP);
gl.TexCoord(1, 1); gl.Vertex(x + 0.5f, y + 0.5f, z);
gl.TexCoord(0, 1); gl.Vertex(x - 0.5f, y + 0.5f, z);
gl.TexCoord(1, 0); gl.Vertex(x + 0.5f, y - 0.5f, z);
gl.TexCoord(0, 0); gl.Vertex(x - 0.5f, y - 0.5f, z);
gl.End();

I have buttons to "zoom" and "pan".

Zoom is currently alters z by +/- 10 between 0 and -400 which is good enough.

I tried to implement pan by using LookAt but I cant make it work. I did put it in resize method - is this the right place for it?

Is it possible to have a single line code to move the eye by +/- 5 left/right/up/down without enduring a matrix theory lesson? Id appreciate it.


Solution

  • Use gl.Translate to translate the model view matrix. e.g:

    gl.MatrixMode(OpenGL.GL_MODELVIEW);
    gl.LoadIdentity();
    gl.Translate(-5, 0, 0)