Source Code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenTK.Graphics.OpenGL; using OpenTK; using OpenTK.Mathematics; using OpenTK.Windowing.Common; using OpenTK.Windowing.Desktop; namespace BasicOpenTK { public class Game : GameWindow { private int vertexbufferObject; private int shaderProgramObject; private int vertexArrayObject; public Game() : base(GameWindowSettings.Default, NativeWindowSettings.Default) { this.CenterWindow(new Vector2i(400,400)); } protected override void OnUpdateFrame(FrameEventArgs args) { base.OnUpdateFrame(args); } protected override void OnLoad() { GL.ClearColor(new Color4(0.3f, 0.4f, 0.5f, 1f)); float[] vertices= new float[] { 0.0f,0.5f,0f, 0.5f,-0.5f,0f, -0.5f,-0.5f,0f }; this.vertexbufferObject=GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, this.vertexbufferObject); GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); this.vertexArrayObject=GL.GenVertexArray(); GL.BindVertexArray(this.vertexArrayObject); GL.BindBuffer(BufferTarget.ArrayBuffer, this.vertexbufferObject); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0); GL.EnableVertexAttribArray(0); GL.BindVertexArray(0); string vertexShaderCode = @" #version 330 core layout (location=0) in vec3 aPosition; void main(){ gl_Position=vec(aPosition,1f); }"; string pixelShaderCode = @" #version 330 core out vec4 pixelColor; void main(){ pixelColor=vec4(0.8f,0.8f,0.1f,1f); } "; int vertexShaderObject= GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(vertexbufferObject,vertexShaderCode); GL.CompileShader(vertexShaderObject); int pixelShaderObject = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(pixelShaderObject, pixelShaderCode); GL.CompileShader(pixelShaderObject); this.shaderProgramObject = GL.CreateProgram(); GL.AttachShader(this.shaderProgramObject, vertexShaderObject); GL.AttachShader(this.shaderProgramObject, pixelShaderObject); GL.LinkProgram(this.shaderProgramObject); GL.DetachShader(this.shaderProgramObject, vertexShaderObject); GL.DetachShader(this.shaderProgramObject,pixelShaderObject); GL.DeleteShader(vertexShaderObject);//Check this place GL.DeleteShader(pixelShaderObject);//relook base.OnLoad(); } protected override void OnUnload() { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DeleteBuffer(this.vertexbufferObject);//check this in case GL.UseProgram(0);//check this too GL.DeleteProgram(this.shaderProgramObject);//Problem identified base.OnUnload(); } protected override void OnResize(ResizeEventArgs e) { GL.Viewport(0, 0, e.Width, e.Height); base.OnResize(e); } protected override void OnRenderFrame(FrameEventArgs args) { GL.Clear(ClearBufferMask.ColorBufferBit); GL.UseProgram(this.shaderProgramObject); GL.BindVertexArray(this.vertexArrayObject); GL.DrawArrays(PrimitiveType.Triangles, 0, 3); this.Context.SwapBuffers(); base.OnRenderFrame(args); } } }
@two-bitcoding80182 жыл бұрын
Thanks!
@Spyboi6662 жыл бұрын
thank you, i've spent a hour trying to fix why the triangle arent drawn, turns out i accidentally use VertexAttribPointerType.HalfFloat instead of VertexAttribPointerType.Float lol
@xcgasparxc2 жыл бұрын
This is drawing a white triangle, but the color parameters are not white, does this happens to you too?
@nyendwa2 жыл бұрын
@@xcgasparxc this is before we load the colours to the fragment shader, as you can see we didn't send any vertices to the pixel shader, go to the next tutorial it was well explained.
@nyendwa2 жыл бұрын
@@Spyboi666 lol its hell if you are doing it for the first time
@frydevtv63122 жыл бұрын
Awesome tutorial. I went through many before finding this one. Up to date and very concise, yet you learn a lot !! Thanks for sharing this. This is not your damn usual "I do this just for the sake of tutorial" type of tutorial(which doesnt teach you real practices) that often populates youtube. Again, thanks. Cant wait for image display and stuff !
@gone47013 жыл бұрын
Wow! Criminally underrated KZbinr.
@gone47013 жыл бұрын
ATTENTION EVERYONE: I have found the fix. I tried the shaders from the LearnOpenTK tutorial and it worked. Use this: 1. Change pixelShaderCode (or fragmentShaderCode) to " #version 330 out vec4 outputColor; void main() { outputColor = vec4(1.0, 1.0, 0.0, 1.0); } " 2. Change verticeShaderCode to " #version 330 core layout(location = 0) in vec3 aPosition; void main(void) { gl_Position = vec4(aPosition, 1.0); } " 3. Have a good day
@bluesillybeard2 жыл бұрын
Thanks, your fix worked for me and probably saved me quite a few hours
@aurk99562 жыл бұрын
wow tysm I'm not sure why indicating the number was a float was ruining the shaders
@bluesillybeard2 жыл бұрын
@@aurk9956 Me neither. I see no reason for the shaders in the tutorial to not work, but I guess you can't argue with the compiler.
@RobotMr-lw7iw5 ай бұрын
thanks buddy.help a lot
@Hart83 жыл бұрын
Great tutorial, can't wait for more
@nicopootato23302 жыл бұрын
super helpful for me as someone that knows C# but want to get into OpenGL.
@NavyCuda3 жыл бұрын
Looking forward to the next video!
@therandomsomeone.2 жыл бұрын
truly amazing you need more views dude
@MaxTheDragon Жыл бұрын
In the GLSL source snippets, it's not strictly necessary to explicitly write the "core" profile value in the #version attribute as core is the default profile. Writing #version 330 is sufficient.
@rayanm21752 жыл бұрын
very good , nice tutorial bro
@Marcosa-jy7cv2 жыл бұрын
onde estas a parte 3???
@two-bitcoding80182 жыл бұрын
Hopefully soon. :) I wasn't planning on a lot of videos. Just a starting point.
@Marcosa-jy7cv2 жыл бұрын
@@two-bitcoding8018 tudo bem entao,estou no aguardo :)
@frydevtv63122 жыл бұрын
@@two-bitcoding8018 How to code a rectangle would be super nice. I have litteraly found 0 tutorial using opentk. Just regular c++ opengl one. I tried to tweak the values from this tutorial but sadly i didnt find out how to draw a rectangle apart from drawing two triangle. But then texturing would be hell..
@two-bitcoding80182 жыл бұрын
@@frydevtv6312 Drawing two triangles is the correct idea. I plan on making a few more videos on basic OpenTk; one of them would be quad and sprite drawing. Hopefully being able to release in the next week.
@jnyendwa2 жыл бұрын
There is need to have a source code posted so we verify what could be wrong with our code whenever it cant do what it should.
@two-bitcoding80182 жыл бұрын
You are correct. I failed to do so with these first videos in the series. Here is a link to the source code from later videos: github.com/twobitcoder101/Basic-OpenTK-part-07
@jnyendwa2 жыл бұрын
@@two-bitcoding8018 overall good work. I love what you do here.
@DLight7932Күн бұрын
Why is my triangle black???
@1jhavus5435 ай бұрын
this is insane
@not_vdb00592 жыл бұрын
Is there a way that i can show this opentk window in a wpf application ui?
@two-bitcoding80182 жыл бұрын
I have never used opentk with wpf but you can probably do use it with an add in library like: github.com/opentk/GLWpfControl (Just as a disclaimer I haven't actually used that library)
@maunguyenvan92962 жыл бұрын
My question is not relation your video, but I want to know how do i use OpenGL Utility Toolkit (glut) in OpenTK C#. Thanks
@two-bitcoding80182 жыл бұрын
I am not familiar with that subject.
@GuumisGuumis3 жыл бұрын
Normalized values mean that the values are between -1,1. In your case the values are already normalized so it should be false.
@two-bitcoding80183 жыл бұрын
Thank you! So if set to true will it normalize the value based on type? So if the type is unsigned byte and normalize is set to true it will convert to float in the range 0 to 1 since it is unsigned?
@GuumisGuumis3 жыл бұрын
@@two-bitcoding8018I would assume so. I know normalized from 3d software where you eg normalize coordinates form values like 1000,1080,200 into 0.35, 045,0.67.
@whisperess2 жыл бұрын
@@two-bitcoding8018 yes, that is correct
@joanhaesen Жыл бұрын
A get a white triangel Can't color it. I have a Nvidia geforce RTX vidiocard
@saustin98 Жыл бұрын
I had the same problem. I had used gl_position (with the lower case p). It still produced the correct vertices, but I got a white triangle. I got no C# compile errors, but behind the scenes, I was getting a GLSL compile error. There's no way to know that unless you use the GetShaderInfoLog function to check for an error message from GLSL: string vectorShaderInfo = GL.GetShaderInfoLog(vHandle); if (vectorShaderInfo != String.Empty) { System.Diagnostics.Debug.WriteLine(vectorShaderInfo); } This told me that GLSL didn't recognize gl_position. This code is explained in a later video. I don't know if that was your problem, but I'm going to run this after ALL my GLSL compiles, at least until it all works. You're very welcome.
@tkuzmic116 күн бұрын
Change vec to vec4 in shader line: gl_Position=vec(aPosition,1f);
@qtpahquby9137 Жыл бұрын
It's not drawing the triangle for me :(
@malsnaize5275 Жыл бұрын
Really awesome - I'vce been looking for something like this for so long! THANK YOU!
@JarppaGuru3 жыл бұрын
Can you make tutorial add color attribute to shader with new color vertices and position/vertices combined for this same sample. i tryed but no success. even have working object renderer class do it for, i just want know how is done this simple way LOL
@two-bitcoding80183 жыл бұрын
Thanks for watching and your feedback. I hope to be able to make that video soon.
@joanhaesen Жыл бұрын
I see a white rectangle, can't color it out. Video card: NVIDIA Geforce RTX.
@two-bitcoding8018 Жыл бұрын
That is strange. I might do a little research on that but I have seen that exact issue before.
@tkuzmic116 күн бұрын
Check that shader line contains vec4 (not vec) (gl_Position=vec4(aPosition,1f);)
@respectt34743 жыл бұрын
I did everything step by step like you, and the triangle is invisible :/
@diamond32tutoriales492 жыл бұрын
new sub :D
@nevermind1O8442 жыл бұрын
As a german dude... using a comma as a decimal separator when defining one of the vertices cost me like 2 hours... -.-
@two-bitcoding80182 жыл бұрын
thanks for the reminder.. I haven't thought about that in a while!
@sepultaris Жыл бұрын
Hmm, my triangle is black, but it works.
@sepultaris Жыл бұрын
Turns out I was missing a semicolon at the end of the pixelColor line. Now it's yellow.