我正在研究等距游戏引擎,并且已经为像素完美点击检测创建了算法.访问该项目并注意到点击检测能够检测到点击了哪个边缘.它还会检查y-index以单击最前面的磁贴.
等距网格由100*65px的平铺图像组成.
TileW=100, TileL=50, tileH=15
地图由三维数组表示map[z][y][x].
平铺中心点(x,y)的计算如下:
//x, y, z are the position of the tile
if(y%2===0) { x-=-0.5; } //To accommodate the offset found in even rows
this.centerX = (x*tileW) + (tileW/2);
this.centerY = (y*tileL) - y*((tileL)/2) + ((tileL)/2) + (tileH/2) - (z*tileH);
Run Code Online (Sandbox Code Playgroud)
用于确定鼠标是否位于磁贴上给定区域内的原型函数:
Tile.prototype.allContainsMouse = function() {
var dx = Math.abs(mouse.mapX-this.centerX),
dy = Math.abs(mouse.mapY-this.centerY);
if(dx>(tileW/2)) {return false;} //Refer to image
return (dx/(tileW*0.5) + (dy/(tileL*0.5)) < (1+tileHLRatio));
}
Run Code Online (Sandbox Code Playgroud)
Tile.prototype.allContainsMouse()如果鼠标在绿色范围内,则返回true.通过检查dx是瓷砖宽度的一半来裁剪红色区域
Tile.prototype.topContainsMouse = …Run Code Online (Sandbox Code Playgroud) 我一直在尝试到处搜索这个问题,但我发现的所有问题都与高级别的问题有关,例如“如何知道正在单击哪个 HTML”。这不是我要找的东西。我正在寻找更底层的细节。
假设有人正在尝试为给定系统编写 GUI 库。另外,假设该程序员已经设法在屏幕上显示图形、文本、按钮等。现在,他们希望实现用户与该界面交互的方法,更具体地说,使用鼠标单击(或者可能是触摸输入)。
GUI 引擎运行的系统能够告诉他们何时发生点击,以及点击发生在屏幕上的哪个 (x, y) 点。要查找屏幕上的哪些元素被单击,一个简单的方法是循环遍历每个元素并测试每个元素。但这是现有 GUI 中通常的做法吗?例如,这是浏览器显示 HTML 页面时所做的事情吗?这就是操作系统对其 GUI 和窗口所做的事情吗?他们是否使用更复杂的数据结构(例如 R 树)?他们如何处理移动元素?
这个问题并不特定于任何软件,我只是想知道在 GUI 开发的背景下通常如何解决这个问题。
我正在使用glReadPixels来获取选择像素的深度值,但是我总是得到1,我该如何解决呢?这是代码:
glEnable(GL_DEPTH_TEST);
..
glReadPixels(x, viewport[3] - y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, z);
Run Code Online (Sandbox Code Playgroud)
我想念什么吗?我的渲染部分如下所示。我使用不同的着色器绘制场景的不同部分,因此如何正确地从缓冲区读取深度值?
void onDisplay(void)
{
// Clear the window and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// calculate the view matrix.
GLFrame eyeFrame;
eyeFrame.MoveUp(gb_eye_height);
eyeFrame.RotateWorld(gb_eye_theta * 3.1415926 / 180.0, 1.0, 0.0, 0.0);
eyeFrame.RotateWorld(gb_eye_phi * 3.1415926 / 180.0, 0.0, 1.0, 0.0);
eyeFrame.MoveForward(-gb_eye_radius);
eyeFrame.GetCameraMatrix(gb_hit_modelview);
gb_modelViewMatrix.PushMatrix(gb_hit_modelview);
// draw coordinate system
if(gb_bCoord)
{
DrawCoordinateAxis();
}
if(gb_bTexture)
{
GLfloat vEyeLight[] = { -100.0f, 100.0f, 150.0f };
GLfloat vAmbientColor[] = { 0.2f, 0.2f, 0.2f, 1.0f …Run Code Online (Sandbox Code Playgroud) 我想知道如何使用顶点法线来实现闪电效果?目前我所拥有的是我可以将顶点和纹理坐标发送到着色器并使用它们但是使用法线,我不知道如何在着色器程序中使用它们.以下是我到目前为止的情况.
// vertex shader
layout(location = 0) in vec4 vert;
layout(location = 1) in vec4 color;
layout(location = 2) in vec2 texcoord;
uniform mat4 m_model;
uniform mat4 m_view;
uniform mat4 m_proj;
void main() {
gl_Position = m_proj * m_view * m_model * vert;
}
// fragment shader
in vec2 fragtexcoord;
out vec4 color;
uniform sampler2D textureunit;
void main(void) {
color = texture(textureunit, fragtexcoord);
}
Run Code Online (Sandbox Code Playgroud)
编辑 以下是我的着色器.
顶点着色器
layout(location = 0) in vec4 vert;
layout(location = 1) in vec4 color;
layout(location …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用OpenGL为iOS应用程序渲染一个阿甘场景。为了使它更好一点,我想在场景中实现深度效果。但是,我需要从OpenGL深度缓冲区获得线性化的深度值。目前,我正在片段着色器(在此处找到)中使用计算。
因此,我的地形片段着色器如下所示:
#version 300 es
precision mediump float;
layout(location = 0) out lowp vec4 out_color;
float linearizeDepth(float depth) {
return 2.0 * nearz / (farz + nearz - depth * (farz - nearz));
}
void main(void) {
float depth = gl_FragCoord.z;
float linearized = (linearizeDepth(depth));
out_color = vec4(linearized, linearized, linearized, 1.0);
}
Run Code Online (Sandbox Code Playgroud)
但是,这将导致以下输出:
如您所见,越远,您得到的深度值越“条纹”(尤其是在船后)。如果地形图块靠近相机,那么输出就可以了。
我什至尝试了另一种计算:
float linearizeDepth(float depth) {
return 2.0 * nearz * farz / (farz + nearz - (2.0 * depth - 1.0) * (farz - …Run Code Online (Sandbox Code Playgroud)