开放式GL照明困难 [英] Open GL Lighting Difficulties

查看:101
本文介绍了开放式GL照明困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

目前,我基本上是在制作CAD查看器,它将在更大的系统中使用.我已经阅读了大量有关学习Open GL的信息,并且我已经走得很远了.我可以导入文件,显示文件并使用键盘控件进行导航.我正在努力理解并设置适当的照明.

Currently I am esentially making a CAD viewer which will be used in a larger system. I have been reading a ton of information about learning Open GL and I am quite far along. I can import the file, display it, and navigate using keyboard controls. I am trying hard to understand and set up proper lighting now.

问题

经过一番讨论之后,我意识到我没有加载着色器,这就是我遇到问题的原因.在进行一些研究时,我发现了一些用C编写的着色器文件,fragment.glsl和vertex.glsl.由于我是用C#/VB.net编写代码的,所以这会成为问题吗?我不这么认为.

After some discussion bellow, I realize that I don't have a shader loaded which is why I am having my problems. In doing some research I have found some shader files, fragment.glsl and vertex.glsl which are written in C. Will this be a problem since I am writing my code in C#/ VB.net? I don't think so.

现在我在编译着色并将着色器链接到我的代码时遇到问题.我找到了一个方便的编译源:

Now I am having a problem with compiling the shading, and linking the shaders to my code. I found a handy source for compiling:

http://pages.cpsc.ucalgary. ca/〜brosz/wiki/pmwiki.php/CSharp/08022008

在本文中,创建了一个用于编译着色器的函数.编译器需要一个流,然后将其传递到编译器.我具有创建的此功能,用于将着色器读取到流中:

In this article, a function is created to compile a shader. The compiler requires a stream to which is then passed on to the compiler. I have this function I created to read the shader to a stream:

Public Function path_to_stream(ByVal path As String) As System.IO.Stream
    Dim bData As Byte()
    Dim br As System.IO.BinaryReader = New System.IO.BinaryReader(System.IO.File.OpenRead(path))
    bData = br.ReadBytes(br.BaseStream.Length)
    Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(bData, 0, bData.Length)
    ms.Write(bData, 0, bData.Length)
    Return ms
End Function

在我的绘画功能即将结束时,我尝试使用着色器功能进​​行加载:

Towards the end of my draw function I try to load using the shader function:

vertexShader = path_to_stream(Application.StartupPath & "\default.frag")
    fragmentShader = path_to_stream(Application.StartupPath & "\default.vert")

    vs_object = Helpers.OpenGLUtilities.createAndCompileShader(vertexShader, Gl.GL_VERTEX_SHADER)
    fs_object = Helpers.OpenGLUtilities.createAndCompileShader(fragmentShader, Gl.GL_FRAGMENT_SHADER)

    program = Helpers.OpenGLUtilities.createAttachLinkShaderProgram(vs_object, fs_object)

但是我收到一条错误消息,指出无法编译着色器.知道我要去哪里错了吗?

I get an error message however that the shader could not be compiled. Any idea where I am going wrong?

这是我的模型的样子:

看起来还不错,但还不算好.

Looks decent but not great..

看起来糟透了..

就像坏..

更新

自从下面被询问以来,这就是我进行三角形实际绘制的方法.从中获取信息的listview基本上是STL文件,该文件包含法线向量,然后是三个点,它们组成一个三角形.

Since it was asked below, this is my method for doing the actual drawing of my triangles. The listview it is getting the information from is basically the STL file, which contains the normal vector and then three points which make a triangle.

        Gl.glRotatef(rotX, 1.0F, 0.0F, 0.0F)
        ' Rotate on x
        Gl.glRotatef(rotY, 0.0F, 1.0F, 0.0F)
        ' Rotate on y
        Gl.glRotatef(rotZ, 0.0F, 0.0F, 1.0F)
        ' Rotate on z
        Gl.glTranslatef(X, Y, Z)

        'Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_FILL)
        Gl.glBegin(Gl.GL_TRIANGLES)
        Gl.glColor3f(part_color.R, part_color.G, part_color.B)


        Dim i As Integer = 0

        Do Until i + 4 >= ListView1.Items.Count
            Gl.glNormal3f(ListView1.Items.Item(i).SubItems(0).Text, ListView1.Items.Item(i).SubItems(1).Text, ListView1.Items.Item(i).SubItems(2).Text)

            Gl.glVertex3f(ListView1.Items.Item(i + 1).SubItems(0).Text - avgx, ListView1.Items.Item(i + 1).SubItems(1).Text - avgy, ListView1.Items.Item(i + 1).SubItems(2).Text - avgz)
            Gl.glVertex3f(ListView1.Items.Item(i + 2).SubItems(0).Text - avgx, ListView1.Items.Item(i + 2).SubItems(1).Text - avgy, ListView1.Items.Item(i + 2).SubItems(2).Text - avgz)
            Gl.glVertex3f(ListView1.Items.Item(i + 3).SubItems(0).Text - avgx, ListView1.Items.Item(i + 3).SubItems(1).Text - avgy, ListView1.Items.Item(i + 3).SubItems(2).Text - avgz)
            i = i + 4
        Loop
        Gl.glEnd()

