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

Chapter 3 Part 1: Model Loading #90

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -43,6 +43,7 @@
#*.jpg binary
#*.png binary
#*.gif binary
*.obj binary

###############################################################################
# diff behavior for common document formats
Expand Down
2 changes: 1 addition & 1 deletion Chapter1/2-HelloTriangle/Window.cs
Expand Up @@ -209,7 +209,7 @@ protected override void OnUnload()
GL.DeleteBuffer(_vertexBufferObject);
GL.DeleteVertexArray(_vertexArrayObject);

GL.DeleteProgram(_shader.ID);
GL.DeleteProgram(_shader.Handle);

base.OnUnload();
}
Expand Down
2 changes: 1 addition & 1 deletion Chapter1/4-Shaders-Uniforms/Window.cs
Expand Up @@ -83,7 +83,7 @@ protected override void OnRenderFrame(FrameEventArgs e)

// This gets the uniform variable location from the frag shader so that we can
// assign the new green value to it.
int vertexColorLocation = GL.GetUniformLocation(_shader.ID, "ourColor");
int vertexColorLocation = GL.GetUniformLocation(_shader.Handle, "ourColor");

// Here we're assigning the ourColor variable in the frag shader
// via the OpenGL Uniform method which takes in the value as the individual vec values (which total 4 in this instance).
Expand Down
2 changes: 1 addition & 1 deletion Chapter1/5-Textures/Window.cs
Expand Up @@ -77,7 +77,7 @@ protected override void OnLoad()
GL.EnableVertexAttribArray(texCoordLocation);
GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));

_texture = Texture.LoadFromFile("Resources/container.png");
_texture = Texture.LoadFromFile("container.png", "Resources", "diffuse");
_texture.Use(TextureUnit.Texture0);
}

Expand Down
2 changes: 1 addition & 1 deletion Chapter3/1-ModelLoading/1-ModelLoading.csproj
Expand Up @@ -3,7 +3,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>LearnOpenTK</RootNamespace>
<AssemblyName>LearnOpenTK</AssemblyName>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ApplicationIcon />
<StartupObject />
</PropertyGroup>
Expand Down
4 changes: 1 addition & 3 deletions Chapter3/1-ModelLoading/Window.cs
Expand Up @@ -172,8 +172,6 @@ protected override void OnRenderFrame(FrameEventArgs e)
// First we setup the shader, including the texture uniform and then call the Draw() method on the imported model to draw all the contained meshes
_backPackShader.Use();
Matrix4 model = Matrix4.Identity;
//Matrix4.CreateScale(2.0f, 2.0f, 2.0f, out model);
//Matrix4.CreateTranslation(0.0f, 0.0f, 2.8f, out model);

_backPackShader.SetMatrix4("model", model);
_backPackShader.SetMatrix4("view", _camera.GetViewMatrix());
Expand All @@ -189,7 +187,7 @@ protected override void OnRenderFrame(FrameEventArgs e)
_shader.Use();

// Change triangle color
int vertexColorLocation = GL.GetUniformLocation(_shader.ID, "ourColor");
int vertexColorLocation = GL.GetUniformLocation(_shader.Handle, "ourColor");
GL.Uniform4(vertexColorLocation, _color.X, _color.Y, _color.Z, 1.0f);

