PDA

View Full Version : SimpleOpenGlControl1_KeyDown



appdeveloper
10-12-2009, 08:07 AM
I'm trying to draw points on the SimpleOpenGlControl1 using the event SimpleOpenGlControl1_KeyDown.
Seems like when i draw the points on the control the points are drawn on diferent places of where i clicked. Anyone knows how can i solve this problem? this is part of the code:


Private Sub SimpleOpenGlControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SimpleOpenGlControl1.MouseDown
m_curPoints += 1
If m_curPoints = 1 Then
ReDim m_Points(m_curPoints)
Else
ReDim Preserve m_Points(m_curPoints)
End If
m_Points(m_curPoints - 1).X = e.X
m_Points(m_curPoints - 1).Y = e.Y
renderScene()
Me.Refresh()
End Sub


Private Sub renderScene()
Dim intContador As Integer

Gl.glClear(Gl.GL_COLOR_BUFFER_BIT Or Gl.GL_DEPTH_BUFFER_BIT)



Gl.glPushMatrix()

'Const viewAngle As Single = 103.0F
'Gl.glRotatef(0.0F, 1.0F, 0.2F, 0.0F)


Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture)
Gl.glBegin(Gl.GL_TRIANGLE_STRIP)
Gl.glColor3f(1.0F, 1.0F, 1.0F)

Gl.glTexCoord2f(-1.0, 0.0) : Gl.glVertex3f(0.0, m_size.Height, 0.0)
Gl.glTexCoord2f(-1.0, 1.0) : Gl.glVertex3f(0.0, 0.0, 0.0)
Gl.glTexCoord2f(0.0, 0.0) : Gl.glVertex3f(m_size.Width, m_size.Height, 0.0)
Gl.glTexCoord2f(0.0, 1.0) : Gl.glVertex3f(m_size.Width, 0.0, 0.0)
Gl.glEnd()

If m_curPoints > 0 Then
Gl.glPointSize(5)
Gl.glColor3f(1.0F, 0.0F, 0.0F)
For intContador = 0 To m_curPoints - 1
Gl.glBegin(Gl.GL_POINTS)
Gl.glVertex3f(m_Points(intContador).X, m_Points(intContador).Y, 0.0)
Gl.glEnd()
Next
End If

Gl.glPopMatrix()
Gl.glFlush()


End Sub

Stephen A
10-12-2009, 08:43 AM
Check your viewport and your projection matrix. WinForms uses a different coordinate system than OpenGL.

appdeveloper
10-13-2009, 12:54 AM
yes it does but i don't seem to be able to use it. I've searched with google and in the taoframework.com and althought there are some questions about this, no answer was given. I can't use
glutMouseFunc(processMouse)
like i already used for other OpenGL implementation because, for what it seems, this implementation doesn't support it. I even tried to change the coordinate like this:

Private Sub SimpleOpenGlControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SimpleOpenGlControl1.MouseDown
m_curPoints += 1
If m_curPoints = 1 Then
ReDim m_Points(m_curPoints)
Else
ReDim Preserve m_Points(m_curPoints)
End If
m_Points(m_curPoints - 1).X = SimpleOpenGlControl1.Width - e.X
m_Points(m_curPoints - 1).Y = SimpleOpenGlControl1.Height - e.Y
renderScene()
Me.Refresh()
End Sub

but didn't work. As far as i can understand, winforms 0,0 is at top left and the SimpleOpenGLControl start using 0,0 (considering Gl.glOrtho(0, 2500, 0, 2500, -4.0, 4.0)), at bottom left. But seem that even knowing this i'm not being able to fix it

appdeveloper
10-13-2009, 01:16 AM
Sotmething must be wrong with the logic used. So, if my winforms uses 0,0 at top left, and my SimpleOpenGLControl has 0,0 at bottom right, and my coordinate system is set at Gl.glOrtho(0, 2500, 0, 2500, -4.0, 4.0) then i have to make 2 convertions. One from winforms to the SimpleOpenGLControl, inverting the mouse coordinates and another convertion to my orthognatic coordinates. So i did this:

Private Sub SimpleOpenGlControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SimpleOpenGlControl1.MouseDown
m_curPoints += 1
If m_curPoints = 1 Then
ReDim m_Points(m_curPoints)
Else
ReDim Preserve m_Points(m_curPoints)
End If
m_Points(m_curPoints - 1).X = SimpleOpenGlControl1.Width - e.X
m_Points(m_curPoints - 1).Y = SimpleOpenGlControl1.Height - e.Y

m_Points(m_curPoints - 1).X = (2000 * m_Points(m_curPoints - 1).X) / SimpleOpenGlControl1.Width
m_Points(m_curPoints - 1).Y = (2000 * m_Points(m_curPoints - 1).Y) / SimpleOpenGlControl1.Height
renderScene()
Me.Refresh()
End Sub

And although it doesn't work, its much more close to what it should be. But what can i be doing wrong?

appdeveloper
10-13-2009, 01:32 AM
Found the Solution. I'll post here because i'm almost certain someone will need it when using this control:

Private Sub SimpleOpenGlControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SimpleOpenGlControl1.MouseDown
m_curPoints += 1
If m_curPoints = 1 Then
ReDim m_Points(m_curPoints)
Else
ReDim Preserve m_Points(m_curPoints)
End If
m_Points(m_curPoints - 1).Y = SimpleOpenGlControl1.Height - e.Y

m_Points(m_curPoints - 1).X = (2500 * e.X) / SimpleOpenGlControl1.Width

m_Points(m_curPoints - 1).Y = (2500 * m_Points(m_curPoints - 1).Y) / SimpleOpenGlControl1.Height
renderScene()
Me.Refresh()
End Sub