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

Addition of Chapter 3 Part 1: Model Loading #76

Open
wants to merge 7 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
24 changes: 24 additions & 0 deletions Chapter3/1-ModelLoading/1-ModelLoading.csproj
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<RootNamespace>LearnOpenTK</RootNamespace>
<AssemblyName>LearnOpenTK</AssemblyName>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ApplicationIcon />
<StartupObject />
</PropertyGroup>

<ItemGroup>
<None Include="Resources/**" CopyToOutputDirectory="PreserveNewest" />
<None Include="Shaders/**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenTK" Version="4.7.4" />
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Common\Common.csproj" />
</ItemGroup>
</Project>
25 changes: 25 additions & 0 deletions Chapter3/1-ModelLoading/OpenTK.dll.config
@@ -0,0 +1,25 @@
<configuration>
<dllmap os="linux" dll="opengl32.dll" target="libGL.so.1"/>
<dllmap os="linux" dll="glu32.dll" target="libGLU.so.1"/>
<dllmap os="linux" dll="openal32.dll" target="libopenal.so.1"/>
<dllmap os="linux" dll="alut.dll" target="libalut.so.0"/>
<dllmap os="linux" dll="opencl.dll" target="libOpenCL.so"/>
<dllmap os="linux" dll="libX11" target="libX11.so.6"/>
<dllmap os="linux" dll="libXi" target="libXi.so.6"/>
<dllmap os="linux" dll="SDL2.dll" target="libSDL2-2.0.so.0"/>
<dllmap os="osx" dll="opengl32.dll" target="/System/Library/Frameworks/OpenGL.framework/OpenGL"/>
<dllmap os="osx" dll="openal32.dll" target="/System/Library/Frameworks/OpenAL.framework/OpenAL" />
<dllmap os="osx" dll="alut.dll" target="/System/Library/Frameworks/OpenAL.framework/OpenAL" />
<dllmap os="osx" dll="libGLES.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="libGLESv1_CM.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="libGLESv2.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="opencl.dll" target="/System/Library/Frameworks/OpenCL.framework/OpenCL"/>
<dllmap os="osx" dll="SDL2.dll" target="libSDL2.dylib"/>
<!-- XQuartz compatibility (X11 on Mac) -->
<dllmap os="osx" dll="libGL.so.1" target="/usr/X11/lib/libGL.dylib"/>
<dllmap os="osx" dll="libX11" target="/usr/X11/lib/libX11.dylib"/>
<dllmap os="osx" dll="libXcursor.so.1" target="/usr/X11/lib/libXcursor.dylib"/>
<dllmap os="osx" dll="libXi" target="/usr/X11/lib/libXi.dylib"/>
<dllmap os="osx" dll="libXinerama" target="/usr/X11/lib/libXinerama.dylib"/>
<dllmap os="osx" dll="libXrandr.so.2" target="/usr/X11/lib/libXrandr.dylib"/>
</configuration>
25 changes: 25 additions & 0 deletions Chapter3/1-ModelLoading/Program.cs
@@ -0,0 +1,25 @@
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;