我的主图子:

Private Sub display()


    Gl.glClear(Gl.GL_COLOR_BUFFER_BIT)
    ' Clear the Color Buffer 
    Gl.glPushMatrix()
    ' It is important to push
    ' the Matrix before calling
    ' glRotatef and glTranslatef
    Gl.glRotatef(rotX, 1.0F, 0.0F, 0.0F)
    ' Rotate on x
    Gl.glRotatef(rotY, 0.0F, 1.0F, 0.0F)
    ' Rotate on y
    Gl.glRotatef(rotZ, 0.0F, 0.0F, 1.0F)
    ' Rotate on z
    Gl.glTranslatef(X, Y, Z)
    ' Translates the screen
    ' left or right, up or down
    ' or zoom in zoom out
    ' Draw the positive side of the lines x,y,z
    Gl.glBegin(Gl.GL_LINES)
    'Gl.glColor3f(0.0F, 1.0F, 0.0F)
    Gl.glColor4f(0.0F, 1.0F, 0.0F, 1.0F)

    ' Green for x axis
    Gl.glVertex3f(0.0F, 0.0F, 0.0F)
    Gl.glVertex3f(10.0F, 0.0F, 0.0F)
    Gl.glColor3f(1.0F, 0.0F, 0.0F)
    ' Red for y axis
    Gl.glVertex3f(0.0F, 0.0F, 0.0F)
    Gl.glVertex3f(0.0F, 10.0F, 0.0F)
    Gl.glColor3f(0.0F, 0.0F, 1.0F)
    ' Blue for z axis
    Gl.glVertex3f(0.0F, 0.0F, 0.0F)
    Gl.glVertex3f(0.0F, 0.0F, 10.0F)
    Gl.glEnd()

    ' Dotted lines for the negative sides of x,y,z
    Gl.glEnable(Gl.GL_LINE_STIPPLE)
    ' Enable line stipple to
    ' use a dotted pattern for
    ' the lines
    Gl.glLineStipple(1, &H101)
    ' Dotted stipple pattern for
    ' the lines
    Gl.glBegin(Gl.GL_LINES)
    Gl.glColor3f(0.0F, 1.0F, 0.0F)
    ' Green for x axis
    Gl.glVertex3f(-10.0F, 0.0F, 0.0F)
    Gl.glVertex3f(0.0F, 0.0F, 0.0F)
    Gl.glColor3f(1.0F, 0.0F, 0.0F)
    ' Red for y axis
    Gl.glVertex3f(0.0F, 0.0F, 0.0F)
    Gl.glVertex3f(0.0F, -10.0F, 0.0F)
    Gl.glColor3f(0.0F, 0.0F, 1.0F)
    ' Blue for z axis
    Gl.glVertex3f(0.0F, 0.0F, 0.0F)
    Gl.glVertex3f(0.0F, 0.0F, -10.0F)
    Gl.glEnd()

    Gl.glDisable(Gl.GL_LINE_STIPPLE)
    ' Disable the line stipple
    Gl.glPopMatrix()
    ' Don't forget to pop the Matrix

    Gl.glEnable(Gl.GL_LIGHTING)

    Light1Position = {0.0F, 0.0F, 100.0F, 0.0F}

    ' Enable Default Light
    Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_AMBIENT, Light1Ambient)
    Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_DIFFUSE, Light1Diffuse)
    Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_POSITION, Light1Position)
    Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_SPECULAR, Light1Specular)

    ' Enable Lighting
    Gl.glEnable(Gl.GL_LIGHT1)




    draw_extras()


    Gl.glEnable(Gl.GL_COLOR_MATERIAL)
    Gl.glColorMaterial(Gl.GL_FRONT, Gl.GL_AMBIENT_AND_DIFFUSE)
    Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_SPECULAR, MaterialSpecular)
    Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_SHININESS, SurfaceShininess)

    Gl.glEnable(Gl.GL_COLOR_MATERIAL)

    Glut.glutSwapBuffers()
