-
Notifications
You must be signed in to change notification settings - Fork 115
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
DarkDoomBringer
wants to merge
7
commits into
opentk:master
Choose a base branch
from
DarkDoomBringer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ef752ef
Addition of Chapter 3 Part 1: Model Loading
DarkDoomBringer 2c4e290
NogginBops Review Changes 1
DarkDoomBringer b0fc78f
Made some changes regarding mesh data and transformation matrices
NogginBops 9b68ec0
Merge branch 'master' into master
NogginBops 08c48ec
Removed extra Using directives. Added and cleaned up comments. Update…
DarkDoomBringer 109cba9
Simplified the Model constructor to not have the extraneous LoadModel…
DarkDoomBringer 8567a66
Changed method for drawing the meshes to avoid inefficient abstractio…
DarkDoomBringer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+19.8 KB
Chapter3/1-ModelLoading/Resources/Backpack/internal_ground_ao_texture.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#version 330 | ||
|
||
in vec2 TexCoords; | ||
|
||
out vec4 outputColor; | ||
|
||
uniform sampler2D texture0; | ||
|
||
void main() | ||
{ | ||
outputColor = texture(texture0, TexCoords); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
using LearnOpenTK.Common; | ||
using OpenTK.Graphics.OpenGL4; | ||
using OpenTK.Mathematics; | ||
using OpenTK.Windowing.Common; | ||
using OpenTK.Windowing.GraphicsLibraryFramework; | ||
using OpenTK.Windowing.Desktop; | ||
|
||
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 _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 | ||
// 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. | ||
_backPack = 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); | ||
_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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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); | ||
} | ||
|
||
public void Draw() | ||
{ | ||
// Draw mesh | ||
GL.BindVertexArray(VAO); | ||
GL.DrawElements(PrimitiveType.Triangles, indicesCount, DrawElementsType.UnsignedInt, 0); | ||
GL.BindVertexArray(0); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
orattribution.txt
inResources/Backpack
where you put the sketchfab link, the license link and specify if any changes where made.