Basic OpenTK part 2 - First Triangle

  Рет қаралды 13,462

Two-Bit Coding

Two-Bit Coding

Күн бұрын

Пікірлер: 56
@jnyendwa
@jnyendwa 2 жыл бұрын
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-bitcoding8018
@two-bitcoding8018 2 жыл бұрын
Thanks!
@Spyboi666
@Spyboi666 2 жыл бұрын
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
@xcgasparxc
@xcgasparxc 2 жыл бұрын
This is drawing a white triangle, but the color parameters are not white, does this happens to you too?
@nyendwa
@nyendwa 2 жыл бұрын
@@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.
@nyendwa
@nyendwa 2 жыл бұрын
@@Spyboi666 lol its hell if you are doing it for the first time
@frydevtv6312
@frydevtv6312 2 жыл бұрын
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 !
@gone4701
@gone4701 3 жыл бұрын
Wow! Criminally underrated KZbinr.
@gone4701
@gone4701 3 жыл бұрын
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
@bluesillybeard
@bluesillybeard 2 жыл бұрын
Thanks, your fix worked for me and probably saved me quite a few hours
@aurk9956
@aurk9956 2 жыл бұрын
wow tysm I'm not sure why indicating the number was a float was ruining the shaders
@bluesillybeard
@bluesillybeard 2 жыл бұрын
@@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-lw7iw
@RobotMr-lw7iw 5 ай бұрын
thanks buddy.help a lot
@Hart8
@Hart8 3 жыл бұрын
Great tutorial, can't wait for more
@nicopootato2330
@nicopootato2330 2 жыл бұрын
super helpful for me as someone that knows C# but want to get into OpenGL.
@NavyCuda
@NavyCuda 3 жыл бұрын
Looking forward to the next video!
@therandomsomeone.
@therandomsomeone. 2 жыл бұрын
truly amazing you need more views dude
@MaxTheDragon
@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.
@rayanm2175
@rayanm2175 2 жыл бұрын
very good , nice tutorial bro
@Marcosa-jy7cv
@Marcosa-jy7cv 2 жыл бұрын
onde estas a parte 3???
@two-bitcoding8018
@two-bitcoding8018 2 жыл бұрын
Hopefully soon. :) I wasn't planning on a lot of videos. Just a starting point.
@Marcosa-jy7cv
@Marcosa-jy7cv 2 жыл бұрын
@@two-bitcoding8018 tudo bem entao,estou no aguardo :)
@frydevtv6312
@frydevtv6312 2 жыл бұрын
@@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-bitcoding8018
@two-bitcoding8018 2 жыл бұрын
@@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.
@jnyendwa
@jnyendwa 2 жыл бұрын
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-bitcoding8018
@two-bitcoding8018 2 жыл бұрын
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
@jnyendwa
@jnyendwa 2 жыл бұрын
@@two-bitcoding8018 overall good work. I love what you do here.
@DLight7932
@DLight7932 Күн бұрын
Why is my triangle black???
@1jhavus543
@1jhavus543 5 ай бұрын
this is insane
@not_vdb0059
@not_vdb0059 2 жыл бұрын
Is there a way that i can show this opentk window in a wpf application ui?
@two-bitcoding8018
@two-bitcoding8018 2 жыл бұрын
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)
@maunguyenvan9296
@maunguyenvan9296 2 жыл бұрын
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-bitcoding8018
@two-bitcoding8018 2 жыл бұрын
I am not familiar with that subject.
@GuumisGuumis
@GuumisGuumis 3 жыл бұрын
Normalized values mean that the values are between -1,1. In your case the values are already normalized so it should be false.
@two-bitcoding8018
@two-bitcoding8018 3 жыл бұрын
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?
@GuumisGuumis
@GuumisGuumis 3 жыл бұрын
@@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.
@whisperess
@whisperess 2 жыл бұрын
@@two-bitcoding8018 yes, that is correct
@joanhaesen
@joanhaesen Жыл бұрын
A get a white triangel Can't color it. I have a Nvidia geforce RTX vidiocard
@saustin98
@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.
@tkuzmic1
@tkuzmic1 16 күн бұрын
Change vec to vec4 in shader line: gl_Position=vec(aPosition,1f);
@qtpahquby9137
@qtpahquby9137 Жыл бұрын
It's not drawing the triangle for me :(
@malsnaize5275
@malsnaize5275 Жыл бұрын
Really awesome - I'vce been looking for something like this for so long! THANK YOU!
@JarppaGuru
@JarppaGuru 3 жыл бұрын
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-bitcoding8018
@two-bitcoding8018 3 жыл бұрын
Thanks for watching and your feedback. I hope to be able to make that video soon.
@joanhaesen
@joanhaesen Жыл бұрын
I see a white rectangle, can't color it out. Video card: NVIDIA Geforce RTX.
@two-bitcoding8018
@two-bitcoding8018 Жыл бұрын
That is strange. I might do a little research on that but I have seen that exact issue before.
@tkuzmic1
@tkuzmic1 16 күн бұрын
Check that shader line contains vec4 (not vec) (gl_Position=vec4(aPosition,1f);)
@respectt3474
@respectt3474 3 жыл бұрын
I did everything step by step like you, and the triangle is invisible :/
@diamond32tutoriales49
@diamond32tutoriales49 2 жыл бұрын
new sub :D
@nevermind1O844
@nevermind1O844 2 жыл бұрын
As a german dude... using a comma as a decimal separator when defining one of the vertices cost me like 2 hours... -.-
@two-bitcoding8018
@two-bitcoding8018 2 жыл бұрын
thanks for the reminder.. I haven't thought about that in a while!
@sepultaris
@sepultaris Жыл бұрын
Hmm, my triangle is black, but it works.
@sepultaris
@sepultaris Жыл бұрын
Turns out I was missing a semicolon at the end of the pixelColor line. Now it's yellow.
Basic OpenTK part 3 - Vertex Colors
14:55
Two-Bit Coding
Рет қаралды 5 М.
Basic OpenTK part 1 - Window Creation
12:08
Two-Bit Coding
Рет қаралды 20 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
Creating a Voxel Engine (like Minecraft) from Scratch in Python
1:06:39
Basic OpenTK part 5 - GLSL Uniforms and Screen Space Transform
15:22
Two-Bit Coding
Рет қаралды 3,4 М.
Advanced C# - LINQ Tutorial
3:07:24
freeCodeCamp.org
Рет қаралды 169 М.
Interactive Graphics 17 - Geometry Shaders
51:09
Cem Yuksel
Рет қаралды 10 М.
Adding levels to my custom game engine (C#/MonoGame)
17:00
OpenTK Lecture | OpenGL Bindings For C#
1:34:09
NoNumberMan
Рет қаралды 13 М.
Basic OpenTK part 4 - Index Buffers
12:47
Two-Bit Coding
Рет қаралды 3,7 М.
C# GAME ENGINE BY 16-YEAR-OLD! // Code Review
49:06
The Cherno
Рет қаралды 315 М.
So I Made My Own Game Engine...
10:19
Chadderbox
Рет қаралды 103 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН