implemented cursor draw in opengl

This commit is contained in:
KojoZero 2026-04-23 20:03:42 -07:00
parent b3ee2d8ac5
commit c22816591d
4 changed files with 61 additions and 12 deletions

View file

@ -11,8 +11,48 @@ layout(binding = 0) uniform sampler2D color_texture;
uniform vec4 i_resolution;
uniform vec4 o_resolution;
uniform vec2 cursor_pos;
uniform bool cursor_enable;
uniform int layer;
in vec2 pixelUnit;
void main() {
color = texture(color_texture, frag_tex_coord);
vec4 pixel = texture(color_texture, frag_tex_coord);
vec2 rfrag_tex_coord = vec2(frag_tex_coord.yx);
//Cursor
if (cursor_enable){
//Black Outline
if (rfrag_tex_coord.x <= (cursor_pos.x + (2.0*pixelUnit.x)) &&
rfrag_tex_coord.x >= (cursor_pos.x - (1.0*pixelUnit.x))) {
if (rfrag_tex_coord.y <= (cursor_pos.y + (5.0*pixelUnit.y)) &&
rfrag_tex_coord.y >= (cursor_pos.y - (4.0*pixelUnit.y))) {
pixel = vec4(0.0, 0.0, 0.0, 1.0);
}
}
if (rfrag_tex_coord.y <= (cursor_pos.y + (2.0*pixelUnit.y)) &&
rfrag_tex_coord.y >= (cursor_pos.y - (1.0*pixelUnit.y))) {
if (rfrag_tex_coord.x <= (cursor_pos.x + (5.0*pixelUnit.x)) &&
rfrag_tex_coord.x >= (cursor_pos.x - (4.0*pixelUnit.x))) {
pixel = vec4(0.0, 0.0, 0.0, 1.0);
}
}
//White Cross
if (rfrag_tex_coord.x <= (cursor_pos.x + (1.0*pixelUnit.x)) &&
rfrag_tex_coord.x >= (cursor_pos.x - (0.0*pixelUnit.x))) {
if (rfrag_tex_coord.y <= (cursor_pos.y + (4.0*pixelUnit.y)) &&
rfrag_tex_coord.y >= (cursor_pos.y - (3.0*pixelUnit.y))) {
pixel = vec4(1.0, 1.0, 1.0, 1.0);
}
}
if (rfrag_tex_coord.y <= (cursor_pos.y + (1.0*pixelUnit.y)) &&
rfrag_tex_coord.y >= (cursor_pos.y - (0.0*pixelUnit.y))) {
if (rfrag_tex_coord.x <= (cursor_pos.x + (4.0*pixelUnit.x)) &&
rfrag_tex_coord.x >= (cursor_pos.x - (3.0*pixelUnit.x))) {
pixel = vec4(1.0, 1.0, 1.0, 1.0);
}
}
}
color = vec4(pixel.rgb, 1.0);
}

View file

@ -13,11 +13,13 @@ layout(location = 0) out vec2 frag_tex_coord;
// The third row could be used for projection, which we don't need in 2D. It hence is assumed to
// implicitly be [0, 0, 1]
uniform mat3x2 modelview_matrix;
out vec2 pixelUnit;
void main() {
// Multiply input position by the rotscale part of the matrix and then manually translate by
// the last column. This is equivalent to using a full 3x3 matrix and expanding the vector
// to `vec3(vert_position.xy, 1.0)`
gl_Position = vec4(mat2(modelview_matrix) * vert_position + modelview_matrix[2], 0.0, 1.0);
frag_tex_coord = vert_tex_coord;
pixelUnit.x = 1/320.0;
pixelUnit.y = 1/240.0;
}

View file

@ -433,6 +433,8 @@ void RendererOpenGL::ReloadShader(Settings::StereoRenderOption render_3d) {
uniform_i_resolution = glGetUniformLocation(shader.handle, "i_resolution");
uniform_o_resolution = glGetUniformLocation(shader.handle, "o_resolution");
uniform_layer = glGetUniformLocation(shader.handle, "layer");
uniform_cursor_pos = glGetUniformLocation(shader.handle, "cursor_pos");
uniform_cursor_enable = glGetUniformLocation(shader.handle, "cursor_enable");
attrib_position = glGetAttribLocation(shader.handle, "vert_position");
attrib_tex_coord = glGetAttribLocation(shader.handle, "vert_tex_coord");
}
@ -556,11 +558,13 @@ void RendererOpenGL::DrawSingleScreen(const ScreenInfo& screen_info, float x, fl
const u32 scale_factor = GetResolutionScaleFactor();
const GLuint sampler = samplers[Settings::values.filter_mode.GetValue()].handle;
glUniform4f(uniform_i_resolution, static_cast<float>(screen_info.texture.width * scale_factor),
static_cast<float>(screen_info.texture.height * scale_factor),
1.0f / static_cast<float>(screen_info.texture.width * scale_factor),
1.0f / static_cast<float>(screen_info.texture.height * scale_factor));
glUniform4f(uniform_o_resolution, h, w, 1.0f / h, 1.0f / w);
glUniform4f(uniform_i_resolution, static_cast<float>(screen_info.texture.height * scale_factor),
static_cast<float>(screen_info.texture.width * scale_factor),
1.0f / static_cast<float>(screen_info.texture.height * scale_factor),
1.0f / static_cast<float>(screen_info.texture.width * scale_factor));
glUniform4f(uniform_o_resolution, w, h, 1.0f / w, 1.0f / h);
glUniform1i(uniform_cursor_enable, 1);
glUniform2f(uniform_cursor_pos, 159.0f/320.0f, 119.0f/240.0f);
state.texture_units[0].texture_2d = screen_info.display_texture;
state.texture_units[0].sampler = sampler;
state.Apply();
@ -627,11 +631,11 @@ void RendererOpenGL::DrawSingleScreenStereo(const ScreenInfo& screen_info_l,
const u32 scale_factor = GetResolutionScaleFactor();
const GLuint sampler = samplers[Settings::values.filter_mode.GetValue()].handle;
glUniform4f(uniform_i_resolution,
static_cast<float>(screen_info_l.texture.width * scale_factor),
static_cast<float>(screen_info_l.texture.height * scale_factor),
1.0f / static_cast<float>(screen_info_l.texture.width * scale_factor),
1.0f / static_cast<float>(screen_info_l.texture.height * scale_factor));
glUniform4f(uniform_o_resolution, h, w, 1.0f / h, 1.0f / w);
static_cast<float>(screen_info_l.texture.width * scale_factor),
1.0f / static_cast<float>(screen_info_l.texture.height * scale_factor),
1.0f / static_cast<float>(screen_info_l.texture.width * scale_factor));
glUniform4f(uniform_o_resolution, w, h, 1.0f / w, 1.0f / h);
state.texture_units[0].texture_2d = screen_info_l.display_texture;
state.texture_units[1].texture_2d = screen_info_r.display_texture;
state.texture_units[0].sampler = sampler;

View file

@ -107,6 +107,9 @@ private:
GLuint uniform_o_resolution;
GLuint uniform_layer;
// Shader uniform for onscreen cursor
GLuint uniform_cursor_pos;
GLuint uniform_cursor_enable;
// Shader attribute input indices
GLuint attrib_position;
GLuint attrib_tex_coord;