// Bind the VAO
Expand Down
6 changes: 3 additions & 3 deletions Common/Model.cs
Expand Up @@ -194,17 +194,17 @@ private List<Texture> loadMaterialTextures(Material mat, TextureType type, strin
{
List<Texture> textures = new List<Texture>();


for (int i = 0; i < mat.GetMaterialTextureCount(type); i++)
{
TextureSlot str;
mat.GetMaterialTexture(type, i, out str);
string filename = directory + "/" + str.FilePath;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be Path.Combine(directory, str.FilePath).

// check if texture was loaded before and if so, continue to next iteration: skip loading a new texture
bool skip = false;
for (int j = 0; j < textures_loaded.Count; j++)
{

if (textures_loaded[j].path.CompareTo(str.FilePath)==0)
if (textures_loaded[j].path.CompareTo(filename) ==0)
{
textures.Add(textures_loaded[j]);
skip = true; // a texture with the same filepath has already been loaded, continue to next one. (optimization)
Expand All @@ -213,7 +213,7 @@ private List<Texture> loadMaterialTextures(Material mat, TextureType type, strin
}
if (!skip)
{ // if texture hasn't been loaded already, load it
Texture texture = Texture.LoadFromFile(str.FilePath, directory, typeName);
Texture texture = Texture.LoadFromFile(filename, typeName);
textures.Add(texture);
textures_loaded.Add(texture); // store it as texture loaded for entire model, to ensure we won't unnecessary load duplicate textures.
}
Expand Down
32 changes: 16 additions & 16 deletions Common/Shader.cs
Expand Up @@ -10,7 +10,7 @@ namespace LearnOpenTK.Common
// A simple class meant to help create shaders.
public class Shader
{
public readonly int ID;
public readonly int Handle;

private readonly Dictionary<string, int> _uniformLocations;

Expand Down Expand Up @@ -46,19 +46,19 @@ public Shader(string vertPath, string fragPath)

// These two shaders must then be merged into a shader program, which can then be used by OpenGL.
// To do this, create a program...
ID = GL.CreateProgram();
Handle = GL.CreateProgram();

// Attach both shaders...
GL.AttachShader(ID, vertexShader);
GL.AttachShader(ID, fragmentShader);
GL.AttachShader(Handle, vertexShader);
GL.AttachShader(Handle, fragmentShader);

// And then link them together.
LinkProgram(ID);
LinkProgram(Handle);

// When the shader program is linked, it no longer needs the individual shaders attached to it; the compiled code is copied into the shader program.
// Detach them, and then delete them.
GL.DetachShader(ID, vertexShader);
GL.DetachShader(ID, fragmentShader);
GL.DetachShader(Handle, vertexShader);
GL.DetachShader(Handle, fragmentShader);
GL.DeleteShader(fragmentShader);
GL.DeleteShader(vertexShader);

Expand All @@ -67,7 +67,7 @@ public Shader(string vertPath, string fragPath)
// later.

// First, we have to get the number of active uniforms in the shader.
GL.GetProgram(ID, GetProgramParameterName.ActiveUniforms, out var numberOfUniforms);
GL.GetProgram(Handle, GetProgramParameterName.ActiveUniforms, out var numberOfUniforms);

// Next, allocate the dictionary to hold the locations.
_uniformLocations = new Dictionary<string, int>();
Expand All @@ -76,10 +76,10 @@ public Shader(string vertPath, string fragPath)
for (var i = 0; i < numberOfUniforms; i++)
{
// get the name of this uniform,
var key = GL.GetActiveUniform(ID, i, out _, out _);
var key = GL.GetActiveUniform(Handle, i, out _, out _);

// get the location,
var location = GL.GetUniformLocation(ID, key);
var location = GL.GetUniformLocation(Handle, key);

// and then add it to the dictionary.
_uniformLocations.Add(key, location);
Expand Down Expand Up @@ -118,14 +118,14 @@ private static void LinkProgram(int program)
// A wrapper function that enables the shader program.
public void Use()
{
GL.UseProgram(ID);
GL.UseProgram(Handle);
}

// The shader sources provided with this project use hardcoded layout(location)-s. If you want to do it dynamically,
// you can omit the layout(location=X) lines in the vertex shader, and use this in VertexAttribPointer instead of the hardcoded values.
public int GetAttribLocation(string attribName)
{
return GL.GetAttribLocation(ID, attribName);
return GL.GetAttribLocation(Handle, attribName);
}

// Uniform setters
Expand All @@ -144,7 +144,7 @@ public int GetAttribLocation(string attribName)
/// <param name="data">The data to set</param>
public void SetInt(string name, int data)
{
GL.UseProgram(ID);
GL.UseProgram(Handle);
GL.Uniform1(_uniformLocations[name], data);
}

Expand All @@ -155,7 +155,7 @@ public void SetInt(string name, int data)
/// <param name="data">The data to set</param>
public void SetFloat(string name, float data)
{
GL.UseProgram(ID);
GL.UseProgram(Handle);
GL.Uniform1(_uniformLocations[name], data);
}

Expand All @@ -171,7 +171,7 @@ public void SetFloat(string name, float data)
/// </remarks>
public void SetMatrix4(string name, Matrix4 data)
{
GL.UseProgram(ID);
GL.UseProgram(Handle);
GL.UniformMatrix4(_uniformLocations[name], true, ref data);
}

Expand All @@ -182,7 +182,7 @@ public void SetMatrix4(string name, Matrix4 data)
/// <param name="data">The data to set</param>
public void SetVector3(string name, Vector3 data)
{
GL.UseProgram(ID);
GL.UseProgram(Handle);
GL.Uniform3(_uniformLocations[name], data);
}
}
Expand Down
11 changes: 4 additions & 7 deletions Common/Texture.cs
Expand Up @@ -12,10 +12,8 @@ public class Texture
public string type;
public string path;

public static Texture LoadFromFile(string path, string directory, string type)
public static Texture LoadFromFile(string filename, string type = "texture_diffuse")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally avoid default arguments in opentk code. Also I don't think this field is a good idea, and if we where going to do it I would rather this be an enum rather than a string.

{
string filename = new string(path);
filename = directory + '/' + filename;

// Generate handle ID
int ID = GL.GenTexture();
Expand All @@ -29,7 +27,7 @@ public static Texture LoadFromFile(string path, string directory, string type)
// OpenGL has it's texture origin in the lower left corner instead of the top left corner,
// so we tell StbImageSharp to flip the image when loading.
// StbImage.stbi_set_flip_vertically_on_load(1);

// Here we open a stream to the file and pass it to StbImageSharp to load.
using (Stream stream = File.OpenRead(filename))
{
Expand All @@ -48,7 +46,7 @@ public static Texture LoadFromFile(string path, string directory, string type)
// And finally, the actual pixels.
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, image.Width, image.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, image.Data);
}

// Now that our texture is loaded, we can set a few settings to affect how the image appears on rendering.

// First, we set the min and mag filter. These are used for when the texture is scaled down and up, respectively.
Expand All @@ -73,10 +71,9 @@ public static Texture LoadFromFile(string path, string directory, string type)
// Here is an example of mips in action https://en.wikipedia.org/wiki/File:Mipmap_Aliasing_Comparison.png
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);

return new Texture(ID, path, type);
return new Texture(ID, filename, type);
}


public Texture(int glHandle, string path, string type)
{
this.ID = glHandle;
Expand Down