Fixed bitmap resize

This commit is contained in:
Absolutely disgusting
2026-03-04 21:26:54 +04:00
parent 34e5993970
commit c88038e38a
4 changed files with 140 additions and 73 deletions

View File

@@ -215,6 +215,17 @@ namespace _2DGAMELIB
resVector.Y = ((double)height - (double)BaseSize.Height / resMag) * 0.5;
}
}
int fbW, fbH;
Glfw.GetFramebufferSize(GlImage.PtrToWindow(window), out fbW, out fbH);
uint vpW = (uint)(BaseSize.Width / resMag);
uint vpH = (uint)(BaseSize.Height / resMag);
int vpX = (int)(resVector.X);
int vpY = (int)(fbH - resVector.Y - vpH);
baseControl.SetViewport(vpW, vpH, vpX, vpY);
};
return BaseSize;

View File

@@ -37,9 +37,16 @@ namespace _2DGAMELIB
private uint texture;
private uint vertex_buf;
private uint vao;
public int texW = 0;
public int texH = 0;
public GlImage() { }
public void SetViewport(uint vpW, uint vpH, int vpX, int vpY)
{
gl.Viewport(vpX, vpY, vpW, vpH);
}
public Vector2D GetCursorPoint() {
double x, y;
Glfw.GetCursorPosition(window, out x, out y);
@@ -92,13 +99,11 @@ namespace _2DGAMELIB
public unsafe void SetBitmap(Bitmap bmp)
{
gl.UseProgram(shader_program);
gl.Viewport(new Size(bmp.Width, bmp.Height));
gl.ActiveTexture(Silk.NET.OpenGL.GLEnum.Texture0);
gl.BindTexture(Silk.NET.OpenGL.GLEnum.Texture2D, texture);
BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
if (bmp.Width != texW || bmp.Height != texH)
{
gl.TexImage2D(
Silk.NET.OpenGL.GLEnum.Texture2D,
0,
@@ -108,32 +113,33 @@ namespace _2DGAMELIB
0,
Silk.NET.OpenGL.GLEnum.Bgra,
Silk.NET.OpenGL.GLEnum.UnsignedByte,
(void*)data.Scan0
);
null);
texW = bmp.Width;
texH = bmp.Height;
}
BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
gl.PixelStore(GLEnum.UnpackAlignment, 1);
gl.TexSubImage2D(
Silk.NET.OpenGL.GLEnum.Texture2D,
0,
0,
0,
(uint)bmp.Width,
(uint)bmp.Height,
Silk.NET.OpenGL.GLEnum.Bgra,
Silk.NET.OpenGL.GLEnum.UnsignedByte,
(void*)data.Scan0);
bmp.UnlockBits(data);
int res_pos = gl.GetUniformLocation(shader_program, "res");
gl.Uniform2(res_pos, (float)bmp.Width, (float)bmp.Height);
gl.BindBuffer(GLEnum.ArrayBuffer, vertex_buf);
uint vert_pos = (uint)gl.GetAttribLocation(shader_program, "vertPos");
gl.EnableVertexAttribArray(vert_pos);
gl.BindVertexArray(vao);
gl.VertexAttribPointer(
vert_pos,
2,
GLEnum.Float,
false,
0,
IntPtr.Zero
);
gl.DrawArrays(GLEnum.TriangleStrip, 0, 4);
gl.BindVertexArray(0);
Glfw.SwapBuffers(window);
}
@@ -164,31 +170,33 @@ namespace _2DGAMELIB
string vertexShaderSource =
@"
#version 100
precision mediump float;
#version 330 core
attribute vec2 vertPos;
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aUV;
out vec2 vUV;
void main()
{
gl_Position = vec4(vertPos, 0.0, 1.0);
gl_Position = vec4(aPos, 0.0f, 1.0f);
vUV = aUV;
}
";
string fragmentShaderSource =
@"
#version 100
precision mediump float;
#version 330 core
in vec2 vUV;
out vec4 FragColor;
uniform sampler2D sTexture;
uniform vec2 res;
void main()
{
vec2 tc = gl_FragCoord.xy / res;
tc.y = 1.0 - tc.y;
gl_FragColor = texture2D(sTexture, tc);
FragColor = texture(sTexture, vUV);
}
";
@@ -223,13 +231,61 @@ void main()
gl.TexParameterI(Silk.NET.OpenGL.GLEnum.Texture2D, Silk.NET.OpenGL.GLEnum.TextureMagFilter, new int[] {(int)TextureMagFilter.Nearest});
gl.TexParameterI(Silk.NET.OpenGL.GLEnum.Texture2D, Silk.NET.OpenGL.GLEnum.TextureMinFilter, new int[] {(int)TextureMinFilter.Nearest});
gl.TexImage2D(
Silk.NET.OpenGL.GLEnum.Texture2D,
0,
InternalFormat.Rgba8,
(uint)bmp.Width,
(uint)bmp.Height,
0,
Silk.NET.OpenGL.GLEnum.Bgra,
Silk.NET.OpenGL.GLEnum.UnsignedByte,
null
);
texW = bmp.Width;
texH = bmp.Height;
float[] buf = {
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f
};
float[] buf = { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f };
vertex_buf = gl.GenBuffer();
gl.BindBuffer(Silk.NET.OpenGL.GLEnum.ArrayBuffer, vertex_buf);
fixed (float* buf_ = buf) gl.BufferData(Silk.NET.OpenGL.GLEnum.ArrayBuffer, (uint)(sizeof(float) * buf.Length), buf_, Silk.NET.OpenGL.GLEnum.StaticDraw);
vao = gl.GenVertexArray();
gl.BindVertexArray(vao);
gl.BindBuffer(GLEnum.ArrayBuffer, vertex_buf);
gl.VertexAttribPointer(
0,
2,
GLEnum.Float,
false,
4 * sizeof(float),
IntPtr.Zero
);
gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(
1,
2,
GLEnum.Float,
false,
4 * sizeof(float),
(void*)(2 * sizeof(float))
);
gl.EnableVertexAttribArray(1);
gl.BindVertexArray(0);
}
}
}

