opentk/opentk.net

Minor tutorial issues (opentk.net/learn)

MV10 opened this issue · 2 comments

MV10 commented

I am using VS2022, OpenTK 4, and .NET 6. Here are a few notes on minor issues I've found in the tutorials (opentk.net/learn). If I have time later on, I'll PR the fixes myself, but I thought I'd note them in an Issue as I run through the tutorial myself. (I know shader programming, but new to OpenTK, and also my wife wants to learn this stuff, so I thought I'd preview the material.)

In Shaders it is necessary to mark the shader.vert and shader.frag files as content to copy to the output directory, otherwise the File.ReadAllText calls in the Shader class constructor will fail. This is done by altering the Copy to Output Directory file property to Copy always:

image

In Compiling the Shaders, you can't use out int success twice within the same method; I used out int vertOk and out int fragOk (and similarly, later on, out int linkOk):

GL.GetShader(VertexShader, ShaderParameter.CompileStatus, out int success);
// snip
GL.GetShader(FragmentShader, ShaderParameter.CompileStatus, out int success);

In Linking Vertex Attributes it may be useful to note that the reader shouldn't add any of the code to their program. How to add that to the program is covered in the following Vertex Array Object section.

In Element Buffer Objects the reader is told, "in OnLoad, below where you initialize the VertexBufferObject, we initialize the ElementBufferObject" however, per the highlighted note which follows (element array buffer can only be bound if there is a VAO bound), that code should actually be added after the VertexArrayObject initialization, not the VertexBufferObject. If the code is written as instructed, the program throws a System.AccessViolation exception.

In Uniforms, the _shader field name is inconsistent with earlier sections; it should be shader without the leading underscore. Additionally earlier sections defined the Handle field in the Shader class as int Handle; which defaults to private scoping; it needs to be explicitly declared as public. Finally, it may be helpful to explicitly instruct the reader to add a new class-level field as Stopwatch timer = new(); and it is then necessary to call timer.Start(); in the constructor, otherwise the elapsed time is always zero and the color will not change.

If I notice anything else when I have time to get back to this, I'll add more posts.

Overall, very clear though!

Thanks a ton for this! Will bring a lot of consistency to the tutorial and make it easier to follow. :)
I'll get to fixing these issues soon.

MV10 commented

I don't know if this is too far off-topic, but for the point about copying the shaders to the output directory, there's another possible issue -- if the only file you modify is a shader, VS doesn't consider that a change that should trigger a build, so the updated shader won't be copied if you run the program again. You have to do a Build -> Clean which is kind of a headache.

The fix is easy, add this to the csproj:

  <ItemGroup>
    <UpToDateCheckInput Include="@(Content);@(None)" />
  </ItemGroup>

But like I said, that's probably starting to get a bit off-track for a tutorial.