End Sub

我的draw_extra子:

My draw_extra sub:

Public Sub draw_extras()
    Dim texture As UInteger() = New UInteger(0) {}


    If allowdraw = True Then

        find_center_of_part()


        Gl.glPushMatrix()


        Gl.glRotatef(rotX, 1.0F, 0.0F, 0.0F)
        ' Rotate on x
        Gl.glRotatef(rotY, 0.0F, 1.0F, 0.0F)
        ' Rotate on y
        Gl.glRotatef(rotZ, 0.0F, 0.0F, 1.0F)
        ' Rotate on z
        Gl.glTranslatef(X, Y, Z)

        Gl.glBegin(Gl.GL_TRIANGLES)
        Gl.glColor3f(part_color.R, part_color.G, part_color.B)


        Dim i As Integer = 0

        Do Until i + 4 >= ListView1.Items.Count
            Gl.glNormal3f(ListView1.Items.Item(i).SubItems(0).Text, ListView1.Items.Item(i).SubItems(1).Text, ListView1.Items.Item(i).SubItems(2).Text)

            Gl.glVertex3f(ListView1.Items.Item(i + 1).SubItems(0).Text - avgx, ListView1.Items.Item(i + 1).SubItems(1).Text - avgy, ListView1.Items.Item(i + 1).SubItems(2).Text - avgz)
            Gl.glVertex3f(ListView1.Items.Item(i + 2).SubItems(0).Text - avgx, ListView1.Items.Item(i + 2).SubItems(1).Text - avgy, ListView1.Items.Item(i + 2).SubItems(2).Text - avgz)
            Gl.glVertex3f(ListView1.Items.Item(i + 3).SubItems(0).Text - avgx, ListView1.Items.Item(i + 3).SubItems(1).Text - avgy, ListView1.Items.Item(i + 3).SubItems(2).Text - avgz)
            i = i + 4
        Loop
        Gl.glEnd()




        ' Disable the line stipple
        Gl.glPopMatrix()

    End If

End Sub

这是STL文件的样子:

Here is what the STL file looks like:

它从法线向量开始,然后是三角形的三个点,然后是另一个法线向量,依此类推.那就是我循环所要做的全部.

It starts with the normal vector, then is followed by three points for the triangle, then another normal vector, and so forth. That is all my loop is doing.

下一个更新

我尝试将灯的代码更改为:

I tried changing the code for the light to be:

 Gl.glEnable(Gl.GL_LIGHTING)

    Light1Position = {X, Y, Z, 0.0F}

    ' Enable Default Light
    Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_AMBIENT, Light1Ambient)
    Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_DIFFUSE, Light1Diffuse)
    Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_POSITION, Light1Position)
    Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_SPECULAR, Light1Specular)


    ' Enable Lighting
    Gl.glEnable(Gl.GL_LIGHT1)

X,Y,Z变量应与摄影机匹配,因为在翻译摄影机的代码中较早:

The X, Y, Z variables should match the camera since earlier in the code it translates the camera:

Gl.glTranslatef(X, Y, Z)

找不到光亮

任何指导都将大为适用.我将其标记为C#和VB .NET,因为这两个答案都对我有用.

Any guidance at all would be greatly appreaiciated. I tagged this as both C# and VB .NET since either answers would work for me.

推荐答案

您还可以编写自己的着色器来渲染实际对象,该对象将计算漫射照明,如果需要的话,甚至可以反射.从那里,您还可以进行纹理映射,阴影,法线等.

You could also write your own shader to render your actual object, which will calculate diffuse lighting, maybe even specular if you want. From there, you could also do texture mapping, shadows, normals, etc.

在着色器代码这里,您可以在互联网上找到有关将GLSL与C#结合使用的教程.

There is an excellent tutorial by OpenGL on the shader code here, and you can find a tutorial on using GLSL with C# on the internet.

这篇关于开放式GL照明困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