PDA

View Full Version : viewports not working



appdeveloper
10-13-2009, 12:28 AM
I'm trying to set 2 viewports and its not working. I belive i'm doing something wrong with the gluLookAt() but can't figure out what. What am i doing wrong?


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
m_curPoints = 0
Me.SimpleOpenGlControl1.InitializeContexts()

Gl.glClearColor(0.0F, 0.0F, 0.0F, 0.0F)
Gl.glMatrixMode(Gl.GL_PROJECTION)
Gl.glLoadIdentity()
Gl.glOrtho(0, 2500, 0, 2500, -4.0, 4.0)


Gl.glEnable(Gl.GL_TEXTURE_2D)
Dim mBitmap As Bitmap
mBitmap = New Bitmap("C:\as.bmp")


Gl.glGenTextures(1, texture)
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture)
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR)
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR)
m_size.Height = mBitmap.Height
m_size.Width = mBitmap.Width
Dim m_data As BitmapData = mBitmap.LockBits(New Rectangle(0, 0, mBitmap.Width, mBitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Glu.gluScaleImage(Gl.GL_RGB, mBitmap.Width, mBitmap.Height, Gl.GL_UNSIGNED_BYTE, m_data.Scan0, 1024, 1024, Gl.GL_UNSIGNED_BYTE, m_data.Scan0)


Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, 1024, 1024, 0, Gl.GL_RGB, Gl.GL_UNSIGNED_BYTE, m_data.Scan0)

End Sub



Private Sub renderScene()
Dim intContador As Integer

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



Gl.glPushMatrix()


Gl.glMatrixMode(Gl.GL_MODELVIEW)

Gl.glViewport(0, 0, 2000, 2500)
Gl.glLoadIdentity()
Glu.gluLookAt(0.0, 0.0, 0.0, 100.0, 100.0, 1.0, 0.0, 1.0, 0.0)


Gl.glMatrixMode(Gl.GL_PROJECTION)
Gl.glLoadIdentity()
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.glMatrixMode(Gl.GL_MODELVIEW)
Gl.glViewport(2000, 0, 500, 2500)
Gl.glLoadIdentity()
Glu.gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0)

Gl.glMatrixMode(Gl.GL_PROJECTION)
Gl.glLoadIdentity()
Gl.glBegin(Gl.GL_TRIANGLE_STRIP)
Gl.glColor3f(1.0F, 1.0F, 1.0F)

Gl.glTexCoord2f(-1.0, 0.0) : Gl.glVertex3f(500, 560, 0.0)
Gl.glTexCoord2f(-1.0, 1.0) : Gl.glVertex3f(500, 500, 0.0)
Gl.glTexCoord2f(0.0, 0.0) : Gl.glVertex3f(560, 560, 0.0)
Gl.glTexCoord2f(0.0, 1.0) : Gl.glVertex3f(560, 500, 500)
Gl.glEnd()



Gl.glPopMatrix()
Gl.glFlush()


End Sub


My thanks in advanced

overlay
10-13-2009, 05:01 AM
you applied gluLookAt on the MODELVIEW matrix instead of the PROJECTION matrix. For glOrtho you did it correctly.

appdeveloper
10-13-2009, 08:22 AM
Thank you for your answer!
Guess its not just that. Still doesn't work

marshats
10-14-2009, 10:18 AM
You were originally correct to apply gluLookAt to the MODELVIEW and not the PROJECTION matrix. Take a look at the post Help stamp out GL_PROJECTION abuse (http://www.sjbaker.org/steve/omniv/projection_abuse.html). It explains why you should use gluLookAt with the GL_MODELVIEW stack. The GL_PROJECTION matrix should only be used with the limited set; gluPerspective/glFrustum/glOrtho/gluOrtho2.

Gl.glViewport takes arguments in pixel units. Does your monitor have 2500x2500 pixel resolution?



Gl.glMatrixMode(Gl.GL_MODELVIEW)
Gl.glViewport(2000, 0, 500, 2500)
Gl.glLoadIdentity()
Glu.gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0)

Gl.glMatrixMode(Gl.GL_PROJECTION)
Gl.glLoadIdentity()
Gl.glBegin(Gl.GL_TRIANGLE_STRIP)
Gl.glColor3f(1.0F, 1.0F, 1.0F)

Gl.glTexCoord2f(-1.0, 0.0) : Gl.glVertex3f(500, 560, 0.0)
Gl.glTexCoord2f(-1.0, 1.0) : Gl.glVertex3f(500, 500, 0.0)
Gl.glTexCoord2f(0.0, 0.0) : Gl.glVertex3f(560, 560, 0.0)
Gl.glTexCoord2f(0.0, 1.0) : Gl.glVertex3f(560, 500, 500)
Gl.glEnd()

The above code does suggest you are misusing gluLookAt. You place the camera at 0,0,0 and look towards (0,0,1). Then you proceed to draw draw a triangle contained in the z=0 plane which the camera isn't even looking at. If you want to see the triangle then something like the following might make more sense


Glu.gluLookAt(530.0, 530.0, 10.0, 530.0, 530.0, 0.0, 0.0, 1.0, 0.0)


Also why are you setting the PROJECTION to identity inside renderScene() function? You are basically undoing the ortho setting Gl.glOrtho(0, 2500, 0, 2500, -4.0, 4.0) whenver you do this. If I remember correctly this equates to glOrtho(-1, 1, -1, 1, -1.0, 1.0) which is not what you want. Why not simply remove the two instances inside of renderSecene() of the form


Gl.glMatrixMode(Gl.GL_PROJECTION)
Gl.glLoadIdentity()


Finally, your Gl.glPushMatrix() + Gl.glPopMatrix() pair is applied to your current matrix; is it the modelview or projection matrix? It is not at all clear which based on the usage in renderScene(). Usually you set your ModelMatrix _then_ use the push/pop pair as you rotate/translate/scale different objects in the scene. Very seldom it the push/pop pair ever applied to anything but the modelview matrix. It appears these aren't even being used in your code. Do you really need the push pop pair?