1

Background

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.

Problem

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?

Here is what my model looks like:

http://i.imgur.com/UvN4Tj8.png

Looks decent but not great..

http://i.imgur.com/G2TJt12.png

Looks terrible..

http://i.imgur.com/SXZ7C5S.png

Just as bad..

Update

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()

My main drawing sub:

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

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

Here is what the STL file looks like:

enter image description here

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.

Next Update

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)

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

Gl.glTranslatef(X, Y, Z)

No light to be found:

enter image description here

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

4

3 回答 3

2

您还可以编写自己的着色器来渲染您的实际对象,这将计算漫反射照明,如果您愿意,甚至可能是镜面反射。从那里,您还可以进行纹理映射、阴影、法线等。

OpenGL 有一个关于着色器代码的优秀教程您可以在 Internet 上找到关于使用 GLSL 和 C# 的教程。

于 2015-07-22T04:46:49.520 回答
1

我不同意这里的答案,如果您的目标是拥有简单的照明并且您刚刚开始学习 OGL,那么您不应该编写自己的着色器。OpenGL 已经内置了为您处理这个问题的功能,我相信您已经在许多教程中看到过。尽管不再支持此功能,尽管它有明显的好处,这促使许多人谴责它,但它将使您的学习过程更加轻松。

既然有人提到您启用了照明,我想知道您目前是如何绘制模型的。如果您使用立即模式(为每个顶点调用 glVertex),您是否还调用 glNormal 来提供法线?如果没有法线,OpenGL 没有关于如何计算模型光照的信息。如果您不使用即时模式,请准确说明您的操作。提交你的代码会有很大帮助,因为我不知道你目前在做什么,而 OpenGL 有一百万种方法让你犯错。

另外:您链接的教程中的着色器代码对于初学者来说似乎过于复杂,您不应该使用它。

我想将此放在评论而不是答案中,但我的声誉目前不足。

请在我的答案的评论中提出任何问题,以便逐步解决您的问题。

于 2015-07-27T12:13:32.323 回答
-1

当你打开闪电时,我什么也看不见。没关系,您打开 2 个灯,glEnable(GL_LIGHTn)但未启用整个功能。如果你感到困惑,有一篇文章。我是说因为你的模型看起来根本没有阴影,它们都有相同的颜色,而且颜色不取决于表面如何指向光源,如果你设置它也不取决于法线我应该说。我认为两盏灯足以达到这个目的,对于演示项目,我通常有我的闪电和一个漫射光,我什至不设置环境光或镜面反射部分。像这样:

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, { 0.8f, 0.8f, 0.8f, 1.0f } );

它非常简单,但您可能想要的一切,基本的阴影,而不仅仅是屏幕上形成形状边框的一个大色斑。

于 2015-07-17T21:17:43.480 回答