使用OpenTK的简单GLSL着色器不起作用 [英] Simple GLSL shader using OpenTK doesn't work

查看:238
本文介绍了使用OpenTK的简单GLSL着色器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我已经开始研究光线跟踪,并认为使用GLSL着色器编写光线跟踪器将是一个好主意,因为它将大大优化性能.

问题:我编写了一个简单的程序来测试GLSL着色器(更多是来自Internet上部分代码的汇编),但是它不起作用.它可以运行,但仅显示清晰的颜色.我正在使用OpenTK和C#.我的视频卡支持OpenGL 2.1,并且我已经看到一些GLSL着色器在其上运行(尽管我没有其源代码).

这是我的代码.

可以在此处找到Shader.cs文件.
我的主要课程是这个(这是OpenTK的Game.cs文件,并添加了一些代码:

Background: I''ve started playing around with raytracing and thought it''ll be a good idea to write the raytracer using GLSL shaders as it''ll optimize the performance a lot.

Problem: I''ve written (more of a assemble from parts of code on the internet) a simple program to test GLSL shaders but it isn''t working. It runs but just shows the clear color. I''m using OpenTK and C#. My video card supports OpenGL 2.1 and I''ve seen some GLSL shaders running on it (though I don''t have their source code).

Here is my code.

The Shader.cs file can be found here.
My main class is this (It''s the OpenTK''s Game.cs file with some code added:

using System;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using OpenTK.Input;

namespace GLSLTest
{
    class Game : GameWindow
    {

        public Shader Shader;

        /// <summary>Creates a 800x600 window with the specified title.</summary>
        public Game()
            : base(800, 600, GraphicsMode.Default, "OpenTK Quick Start Sample")
        {
            VSync = VSyncMode.On;
            Shader = new Shader(
                "void main(void){ gl_Position = gl_Vertex;}",
                "void main(void) {"
                + "vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);"
                + "float dist_squared = dot(pos, pos);"

                + "gl_FragColor = (dist_squared < 400.0) "
                + "? vec4(.90, .90, .90, 1.0)"
                + ": vec4(.20, .20, .40, 1.0);"
                + "}");
            Shader.Activate();
        }

        /// <summary>Load resources here.</summary>
        /// <param name="e">Not used.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
            GL.Enable(EnableCap.DepthTest);
        }

        /// <summary>
        /// Called when your window is resized. Set your viewport here. It is also
        /// a good place to set up your projection matrix (which probably changes
        /// along when the aspect ratio of your window).
        /// </summary>
        /// <param name="e">Not used.</param>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);

            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
        }

        /// <summary>
        /// Called when it is time to setup the next frame. Add you game logic here.
        /// </summary>
        /// <param name="e">Contains timing information for framerate independent logic.</param>
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);

            if (Keyboard[Key.Escape])
                Exit();
        }

        /// <summary>
        /// Called when it is time to render the next frame. Add your rendering code here.
        /// </summary>
        /// <param name="e">Contains timing information.</param>
        /// 

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelview);

            SwapBuffers();
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // The 'using' idiom guarantees proper resource cleanup.
            // We request 30 UpdateFrame events per second, and unlimited
            // RenderFrame events (as fast as the computer can handle).
            using (Game game = new Game())
            {
                game.Run(30.0);
            }
        }
    }
}

推荐答案

我看不到要在场景中添加任何几何图形的位置.着色器通常应用于几何体,即使它是像素着色器也是如此.尝试在相机前面绘制一个四边形,然后将像素着色器附加到该四边形.
I can''t see where you''re adding any geometry to your scene. A shader is typically applied to geometry, even if it''s a pixel shader. Try drawing a quad in front of the camera and attaching the pixel shader to that.


这篇关于使用OpenTK的简单GLSL着色器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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