namespace LearnOpenTK
{
public static class Program
{
private static void Main()
{
var nativeWindowSettings = new NativeWindowSettings()
{
Size = new Vector2i(800, 600),
Title = "LearnOpenTK - Model Loading",
// This is needed to run on macos
Flags = ContextFlags.ForwardCompatible,
};

using (var window = new Window(GameWindowSettings.Default, nativeWindowSettings))
{
window.Run();
}
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions Chapter3/1-ModelLoading/Shaders/shader.frag
@@ -0,0 +1,12 @@
#version 330

in vec2 TexCoords;

out vec4 outputColor;

uniform sampler2D texture0;

void main()
{
outputColor = texture(texture0, TexCoords);
}
20 changes: 20 additions & 0 deletions Chapter3/1-ModelLoading/Shaders/shader.vert
@@ -0,0 +1,20 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoords;

void main()
{
gl_Position = vec4(aPos, 1.0) * model * view * projection;
FragPos = vec3(vec4(aPos, 1.0) * model);
Normal = aNormal * mat3(transpose(inverse(model)));
TexCoords = aTexCoords;
}
152 changes: 152 additions & 0 deletions Chapter3/1-ModelLoading/Window.cs
@@ -0,0 +1,152 @@
using LearnOpenTK.Common;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;

namespace LearnOpenTK
{
// In this tutorial we focus on importing a model using Assimp and then drawing the model in our scene.
public class Window : GameWindow
{
private Model _backPackModel;

private Shader _backPackShader;

private Texture _backPackTexture;

private Camera _camera;

private bool _firstMove = true;

private Vector2 _lastPos;

public Window(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings)
: base(gameWindowSettings, nativeWindowSettings)
{
}

protected override void OnLoad()
{
base.OnLoad();

GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);

GL.Enable(EnableCap.DepthTest);

// Backpack by Berk Gedik: https://sketchfab.com/3d-models/survival-guitar-backpack-low-poly-799f8c4511f84fab8c3f12887f7e6b36
Copy link
Member

Choose a reason for hiding this comment

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

According to the license the model is under you need to not only give attribution to the creator but also link the license.
You also need to specify if any changes where made to the work.

I would suggest creating a license.txt or attribution.txt in Resources/Backpack where you put the sketchfab link, the license link and specify if any changes where made.

// Here we import the backpack model object, texture and define our Shader.
// For now we are just using a simple shader which just samples the texture.
_backPackModel = new Model("Resources/Backpack/Survival_BackPack_2.fbx");
_backPackTexture = Texture.LoadFromFile("Resources/Backpack/1001_albedo.jpg");
_backPackShader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");

_camera = new Camera(Vector3.UnitZ * 3, Size.X / (float)Size.Y);

CursorState = CursorState.Grabbed;
}

protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);

GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

// 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();
_backPackShader.SetMatrix4("model", Matrix4.Identity);
_backPackShader.SetMatrix4("view", _camera.GetViewMatrix());
_backPackShader.SetMatrix4("projection", _camera.GetProjectionMatrix());
_backPackTexture.Use(TextureUnit.Texture0);
_backPackShader.SetInt("texture0", 0);

// For each mesh in the model, bind the VAO (that contains all of the vertices and indices) and draw the triangles
foreach (Mesh mesh in _backPackModel.meshes)
{
GL.BindVertexArray(mesh.VAO);
GL.DrawElements(PrimitiveType.Triangles, mesh.indicesCount, DrawElementsType.UnsignedInt, 0);
GL.BindVertexArray(0);
}

SwapBuffers();
}

protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);

if (!IsFocused)
{
return;
}

var input = KeyboardState;

if (input.IsKeyDown(Keys.Escape))
{
Close();
}

const float cameraSpeed = 1.5f;
const float sensitivity = 0.2f;

if (input.IsKeyDown(Keys.W))
{
_camera.Position += _camera.Front * cameraSpeed * (float)e.Time; // Forward
}
if (input.IsKeyDown(Keys.S))
{
_camera.Position -= _camera.Front * cameraSpeed * (float)e.Time; // Backwards
}
if (input.IsKeyDown(Keys.A))
{
_camera.Position -= _camera.Right * cameraSpeed * (float)e.Time; // Left
}
if (input.IsKeyDown(Keys.D))
{
_camera.Position += _camera.Right * cameraSpeed * (float)e.Time; // Right
}
if (input.IsKeyDown(Keys.Space))
{
_camera.Position += _camera.Up * cameraSpeed * (float)e.Time; // Up
}
if (input.IsKeyDown(Keys.LeftShift))
{
_camera.Position -= _camera.Up * cameraSpeed * (float)e.Time; // Down
}

var mouse = MouseState;

if (_firstMove)
{
_lastPos = new Vector2(mouse.X, mouse.Y);
_firstMove = false;
}
else
{
var deltaX = mouse.X - _lastPos.X;
var deltaY = mouse.Y - _lastPos.Y;
_lastPos = new Vector2(mouse.X, mouse.Y);

_camera.Yaw += deltaX * sensitivity;
_camera.Pitch -= deltaY * sensitivity;
}
}

protected override void OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);

_camera.Fov -= e.OffsetY;
}

protected override void OnResize(ResizeEventArgs e)
{
base.OnResize(e);

GL.Viewport(0, 0, Size.X, Size.Y);
_camera.AspectRatio = Size.X / (float)Size.Y;
}
}
}
1 change: 1 addition & 0 deletions Common/Common.csproj
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AssimpNet" Version="4.1.0" />
<PackageReference Include="OpenTK" Version="4.7.4" />
<PackageReference Include="StbImageSharp" Version="2.27.11" />
</ItemGroup>
Expand Down
49 changes: 49 additions & 0 deletions Common/Mesh.cs
@@ -0,0 +1,49 @@
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace LearnOpenTK.Common
{
public struct Vertex
{
public Vector3 Position;
public Vector3 Normal;
public Vector2 TexCoords;
}

public class Mesh
{
public readonly int VAO;
public readonly int indicesCount;

public Mesh(Span<Vertex> vertices, Span<int> indices)
{
indicesCount = indices.Length;

VAO = GL.GenVertexArray();
int VBO = GL.GenBuffer();
int EBO = GL.GenBuffer();

GL.BindVertexArray(VAO);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * Unsafe.SizeOf<Vertex>(), ref MemoryMarshal.GetReference(vertices), BufferUsageHint.StaticDraw);

GL.BindBuffer(BufferTarget.ElementArrayBuffer, EBO);
GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(int), ref MemoryMarshal.GetReference(indices), BufferUsageHint.StaticDraw);

// Vertex positions
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Unsafe.SizeOf<Vertex>(), Marshal.OffsetOf<Vertex>(nameof(Vertex.Position)));
// Vertex normals
GL.EnableVertexAttribArray(1);
GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, Unsafe.SizeOf<Vertex>(), Marshal.OffsetOf<Vertex>(nameof(Vertex.Normal)));
// Vertex texture coords
GL.EnableVertexAttribArray(2);
GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, Unsafe.SizeOf<Vertex>(), Marshal.OffsetOf<Vertex>(nameof(Vertex.TexCoords)));

GL.BindVertexArray(0);
}
}
}