简体   繁体   中英

OpenGL Fitting Viewer to Object (written in VB .NET but C# answers will work too)

Background

I am attempting to make a very simple CAD viewer. I have chosen to start with .STL files which, for those of you who don't know, are just a bunch of triangles that will then form the part on screen. When outputting from most CAD software, it can be output as a text file version if STL. This lists all of the points:

solid
facet normal -7.8662990E-02 +8.3699658E-01 -5.4152455E-01
outer loop      
  vertex   +3.3529416E+03 -3.7456115E+02 +8.5293264E+02
  vertex   +3.3530518E+03 -3.7467327E+02 +8.5274334E+02
  vertex   +3.3527449E+03 -3.7467680E+02 +8.5278246E+02
endloop
endfacet
facet normal -1.7741169E-02 +8.8266388E-01 -4.6966979E-01
outer loop      
  vertex   +3.3527449E+03 -3.7467680E+02 +8.5278246E+02
  vertex   +3.3522178E+03 -3.7464933E+02 +8.5285399E+02
  vertex   +3.3524442E+03 -3.7463108E+02 +8.5287975E+02
endloop
endfacet

and so forth.. I can then basically loop through all the points and create triangles:

Dim i As Integer = 0
        Do Until i + 2 >= ListView1.Items.Count
            Gl.glPushMatrix()
            Gl.glBegin(Gl.GL_TRIANGLES)

            Gl.glColor3f(1.0, 0.0, 0.0)
            Gl.glVertex3f(Convert.ToSingle(ListView1.Items.Item(i).SubItems(0).Text), Convert.ToSingle(ListView1.Items.Item(i).SubItems(1).Text), Convert.ToSingle(ListView1.Items.Item(i).SubItems(2).Text))

            Gl.glColor3f(1.0, 0.0, 0.0)
            Gl.glVertex3f(Convert.ToSingle(ListView1.Items.Item(i + 1).SubItems(0).Text), Convert.ToSingle(ListView1.Items.Item(i + 1).SubItems(1).Text), Convert.ToSingle(ListView1.Items.Item(i + 1).SubItems(2).Text))


            Gl.glColor3f(1.0, 0.0, 0.0)
            Gl.glVertex3f(Convert.ToSingle(ListView1.Items.Item(i + 2).SubItems(0).Text), Convert.ToSingle(ListView1.Items.Item(i + 1).SubItems(1).Text), Convert.ToSingle(ListView1.Items.Item(i + 2).SubItems(2).Text))

            Gl.glEnd()
            Gl.glPopMatrix()

            i = i + 3
        Loop

Problem

My method seems to work when I manually put in my triangles, but when doing it as shown above, I can't find where they are. I believe this is one of two reasons. Either my world is not big enough for the locations specified since some are in the range of (X=3352, Y=-374, Z=852), when I am starting off viewing (X=0, Y=0, Z=0),

How do I know, or change, the size of my world if this could possibly be the problem?

Here is what I use for setting up my world so far:

    GLCtrl.InitializeContexts()
    Gl.glClearColor(0, 0, 0, 0)

    Gl.glMatrixMode(Gl.GL_PROJECTION)
    Gl.glLoadIdentity()                     
    Gl.glOrtho(-1, 1, -1, 1, -1, 1)         
    Gl.glMatrixMode(Gl.GL_MODELVIEW)       
    Gl.glLoadIdentity()                    

    GLCtrl.Focus()

    'start the gameloop

    gameLoop()

If this seems to not be the problem, then it may be that I am not going to my triangles correctly? I currently go to them using translate:

 Gl.glTranslatef(view_location.x, view_location.y, view_location.z)

and I use coordinates close to the ones I listed above, and try to zoom and pan but still can't find anything.

I will actually answer my own question. The answer is simply that the Gl.glTranslatef is inverse from what I thought. The solution is:

Gl.glTranslatef(-view_location.x, -view_location.y, -view_location.z)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM