Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Un-OOP the tutorial #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions Chapter1/2-HelloTriangle/Shaders/shader.vert
Expand Up @@ -18,14 +18,14 @@
// shader(vertex)


// This defines our input variable, aPosition.
// This defines our input variable, aPos.
// It starts with the line "layout(location = 0)". This defines where this input variable will be located, which is needed for GL.VertexAttribPointer.
// However, you can omit it, and replace this with just "in vec3 aPosition". If you do that, you'll have to replace the 0 in GL.VertexAttribPointer with
// However, you can omit it, and replace this with just "in vec3 aPos". If you do that, you'll have to replace the 0 in GL.VertexAttribPointer with
// a call to GL.GetAttribLocation(shaderHandle, attributeName)
// Next, the keyword "in" defines this as an input variable. We'll have an example of the "out" keyword in the next tutorial.
// Then, the keyword "vec3" means this is a vector with 3 floats inside.

layout(location = 0) in vec3 aPosition;
layout(location = 0) in vec3 aPos;


// Like C, we have an entrypoint function. In this case, it takes void and returns void, and must be named main.
Expand All @@ -37,5 +37,5 @@ layout(location = 0) in vec3 aPosition;

void main(void)
{
gl_Position = vec4(aPosition, 1.0);
gl_Position = vec4(aPos, 1.0);
}
6 changes: 3 additions & 3 deletions Chapter1/2-HelloTriangle/Window.cs
Expand Up @@ -114,11 +114,11 @@ protected override void OnLoad()
// Shaders are tiny programs that live on the GPU. OpenGL uses them to handle the vertex-to-pixel pipeline.
// Check out the Shader class in Common to see how we create our shaders, as well as a more in-depth explanation of how shaders work.
// shader.vert and shader.frag contain the actual shader code.
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_shader = Shader.FromFile("Shaders/shader.vert", "Shaders/shader.frag");

// Now, enable the shader.
// Just like the VBO, this is global, so every function that uses a shader will modify this one until a new one is bound instead.
_shader.Use();
GL.UseProgram(_shader.Handle);

// Setup is now complete! Now we move to the OnRenderFrame function to finally draw the triangle.
}
Expand All @@ -140,7 +140,7 @@ protected override void OnRenderFrame(FrameEventArgs e)
// and then calling an OpenGL function to render.

// Bind the shader
_shader.Use();
GL.UseProgram(_shader.Handle);

// Bind the VAO
GL.BindVertexArray(_vertexArrayObject);
Expand Down
4 changes: 2 additions & 2 deletions Chapter1/3-ElementBufferObjects/Shaders/shader.vert
@@ -1,8 +1,8 @@
#version 330 core

layout(location = 0) in vec3 aPosition;
layout(location = 0) in vec3 aPos;

void main(void)
{
gl_Position = vec4(aPosition, 1.0);
gl_Position = vec4(aPos, 1.0);
}
6 changes: 3 additions & 3 deletions Chapter1/3-ElementBufferObjects/Window.cs
Expand Up @@ -76,8 +76,8 @@ protected override void OnLoad()
GL.BufferData(BufferTarget.ElementArrayBuffer, _indices.Length * sizeof(uint), _indices, BufferUsageHint.StaticDraw);
// The EBO has now been properly setup. Go to the Render function to see how we draw our rectangle now!

_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_shader.Use();
_shader = Shader.FromFile("Shaders/shader.vert", "Shaders/shader.frag");
GL.UseProgram(_shader.Handle);
}

protected override void OnRenderFrame(FrameEventArgs e)
Expand All @@ -86,7 +86,7 @@ protected override void OnRenderFrame(FrameEventArgs e)

GL.Clear(ClearBufferMask.ColorBufferBit);

_shader.Use();
GL.UseProgram(_shader.Handle);

// Because ElementArrayObject is a property of the currently bound VAO,
// the buffer you will find in the ElementArrayBuffer will change with the currently bound VAO.
Expand Down
4 changes: 2 additions & 2 deletions Chapter1/4-Shaders-InsAndOuts/Shaders/shader.vert
@@ -1,7 +1,7 @@
#version 330 core

// the position variable has attribute position 0
layout(location = 0) in vec3 aPosition;
layout(location = 0) in vec3 aPos;

// This variable uses the keyword out in order to pass on the value to the
// next shader down in the chain, in this case the frag shader
Expand All @@ -10,7 +10,7 @@ out vec4 vertexColor;
void main(void)
{
// see how we directly give a vec3 to vec4's constructor
gl_Position = vec4(aPosition, 1.0);
gl_Position = vec4(aPos, 1.0);

// Here we assign the variable a dark red color to the out variable
vertexColor = vec4(0.5, 0.0, 0.0, 1.0);
Expand Down
7 changes: 4 additions & 3 deletions Chapter1/4-Shaders-InsAndOuts/Window.cs
Expand Up @@ -56,8 +56,9 @@ protected override void OnLoad()
GL.GetInteger(GetPName.MaxVertexAttribs, out int maxAttributeCount);
Debug.WriteLine($"Maximum number of vertex attributes supported: {maxAttributeCount}");

_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_shader.Use();
_shader = Shader.FromFile("Shaders/shader.vert", "Shaders/shader.frag");

GL.UseProgram(_shader.Handle);
}

protected override void OnRenderFrame(FrameEventArgs e)
Expand All @@ -66,7 +67,7 @@ protected override void OnRenderFrame(FrameEventArgs e)

GL.Clear(ClearBufferMask.ColorBufferBit);

_shader.Use();
GL.UseProgram(_shader.Handle);

GL.BindVertexArray(_vertexArrayObject);

Expand Down
4 changes: 2 additions & 2 deletions Chapter1/4-Shaders-MoreAttributes/Shaders/shader.vert
@@ -1,7 +1,7 @@
#version 330 core

// the position variable has attribute position 0
layout(location = 0) in vec3 aPosition;
layout(location = 0) in vec3 aPos;

// This is where the color values we assigned in the main program goes to
layout(location = 1) in vec3 aColor;
Expand All @@ -11,7 +11,7 @@ out vec3 ourColor; // output a color to the fragment shader
void main(void)
{
// see how we directly give a vec3 to vec4's constructor
gl_Position = vec4(aPosition, 1.0);
gl_Position = vec4(aPos, 1.0);

// We use the outColor variable to pass on the color information to the frag shader
ourColor = aColor;
Expand Down
6 changes: 3 additions & 3 deletions Chapter1/4-Shaders-MoreAttributes/Window.cs
Expand Up @@ -67,8 +67,8 @@ protected override void OnLoad()
GL.GetInteger(GetPName.MaxVertexAttribs, out int maxAttributeCount);
Debug.WriteLine($"Maximum number of vertex attributes supported: {maxAttributeCount}");

_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_shader.Use();
_shader = Shader.FromFile("Shaders/shader.vert", "Shaders/shader.frag");
GL.UseProgram(_shader.Handle);
}

protected override void OnRenderFrame(FrameEventArgs e)
Expand All @@ -77,7 +77,7 @@ protected override void OnRenderFrame(FrameEventArgs e)

GL.Clear(ClearBufferMask.ColorBufferBit);

_shader.Use();
GL.UseProgram(_shader.Handle);

GL.BindVertexArray(_vertexArrayObject);

Expand Down
4 changes: 2 additions & 2 deletions Chapter1/4-Shaders-Uniforms/Shaders/shader.vert
@@ -1,9 +1,9 @@
#version 330 core

layout(location = 0) in vec3 aPosition; // the position variable has attribute position 0
layout(location = 0) in vec3 aPos; // the position variable has attribute position 0

void main(void)
{
// see how we directly give a vec3 to vec4's constructor
gl_Position = vec4(aPosition, 1.0);
gl_Position = vec4(aPos, 1.0);
}
6 changes: 3 additions & 3 deletions Chapter1/4-Shaders-Uniforms/Window.cs
Expand Up @@ -56,8 +56,8 @@ protected override void OnLoad()
GL.GetInteger(GetPName.MaxVertexAttribs, out int maxAttributeCount);
Debug.WriteLine($"Maximum number of vertex attributes supported: {maxAttributeCount}");

_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_shader.Use();
_shader = Shader.FromFile("Shaders/shader.vert", "Shaders/shader.frag");
GL.UseProgram(_shader.Handle);

// We start the stopwatch here as this method is only called once.
_timer = new Stopwatch();
Expand All @@ -70,7 +70,7 @@ protected override void OnRenderFrame(FrameEventArgs e)

GL.Clear(ClearBufferMask.ColorBufferBit);

_shader.Use();
GL.UseProgram(_shader.Handle);

// Here, we get the total seconds that have elapsed since the last time this method has reset
// and we assign it to the timeValue variable so it can be used for the pulsating color.
Expand Down
4 changes: 2 additions & 2 deletions Chapter1/5-Textures/Shaders/shader.vert
@@ -1,6 +1,6 @@
#version 330 core

layout(location = 0) in vec3 aPosition;
layout(location = 0) in vec3 aPos;

// We add another input variable for the texture coordinates.

Expand All @@ -18,5 +18,5 @@ void main(void)

texCoord = aTexCoord;

gl_Position = vec4(aPosition, 1.0);
gl_Position = vec4(aPos, 1.0);
}
19 changes: 11 additions & 8 deletions Chapter1/5-Textures/Window.cs
Expand Up @@ -35,7 +35,7 @@ public class Window : GameWindow
private Shader _shader;

// For documentation on this, check Texture.cs.
private Texture _texture;
private int _texture;

public Window(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings)
: base(gameWindowSettings, nativeWindowSettings)
Expand All @@ -60,25 +60,26 @@ protected override void OnLoad()
GL.BufferData(BufferTarget.ElementArrayBuffer, _indices.Length * sizeof(uint), _indices, BufferUsageHint.StaticDraw);

// The shaders have been modified to include the texture coordinates, check them out after finishing the OnLoad function.
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_shader.Use();
_shader = Shader.FromFile("Shaders/shader.vert", "Shaders/shader.frag");
GL.UseProgram(_shader.Handle);

// Because there's now 5 floats between the start of the first vertex and the start of the second,
// we modify the stride from 3 * sizeof(float) to 5 * sizeof(float).
// This will now pass the new vertex array to the buffer.
var vertexLocation = _shader.GetAttribLocation("aPosition");
var vertexLocation = 0; // The location of aPos
GL.EnableVertexAttribArray(vertexLocation);
GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);

// Next, we also setup texture coordinates. It works in much the same way.
// We add an offset of 3, since the texture coordinates comes after the position data.
// We also change the amount of data to 2 because there's only 2 floats for texture coordinates.
var texCoordLocation = _shader.GetAttribLocation("aTexCoord");
var texCoordLocation = 1; // The location of aTexCoord
GL.EnableVertexAttribArray(texCoordLocation);
GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));

_texture = Texture.LoadFromFile("Resources/container.png");
_texture.Use(TextureUnit.Texture0);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, _texture);
}

protected override void OnRenderFrame(FrameEventArgs e)
Expand All @@ -89,8 +90,10 @@ protected override void OnRenderFrame(FrameEventArgs e)

GL.BindVertexArray(_vertexArrayObject);

_texture.Use(TextureUnit.Texture0);
_shader.Use();
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, _texture);

GL.UseProgram(_shader.Handle);

GL.DrawElements(PrimitiveType.Triangles, _indices.Length, DrawElementsType.UnsignedInt, 0);

Expand Down
4 changes: 2 additions & 2 deletions Chapter1/6-MultipleTextures/Shaders/shader.vert
@@ -1,6 +1,6 @@
#version 330 core

layout(location = 0) in vec3 aPosition;
layout(location = 0) in vec3 aPos;

layout(location = 1) in vec2 aTexCoord;

Expand All @@ -10,5 +10,5 @@ void main(void)
{
texCoord = aTexCoord;

gl_Position = vec4(aPosition, 1.0);
gl_Position = vec4(aPos, 1.0);
}
36 changes: 22 additions & 14 deletions Chapter1/6-MultipleTextures/Window.cs
Expand Up @@ -31,9 +31,9 @@ public class Window : GameWindow

private Shader _shader;

private Texture _texture;
private int _texture;

private Texture _texture2;
private int _texture2;

public Window(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings)
: base(gameWindowSettings, nativeWindowSettings)
Expand All @@ -58,31 +58,36 @@ protected override void OnLoad()
GL.BufferData(BufferTarget.ElementArrayBuffer, _indices.Length * sizeof(uint), _indices, BufferUsageHint.StaticDraw);

// shader.frag has been modified yet again, take a look at it as well.
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_shader.Use();
_shader = Shader.FromFile("Shaders/shader.vert", "Shaders/shader.frag");
GL.UseProgram(_shader.Handle);

var vertexLocation = _shader.GetAttribLocation("aPosition");
var vertexLocation = 0; // The location of aPos
GL.EnableVertexAttribArray(vertexLocation);
GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);

var texCoordLocation = _shader.GetAttribLocation("aTexCoord");
var texCoordLocation = 1; // The location of aTexCoord
GL.EnableVertexAttribArray(texCoordLocation);
GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));

_texture = Texture.LoadFromFile("Resources/container.png");
// Texture units are explained in Texture.cs, at the Use function.
// First texture goes in texture unit 0.
_texture.Use(TextureUnit.Texture0);

// First we use GL.ActiveTexture to tell OpenGL which texture unit we would like following commands to use.
GL.ActiveTexture(TextureUnit.Texture0);
// Then we bind our first texture to the 0th texture unit as a 2D texture.
GL.BindTexture(TextureTarget.Texture2D, _texture);

// This is helpful because System.Drawing reads the pixels differently than OpenGL expects.
_texture2 = Texture.LoadFromFile("Resources/awesomeface.png");
// Then, the second goes in texture unit 1.
_texture2.Use(TextureUnit.Texture1);
GL.ActiveTexture(TextureUnit.Texture1);
GL.BindTexture(TextureTarget.Texture2D, _texture2);

// Next, we must setup the samplers in the shaders to use the right textures.
// Next, we must setup the samplers in the shaders to use the right texture units.
// The int we send to the uniform indicates which texture unit the sampler should use.
_shader.SetInt("texture0", 0);
_shader.SetInt("texture1", 1);
GL.Uniform1(_shader.UniformLocations["texture0"], 0);
GL.Uniform1(_shader.UniformLocations["texture1"], 1);
}

protected override void OnRenderFrame(FrameEventArgs e)
Expand All @@ -93,9 +98,12 @@ protected override void OnRenderFrame(FrameEventArgs e)

GL.BindVertexArray(_vertexArrayObject);

_texture.Use(TextureUnit.Texture0);
_texture2.Use(TextureUnit.Texture1);
_shader.Use();
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, _texture);
GL.ActiveTexture(TextureUnit.Texture1);
GL.BindTexture(TextureTarget.Texture2D, _texture2);

GL.UseProgram(_shader.Handle);

GL.DrawElements(PrimitiveType.Triangles, _indices.Length, DrawElementsType.UnsignedInt, 0);

Expand Down
4 changes: 2 additions & 2 deletions Chapter1/7-Transformations/Shaders/shader.vert
@@ -1,6 +1,6 @@
#version 330 core

layout(location = 0) in vec3 aPosition;
layout(location = 0) in vec3 aPos;

layout(location = 1) in vec2 aTexCoord;

Expand All @@ -14,5 +14,5 @@ void main(void)
texCoord = aTexCoord;

// Then all you have to do is multiply the vertices by the transformation matrix, and you'll see your transformation in the scene!
gl_Position = vec4(aPosition, 1.0) * transform;
gl_Position = vec4(aPos, 1.0) * transform;
}