diff --git a/.gitignore b/.gitignore index 5028678..705b307 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ build/ *.o compile_commands.json .idea/ +bin/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..16e619c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "06-monogame/Introduction/themes/hugo-bearcub"] + path = 06-monogame/Introduction/themes/hugo-bearcub + url = https://github.com/clente/hugo-bearcub diff --git a/.imgs/monogame.png b/.imgs/monogame.png new file mode 100644 index 0000000..89a5770 Binary files /dev/null and b/.imgs/monogame.png differ diff --git a/.imgs/opengl.png b/.imgs/opengl.png new file mode 100644 index 0000000..86590b8 Binary files /dev/null and b/.imgs/opengl.png differ diff --git a/.imgs/icon.png b/.imgs/sdl.png similarity index 100% rename from .imgs/icon.png rename to .imgs/sdl.png diff --git a/05-opengl/assets/shaders/fragment.glsl b/05-opengl/assets/shaders/fragment.glsl index fd74ddb..4c59ebf 100644 --- a/05-opengl/assets/shaders/fragment.glsl +++ b/05-opengl/assets/shaders/fragment.glsl @@ -2,10 +2,10 @@ out vec4 color; in vec2 tex_coord; -uniform sampler2D texture1; +uniform sampler2D sampler; void main() { - color = texture(texture1, tex_coord); + color = texture(sampler, tex_coord); // color = vec4(1.0, 0.0, 0.0, 1.0); if (color.a < 0.1) diff --git a/05-opengl/assets/shaders/vertex.glsl b/05-opengl/assets/shaders/vertex.glsl index 0ed2722..e1776fe 100644 --- a/05-opengl/assets/shaders/vertex.glsl +++ b/05-opengl/assets/shaders/vertex.glsl @@ -3,18 +3,12 @@ layout (location = 0) in vec3 pos; layout (location = 1) in vec2 texture_coord; -uniform vec3 size; -uniform ivec3 offset; -uniform mat4 model; uniform mat4 view; uniform mat4 projection; out vec2 tex_coord; void main() { - vec3 new_pos = pos.xyz + offset.xyz; - new_pos = new_pos.xyz * size.xyz; - - gl_Position = projection * view * model * vec4(new_pos.xyz, 1.0); + gl_Position = projection * view * vec4(pos.xyz, 1.0); tex_coord = texture_coord; } diff --git a/05-opengl/src/game.cpp b/05-opengl/src/game.cpp index cce5642..1ce6eb3 100644 --- a/05-opengl/src/game.cpp +++ b/05-opengl/src/game.cpp @@ -13,7 +13,10 @@ Game::Game(GLFWwindow *window) { this->window = window; this->program = Program(); + stbi_set_flip_vertically_on_load(true); + + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &this->texture_slots); } // code this later lol @@ -41,14 +44,40 @@ int Game::setup(int initial_width, int initial_height) { } glUseProgram(this->program.id); + + glGenVertexArrays(1, &this->vao); + glGenBuffers(1, &this->vbo); + glGenBuffers(1, &this->ebo); + + glBindVertexArray(this->vao); + glBindBuffer(GL_ARRAY_BUFFER, this->vbo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->ebo); + return 0; } void Game::run() { + this->counter = 0; + this->vbo_offset = 0; + this->ebo_offset = 0; + Sprite sprite("assets/player/idle.png", SPLIT_TEXTURE); sprite.bake(); this->sprites.push_back(&sprite); + glBindVertexArray(this->vao); + glBindBuffer(GL_ARRAY_BUFFER, this->vbo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->ebo); + + glBufferData(GL_ARRAY_BUFFER, sizeof(struct vertex) * 4 * this->texture_slots, nullptr, GL_DYNAMIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * 6 * this->texture_slots, nullptr, GL_DYNAMIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(struct vertex), (void *) offsetof(vertex, position)); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(struct vertex), (void *) offsetof(vertex, texture)); + glEnableVertexAttribArray(1); + Game::logic(); } @@ -74,8 +103,24 @@ void Game::logic() { glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); - for (int i = 0; i < this->sprites.size(); i++) - Game::draw(this->sprites[i]); + + for (int sprite = 0; sprite < this->sprites.size(); sprite++, counter++) { + Game::load(this->sprites[sprite], this->counter); + + std::cout << "Counter: " << counter << " Texture slots: " << this->texture_slots << std::endl; + if (counter > 0 && counter % this->texture_slots == 0) { + Game::draw(); + this->counter = 0; + this->vbo_offset = 0; + this->ebo_offset = 0; + } + + } + + Game::draw(); + this->counter = 0; + this->vbo_offset = 0; + this->ebo_offset = 0; glfwPollEvents(); glfwSwapBuffers(this->window); @@ -84,16 +129,25 @@ void Game::logic() { glBindVertexArray(0); } -void Game::draw(Sprite *sprite) { +void Game::load(Sprite *sprite, int count) { unsigned int program = this->program.id; glUseProgram(program); - glUniform3i(glGetUniformLocation(program, "offset"), sprite->pos.x, sprite->pos.y, sprite->pos.z); - glUniform3f(glGetUniformLocation(program, "size"), sprite->size.x, sprite->size.y, sprite->size.z); - glUniform1i(glGetUniformLocation(program, "texture1"), 0); + glUniform1i(glGetUniformLocation(program, "sampler"), count); - glm::mat4 model = glm::mat4(1.0f); - model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); + glBufferSubData(GL_ARRAY_BUFFER, vbo_offset, sizeof(struct vertex) * sprite->vertices.size(), sprite->vertices.data()); + glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, ebo_offset, sizeof(int) * sprite->indices.size(), sprite->indices.data()); + + vbo_offset += sizeof(struct vertex) * sprite->vertices.size(); + ebo_offset += sizeof(int) * sprite->indices.size(); + + glActiveTexture(GL_TEXTURE0 + count); + glBindTexture(GL_TEXTURE_2D, sprite->texture.texture_id); +} + +void Game::draw() { + unsigned int program = this->program.id; + glUseProgram(program); glm::mat4 view = glm::mat4(1.0f); view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); @@ -109,19 +163,10 @@ void Game::draw(Sprite *sprite) { projection = glm::ortho(-half_width/100, half_width/100, -half_height/100, half_height/100, 0.1f, 100.0f); - glUniformMatrix4fv(glGetUniformLocation(program, "model"), 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(glGetUniformLocation(program,"view"), 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); - glBindVertexArray(sprite->vao); - glBindBuffer(GL_ARRAY_BUFFER, sprite->vbo); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sprite->ebo); + glDrawElements(GL_TRIANGLES, this->sprites[0]->indices.size(), GL_UNSIGNED_INT, 0); + std::cout << "Error: " << glGetError() << std::endl; - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(struct vertex) * sprite->vertices.size(), sprite->vertices.data()); - glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(int) * sprite->indices.size(), sprite->indices.data()); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, sprite->texture.texture_id); - - glDrawElements(GL_TRIANGLES, sprite->indices.size(), GL_UNSIGNED_INT, 0); } diff --git a/05-opengl/src/game.hpp b/05-opengl/src/game.hpp index fec1f6e..8c63811 100644 --- a/05-opengl/src/game.hpp +++ b/05-opengl/src/game.hpp @@ -12,15 +12,23 @@ public: int setup(int initial_width, int initial_height); void run(); void logic(); - void draw(Sprite *sprite); + void load(Sprite *sprite, int counter); + void draw(); // callbacks static void input_callback(GLFWwindow *window, int key, int scancode, int action, int mods); static void resize_callback(GLFWwindow *window, int new_width, int new_height); GLFWwindow *window; + Program program; std::vector sprites; - Program program; + int texture_slots; + unsigned int vao; + unsigned int vbo; + unsigned int ebo; + unsigned int counter; + unsigned int vbo_offset; + unsigned int ebo_offset; }; diff --git a/05-opengl/src/sprite.cpp b/05-opengl/src/sprite.cpp index 21892fa..37b3886 100644 --- a/05-opengl/src/sprite.cpp +++ b/05-opengl/src/sprite.cpp @@ -23,10 +23,6 @@ Sprite::Sprite(const char *path, enum TextureType type) : texture(type) { stbi_image_free(image_bytes); - glGenVertexArrays(1, &this->vao); - glGenBuffers(1, &this->vbo); - glGenBuffers(1, &this->ebo); - this->pos = glm::vec3(0.0f); this->size = glm::vec3(1.0f); this->rot = glm::vec3(0.0f); @@ -60,20 +56,4 @@ void Sprite::bake() { v->texture[1] = tex_y; } } - - glBindVertexArray(this->vao); - - glBindBuffer(GL_ARRAY_BUFFER, this->vbo); - glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(struct vertex), nullptr, GL_DYNAMIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(int), nullptr, GL_DYNAMIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(struct vertex), (void *) offsetof(vertex, position)); - glEnableVertexAttribArray(0); - - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(struct vertex), (void *) offsetof(vertex, texture)); - glEnableVertexAttribArray(1); - - glBindVertexArray(0); } diff --git a/05-opengl/src/sprite.hpp b/05-opengl/src/sprite.hpp index dc0c215..bf8f8dc 100644 --- a/05-opengl/src/sprite.hpp +++ b/05-opengl/src/sprite.hpp @@ -22,10 +22,6 @@ public: std::vector vertices; std::vector indices; - unsigned int vao; - unsigned int vbo; - unsigned int ebo; - Game *game; Sprite(const char *path, enum TextureType type); diff --git a/06-monogame/.gitignore b/06-monogame/.gitignore new file mode 100644 index 0000000..bfca666 --- /dev/null +++ b/06-monogame/.gitignore @@ -0,0 +1,43 @@ +*.swp +*.*~ +project.lock.json +.DS_Store +*.pyc +nupkg/ + +# Visual Studio Code +.vscode/ + +# Rider +.idea/ + +# Visual Studio +.vs/ + +# Fleet +.fleet/ + +# Code Rush +.cr/ + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +msbuild.log +msbuild.err +msbuild.wrn diff --git a/06-monogame/Introduction/.config/dotnet-tools.json b/06-monogame/Introduction/.config/dotnet-tools.json new file mode 100644 index 0000000..efabe22 --- /dev/null +++ b/06-monogame/Introduction/.config/dotnet-tools.json @@ -0,0 +1,36 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-mgcb": { + "version": "3.8.1.303", + "commands": [ + "mgcb" + ] + }, + "dotnet-mgcb-editor": { + "version": "3.8.1.303", + "commands": [ + "mgcb-editor" + ] + }, + "dotnet-mgcb-editor-linux": { + "version": "3.8.1.303", + "commands": [ + "mgcb-editor-linux" + ] + }, + "dotnet-mgcb-editor-windows": { + "version": "3.8.1.303", + "commands": [ + "mgcb-editor-windows" + ] + }, + "dotnet-mgcb-editor-mac": { + "version": "3.8.1.303", + "commands": [ + "mgcb-editor-mac" + ] + } + } +} \ No newline at end of file diff --git a/06-monogame/Introduction/Character.cs b/06-monogame/Introduction/Character.cs new file mode 100644 index 0000000..c2c1a35 --- /dev/null +++ b/06-monogame/Introduction/Character.cs @@ -0,0 +1,100 @@ +using System; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using Vector2 = Microsoft.Xna.Framework.Vector2; + +namespace Introduction; + +public class Character +{ + public Texture2D texture; + public Texture2D noise; + public Vector2 pos; + public Vector2 scale; + public Vector2 rot; + public float angles; + public float speed; + + private bool glitchEnabled = false; + private Effect glitch; + private float time; + + public void Initialize() + { + speed = 300.0f; + pos = Vector2.Zero; + scale = new Vector2(1f); + rot = Vector2.UnitX; + angles = 0.0f; + } + + public void LoadContent(ContentManager content) + { + texture = content.Load("player_texture"); + noise = content.Load("noise"); + glitch = content.Load("glitch"); + + if (glitch.Parameters["NoiseTexture"] is not null) + glitch.Parameters["NoiseTexture"].SetValue(texture); + + if (glitch.Parameters["textureSize"] is not null) + glitch.Parameters["textureSize"].SetValue(new Vector2(texture.Width, texture.Height)); + } + + public void Update(GameTime gameTime) + { + var kstate = Keyboard.GetState(); + if (kstate.IsKeyDown(Keys.W)) + { + pos.Y -= speed * (float)gameTime.ElapsedGameTime.TotalSeconds; + } + + if (kstate.IsKeyDown(Keys.S)) + { + pos.Y += speed * (float)gameTime.ElapsedGameTime.TotalSeconds; + } + + if (kstate.IsKeyDown(Keys.A)) + { + pos.X -= speed * (float)gameTime.ElapsedGameTime.TotalSeconds; + } + + if (kstate.IsKeyDown(Keys.D)) + { + pos.X += speed * (float)gameTime.ElapsedGameTime.TotalSeconds; + } + + if (kstate.IsKeyDown(Keys.K)) + { + glitchEnabled = !glitchEnabled; + } + + var mstate = Mouse.GetState(); + Vector2 mousePosition = new Vector2(mstate.X, mstate.Y); + Vector2 distancePosition = mousePosition - pos; + + angles = (float) (Math.Atan2(distancePosition.Y, distancePosition.X)); + + time += (float) gameTime.ElapsedGameTime.TotalSeconds; + + if (glitch.Parameters["Time"] is not null) + glitch.Parameters["Time"].SetValue(time); + } + + public void Draw(GameTime gameTime, SpriteBatch spriteBatch) + { + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); + Vector2 origin = new Vector2(texture.Width / 2, texture.Height / 2); + + if (glitchEnabled) + glitch.CurrentTechnique.Passes[0].Apply(); + else + time = 0; + + spriteBatch.Draw(texture, pos, null, Color.White, angles, origin, scale, SpriteEffects.None, 0.0f); + spriteBatch.End(); + } +} \ No newline at end of file diff --git a/06-monogame/Introduction/Content/Content.mgcb b/06-monogame/Introduction/Content/Content.mgcb new file mode 100644 index 0000000..90dcd56 --- /dev/null +++ b/06-monogame/Introduction/Content/Content.mgcb @@ -0,0 +1,45 @@ + +#----------------------------- Global Properties ----------------------------# + +/outputDir:bin/$(Platform) +/intermediateDir:obj/$(Platform) +/platform:DesktopGL +/config: +/profile:Reach +/compress:False + +#-------------------------------- References --------------------------------# + + +#---------------------------------- Content ---------------------------------# + +#begin glitch.fx +/importer:EffectImporter +/processor:EffectProcessor +/processorParam:DebugMode=Auto +/build:glitch.fx + +#begin noise.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:noise.png + +#begin player_texture.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:player_texture.png + diff --git a/06-monogame/Introduction/Content/glitch.fx b/06-monogame/Introduction/Content/glitch.fx new file mode 100755 index 0000000..3c09bed --- /dev/null +++ b/06-monogame/Introduction/Content/glitch.fx @@ -0,0 +1,47 @@ +#if OPENGL + #define SV_POSITION POSITION + #define VS_SHADERMODEL vs_3_0 + #define PS_SHADERMODEL ps_3_0 +#else + #define VS_SHADERMODEL vs_4_0_level_9_1 + #define PS_SHADERMODEL ps_4_0_level_9_1 +#endif + +matrix WorldViewProjection; + +sampler s0 : register (s0); +float Time; +float2 textureSize; + +Texture2D NoiseTexture; +sampler2D NoiseSampler = sampler_state +{ + Texture = ; +}; + +float4 MainPS(float2 coords: TEXCOORD0) : COLOR0 +{ + float2 up_limit = float2(0.0, 0.0) + float2(Time, Time); + float2 down_limit = textureSize - float2(Time, Time); + + float4 output_col; + if (coords.x < up_limit.x || coords.y < up_limit.y) { + output_col = float4(0.0, 0.0, 0.0, 0.0); + return output_col; + } + + if (coords.x > down_limit.x || coords.y > down_limit.y) { + output_col = float4(0.0, 0.0, 0.0, 0.0); + return output_col; + } + + return tex2D(s0, coords); +} + +technique BasicColorDrawing +{ + pass P0 + { + PixelShader = compile PS_SHADERMODEL MainPS(); + } +}; \ No newline at end of file diff --git a/06-monogame/Introduction/Content/noise.png b/06-monogame/Introduction/Content/noise.png new file mode 100644 index 0000000..aefe249 Binary files /dev/null and b/06-monogame/Introduction/Content/noise.png differ diff --git a/06-monogame/Introduction/Content/player_texture.png b/06-monogame/Introduction/Content/player_texture.png new file mode 100644 index 0000000..9b2de9a Binary files /dev/null and b/06-monogame/Introduction/Content/player_texture.png differ diff --git a/06-monogame/Introduction/Game1.cs b/06-monogame/Introduction/Game1.cs new file mode 100644 index 0000000..fd67519 --- /dev/null +++ b/06-monogame/Introduction/Game1.cs @@ -0,0 +1,53 @@ +using System.Data; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; + +namespace Introduction; + +public class Game1 : Game +{ + private GraphicsDeviceManager _graphics; + private SpriteBatch _spriteBatch; + private Character player; + + public Game1() + { + _graphics = new GraphicsDeviceManager(this); + Content.RootDirectory = "Content"; + IsMouseVisible = true; + player = new Character(); + } + + protected override void Initialize() + { + player.Initialize(); + + base.Initialize(); + } + + protected override void LoadContent() + { + _spriteBatch = new SpriteBatch(GraphicsDevice); + player.LoadContent(Content); + } + + protected override void Update(GameTime gameTime) + { + if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) + Exit(); + + player.Update(gameTime); + + base.Update(gameTime); + } + + protected override void Draw(GameTime gameTime) + { + GraphicsDevice.Clear(Color.CornflowerBlue); + + player.Draw(gameTime, _spriteBatch); + + base.Draw(gameTime); + } +} diff --git a/06-monogame/Introduction/Icon.bmp b/06-monogame/Introduction/Icon.bmp new file mode 100644 index 0000000..2b48165 Binary files /dev/null and b/06-monogame/Introduction/Icon.bmp differ diff --git a/06-monogame/Introduction/Icon.ico b/06-monogame/Introduction/Icon.ico new file mode 100644 index 0000000..7d9dec1 Binary files /dev/null and b/06-monogame/Introduction/Icon.ico differ diff --git a/06-monogame/Introduction/Introduction.csproj b/06-monogame/Introduction/Introduction.csproj new file mode 100644 index 0000000..a7fb71d --- /dev/null +++ b/06-monogame/Introduction/Introduction.csproj @@ -0,0 +1,29 @@ + + + WinExe + net6.0 + Major + false + false + + + app.manifest + Icon.ico + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/06-monogame/Introduction/Program.cs b/06-monogame/Introduction/Program.cs new file mode 100644 index 0000000..746e1a8 --- /dev/null +++ b/06-monogame/Introduction/Program.cs @@ -0,0 +1,3 @@ + +using var game = new Introduction.Game1(); +game.Run(); diff --git a/06-monogame/Introduction/app.manifest b/06-monogame/Introduction/app.manifest new file mode 100644 index 0000000..556a3d3 --- /dev/null +++ b/06-monogame/Introduction/app.manifest @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true/pm + permonitorv2,permonitor + + + + diff --git a/README.md b/README.md index 444432d..8796f44 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@

- + + +

# Graphics