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

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

<ItemGroup>
<PackageReference Include="OpenTK" Version="4.4.0" />
<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.
18 changes: 18 additions & 0 deletions Chapter3/1-ModelLoading/Shaders/shader.frag
@@ -0,0 +1,18 @@
#version 330

out vec4 outputColor;

in vec2 TexCoords;

// A sampler2d is the representation of a texture in a shader.
// Each sampler is bound to a texture unit (texture units are described in Texture.cs on the Use function).
// By default, the unit is 0, so no code-related setup is actually needed.
// Multiple samplers will be demonstrated in section 1.5.
NogginBops marked this conversation as resolved.
Show resolved Hide resolved
uniform sampler2D texture0;

void main()
{
// To use a texture, you call the texture() function.
// It takes two parameters: the sampler to use, and a vec2, used as texture coordinates.
NogginBops marked this conversation as resolved.
Show resolved Hide resolved
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 Normal;
out vec3 FragPos;
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;
}
145 changes: 145 additions & 0 deletions Chapter3/1-ModelLoading/Window.cs
@@ -0,0 +1,145 @@
using System;
using LearnOpenTK.Common;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.GraphicsLibraryFramework;
using OpenTK.Windowing.Desktop;
using System.Reflection;

namespace LearnOpenTK
{
// In this tutorial we focus on how to set up a scene with multiple lights, both of different types but also
// with several point lights
public class Window : GameWindow
{
private Model _backPack;

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
_backPack = new Model("Resources/Backpack/Survival_BackPack_2.fbx");
_backPackShader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_backPackTexture = Texture.LoadFromFile("Resources/Backpack/1001_albedo.jpg");

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

CursorGrabbed = true;
}

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

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

_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);
_backPack.Draw();

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.4.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
</ItemGroup>
Expand Down
65 changes: 65 additions & 0 deletions Common/Mesh.cs
@@ -0,0 +1,65 @@

using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;

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

public class Mesh
{
// mesh data
readonly Vertex[] vertices;
readonly int[] indices;

public Mesh(Vertex[] vertices, int[] indices)
{
this.vertices = vertices;
this.indices = indices;

SetupMesh();
}

public void Draw()
{
// draw mesh
GL.BindVertexArray(VAO);
GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0);
GL.BindVertexArray(0);
}

// render data
private int VAO;

private void SetupMesh()
{
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 * 8 * sizeof(float), vertices, BufferUsageHint.StaticDraw);

GL.BindBuffer(BufferTarget.ElementArrayBuffer, EBO);
GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(uint), indices, BufferUsageHint.StaticDraw);

// vertex positions
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), 0);
// vertex normals
GL.EnableVertexAttribArray(1);
GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), 3 * sizeof(float));
// vertex texture coords
GL.EnableVertexAttribArray(2);
GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 8 * sizeof(float), 6 * sizeof(float));

GL.BindVertexArray(0);
}
}
}