-
-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* start of documentation - how to add textures v1 * texture implementation finished + some cool information about parameters * texture tutorial finished * fixing errors and adding information * fixing the source code * corrections * fix v2 Co-authored-by: Beyley Thomas <[email protected]> * + corrections * Perksey and ThomasMiz changing requests * ThomasMiz corrections 2 * ThomasMiz corrections 3 * Perksey corrections 2 * ThomasMiz corrections 4 * last corrections * Beyley corrections 2 * Beyley corrections 3 * Apply suggestions from code review Co-authored-by: Beyley Thomas <[email protected]> --------- Co-authored-by: Beyley Thomas <[email protected]> Co-authored-by: Dylan Perks <[email protected]>
- Loading branch information
1 parent
1f6b53d
commit b64fecf
Showing
18 changed files
with
669 additions
and
3 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,201 @@ | ||
# Final source code of Chapter 1, Tutorial 3. | ||
|
||
```c# | ||
using System; | ||
using System.Drawing; | ||
using Silk.NET.Maths; | ||
using Silk.NET.Windowing; | ||
using Silk.NET.OpenGL; | ||
using StbImageSharp; | ||
|
||
namespace MyProgram; | ||
|
||
public class Program | ||
{ | ||
private static IWindow _window; | ||
private static GL _gl; | ||
|
||
private static uint _vao; | ||
private static uint _vbo; | ||
private static uint _ebo; | ||
|
||
private static uint _program; | ||
|
||
private static uint _texture; | ||
|
||
public static void Main(string[] args) | ||
{ | ||
WindowOptions options = WindowOptions.Default; | ||
options.Size = new Vector2D<int>(800, 600); | ||
options.Title = "1.3 - Textures"; | ||
|
||
_window = Window.Create(options); | ||
|
||
_window.Load += OnLoad; | ||
_window.Update += OnUpdate; | ||
_window.Render += OnRender; | ||
_window.Run(); | ||
|
||
_window.Dispose(); | ||
_gl.Dispose(); | ||
} | ||
|
||
private static unsafe void OnLoad() | ||
{ | ||
_gl = _window.CreateOpenGL(); | ||
|
||
_gl.ClearColor(Color.CornflowerBlue); | ||
|
||
// Create the VAO. | ||
_vao = _gl.GenVertexArray(); | ||
_gl.BindVertexArray(_vao); | ||
|
||
// The quad vertices data. | ||
float[] vertices = | ||
{ | ||
// aPosition---- aTexCoords | ||
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, | ||
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, | ||
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, | ||
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f | ||
}; | ||
|
||
// Create the VBO. | ||
_vbo = _gl.GenBuffer(); | ||
_gl.BindBuffer(BufferTargetARB.ArrayBuffer, _vbo); | ||
|
||
// Upload the vertices data to the VBO. | ||
fixed (float* buf = vertices) | ||
_gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(vertices.Length * sizeof(float)), buf, BufferUsageARB.StaticDraw); | ||
|
||
// The quad indices data. | ||
uint[] indices = | ||
{ | ||
0u, 1u, 3u, | ||
1u, 2u, 3u | ||
}; | ||
|
||
// Create the EBO. | ||
_ebo = _gl.GenBuffer(); | ||
_gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _ebo); | ||
|
||
// Upload the indices data to the EBO. | ||
fixed (uint* buf = indices) | ||
_gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), buf, BufferUsageARB.StaticDraw); | ||
|
||
const string vertexCode = @" | ||
#version 330 core | ||
layout (location = 0) in vec3 aPosition; | ||
layout (location = 1) in vec2 aTextureCoord; | ||
out vec2 frag_texCoords; | ||
void main() | ||
{ | ||
gl_Position = vec4(aPosition, 1.0); | ||
frag_texCoords = aTextureCoord; | ||
}"; | ||
|
||
const string fragmentCode = @" | ||
#version 330 core | ||
in vec2 frag_texCoords; | ||
uniform sampler2D uTexture; | ||
out vec4 out_color; | ||
void main() | ||
{ | ||
// -out_color = vec4(frag_texCoords.x, frag_texCoords.y, 0, 1.0); | ||
out_color = texture(uTexture, frag_texCoords); | ||
}"; | ||
|
||
uint vertexShader = _gl.CreateShader(ShaderType.VertexShader); | ||
_gl.ShaderSource(vertexShader, vertexCode); | ||
|
||
_gl.CompileShader(vertexShader); | ||
|
||
_gl.GetShader(vertexShader, ShaderParameterName.CompileStatus, out int vStatus); | ||
if (vStatus != (int)GLEnum.True) | ||
throw new Exception("Vertex shader failed to compile: " + _gl.GetShaderInfoLog(vertexShader)); | ||
|
||
uint fragmentShader = _gl.CreateShader(ShaderType.FragmentShader); | ||
_gl.ShaderSource(fragmentShader, fragmentCode); | ||
|
||
_gl.CompileShader(fragmentShader); | ||
|
||
_gl.GetShader(fragmentShader, ShaderParameterName.CompileStatus, out int fStatus); | ||
if (fStatus != (int)GLEnum.True) | ||
throw new Exception("Fragment shader failed to compile: " + _gl.GetShaderInfoLog(fragmentShader)); | ||
|
||
_program = _gl.CreateProgram(); | ||
|
||
_gl.AttachShader(_program, vertexShader); | ||
_gl.AttachShader(_program, fragmentShader); | ||
|
||
_gl.LinkProgram(_program); | ||
|
||
_gl.GetProgram(_program, ProgramPropertyARB.LinkStatus, out int lStatus); | ||
if (lStatus != (int)GLEnum.True) | ||
throw new Exception("Program failed to link: " + _gl.GetProgramInfoLog(_program)); | ||
|
||
_gl.DetachShader(_program, vertexShader); | ||
_gl.DetachShader(_program, fragmentShader); | ||
_gl.DeleteShader(vertexShader); | ||
_gl.DeleteShader(fragmentShader); | ||
|
||
const uint positionLoc = 0; | ||
_gl.EnableVertexAttribArray(positionLoc); | ||
_gl.VertexAttribPointer(positionLoc, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), (void*)0); | ||
|
||
const uint texCoordLoc = 1; | ||
_gl.EnableVertexAttribArray(texCoordLoc); | ||
_gl.VertexAttribPointer(texCoordLoc, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), (void*)(3 * sizeof(float))); | ||
|
||
_gl.BindVertexArray(0); | ||
_gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0); | ||
_gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0); | ||
|
||
_texture = _gl.GenTexture(); | ||
_gl.ActiveTexture(TextureUnit.Texture0); | ||
_gl.BindTexture(TextureTarget.Texture2D, _texture); | ||
|
||
ImageResult result = ImageResult.FromMemory(File.ReadAllBytes("silk.png"), ColorComponents.RedGreenBlueAlpha); | ||
|
||
fixed (byte* ptr = result.Data) | ||
_gl.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba, (uint)result.Width, | ||
(uint)result.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, ptr); | ||
|
||
_gl.TexParameterI(GLEnum.Texture2D, GLEnum.TextureWrapS, (int)TextureWrapMode.Repeat); | ||
_gl.TexParameterI(GLEnum.Texture2D, GLEnum.TextureWrapT, (int)TextureWrapMode.Repeat); | ||
_gl.TexParameterI(GLEnum.Texture2D, GLEnum.TextureMinFilter, (int)TextureMinFilter.Nearest); | ||
_gl.TexParameterI(GLEnum.Texture2D, GLEnum.TextureMagFilter, (int)TextureMagFilter.Nearest); | ||
|
||
_gl.BindTexture(TextureTarget.Texture2D, 0); | ||
|
||
int location = _gl.GetUniformLocation(_program, "uTexture"); | ||
_gl.Uniform1(location, 0); | ||
|
||
_gl.Enable(EnableCap.Blend); | ||
_gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); | ||
} | ||
|
||
private static void OnUpdate(double deltaTime) { } | ||
|
||
private static unsafe void OnRender(double deltaTime) | ||
{ | ||
_gl.Clear(ClearBufferMask.ColorBufferBit); | ||
|
||
_gl.BindVertexArray(_vao); | ||
_gl.UseProgram(_program); | ||
|
||
_gl.ActiveTexture(TextureUnit.Texture0); | ||
_gl.BindTexture(TextureTarget.Texture2D, _texture); | ||
|
||
_gl.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, (void*)0); | ||
} | ||
} | ||
``` |
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.
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 added
BIN
+3.46 KB
website/images/opengl/chapter1/lesson3/texParameters/filter_linear.png
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
+3.46 KB
website/images/opengl/chapter1/lesson3/texParameters/filter_nearest.png
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
+14 KB
website/images/opengl/chapter1/lesson3/texParameters/quad-with-transparency.png
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 added
BIN
+53.6 KB
website/images/opengl/chapter1/lesson3/texParameters/repeatMirrored.png
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.