View File

@@ -2,7 +2,7 @@ using _2DGAMELIB;
namespace SlaveMatrix
{
public class _デンマ : Ele
public class _デンマ : Ele //baibu_denma
{
public Par X0Y0_ヘッド;

View File

@@ -902,7 +902,7 @@ namespace SlaveMatrix
return t[num][num2];
}
public static List<string[]> (this string s)
public static List<string[]> Parse(this string s)
{
List<string[]> list = new List<string[]>();
string[] array = s.Split("\r\n\r\n");
@@ -921,29 +921,29 @@ namespace SlaveMatrix
public static void Set擬音()
{
string[] array = ImiPath.FromText().Split(',');
= array[0].();
= array[1].();
= array[2].();
= array[3].();
= array[4].();
= array[5].();
= array[6].();
= array[7].();
= array[8].();
= array[9].();
= array[10].();
= array[11].();
= array[12].();
尿 = array[13].();
= array[14].();
= array[15].();
= array[16].();
= array[17].();
= array[18].();
= array[19].();
= array[20].();
= array[21].();
= array[22].();
= array[0].Parse();
= array[1].Parse();
= array[2].Parse();
= array[3].Parse();
= array[4].Parse();
= array[5].Parse();
= array[6].Parse();
= array[7].Parse();
= array[8].Parse();
= array[9].Parse();
= array[10].Parse();
= array[11].Parse();
= array[12].Parse();
尿 = array[13].Parse();
= array[14].Parse();
= array[15].Parse();
= array[16].Parse();
= array[17].Parse();
= array[18].Parse();
= array[19].Parse();
= array[20].Parse();
= array[21].Parse();
= array[22].Parse();
= (from f in array[23].Split("\r\n")
where !string.IsNullOrWhiteSpace(f) && !f.StartsWith("//")
select f).First();
@@ -1088,9 +1088,9 @@ namespace SlaveMatrix
obj.MigrateKeys();
= obj;
//胴体.SaveExMod("C:\\Users\\dave\\Documents\\胴体");
//Ser.ToJson(胴体, "C:\\Users\\dave\\Documents\\胴体.json");
//胴体 = Ser.UnJson<Obj>("C:\\Users\\dave\\Documents\\胴体.json");
//胴体.SaveExMod("C:\\Users\\adel4\\Documents\\胴体");
//Ser.ToJson(胴体, "C:\\Users\\adel4\\Documents\\胴体.json");
//胴体 = Ser.UnJson<Obj>("C:\\Users\\adel4\\Documents\\胴体.json");
obj = Resources..ObjLoad();
obj.MigrateKeys();