Hello.
I have problem with my application. When I click left mouse button and move with mouse, it change translation. When I click right mouse button and move with mouse, it change rotation. When I press ‘a’ and click, gluSphere will be draw at mouse coordinates in openGL. Drawing , rotation and translation work fine, until i rotate scene. After rotation calculate false coordinates, so spheres will draw at a strange places. I read a lot of tutorials and forums, but I still dont know where I do mistake. I make it in C#.
Here is a part of my code:
Mouse events:
private void openGL_MouseUp(object sender, MouseEventArgs e)
{
BodGL bod;
if (((AtrBod == true) || (BasBod == true) || (ListBod == true)) & (MouseD == 0))
{
bod = new BodGL();
bod.Quad = GLU.gluNewQuadric();
bod.bod = new clsBod();
bod.bod.Pozicia = openGLPos(e.Location.X, e.Location.Y);
.
.
.
function openGLPos
private Vektor openGLPos(int x, int y)
{
double outX, outY, outZ;
float z = zoom/100;
Vektor win = new Vektor((float)x, (float)viewport[3] - y, z);
unsafe
{
GL.glReadPixels(x, (int)win.Y, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, (&outZ));
}
GL.gluUnProject((float)win.X, (float)win.Y, outZ, modelview, projection, viewport, out outX, out outY, out outZ);
Vektor temp = new Vektor(outX - xpos / 100, outY - ypos / 100, outZ - zoom / 100); // outZ - zoom / 100
return temp;
}
draw procedure
public void glDraw()
{
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
InitGLContext();
GL.glLoadIdentity();
GL.glPushMatrix();
GL.glTranslatef(xpos, ypos, zoom);
GL.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
GL.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
foreach (BodGL B in poleBodov)
{
GL.glTranslatef((float)(B.bod.Pozicia.X * 100), (float)(B.bod.Pozicia.Y * 100), (float)(B.bod.Pozicia.Z * 100));
if (Math.Round((B.bod.Pozicia.Z * 100 + 10) + zoom) == 0) B.bod.Farba = Color.FromArgb(B.bod.Farba.R, B.bod.Farba.G, B.bod.Farba.B);
VykresliAtrBod(B.Quad, B.bod.Priemer, B.bod.Farba, B.bod.Fill);
if (Math.Round((B.bod.Pozicia.Z * 100 + 10) + zoom) == 0) B.bod.Farba = Color.FromArgb(B.bod.Farba.R,B.bod.Farba.G, B.bod.Farba.B);
GL.glTranslatef(-(float)(B.bod.Pozicia.X * 100), -(float)(B.bod.Pozicia.Y * 100), -(float)(B.bod.Pozicia.Z * 100));
}
GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelview);
GL.glGetDoublev(GL.GL_PROJECTION_MATRIX, projection);
GL.glGetIntegerv(GL.GL_VIEWPORT, viewport);
GL.glPopMatrix();
}
matrics
private double[] modelview = new double[16];
int[] viewport = new int[4];
double[] projection = new double[16];
are as global variable in my class.
Please help me and sorry about my english.
Thanks.