- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚完成了我的 iOS 应用程序的第一个版本,Corebox ,现在正在开发一些新功能。
其中一项新功能是对 OpenGL 渲染进行“小”调整,以强制某些对象永远不会被绘制为小于最小尺寸。所有需要这种处理的对象都是使用 GL_LINES 绘制的简单的 2 点线。
这个带注释的屏幕截图解释了我所追求的。忽略灰色线条,我唯一想改变的对象是黄色较宽的线条。
我用谷歌搜索了很多,看来我需要做的是使用顶点着色器改变线条的几何形状。我对 GLSL 很陌生,我能找到的大多数着色器示例都涉及应用照明和其他效果,例如:GLSL Heroku Editor和 KicksJS shader editor .
我当前的顶点着色器非常基础:
// GL_LINES vertex shader
uniform mat4 Projection;
uniform mat4 Modelview;
attribute vec4 Position;
attribute vec4 SourceColor;
varying vec4 DestinationColor;
void main(void) {
DestinationColor = SourceColor;
gl_Position = Projection * Modelview * Position;
}
就像我的片段着色器一样:
// GL_LINES fragment shader
varying lowp vec4 DestinationColor;
void main(void) {
gl_FragColor = DestinationColor;
}
我对需要什么的猜测:
注意事项和其他注意事项:
编辑:这是实现 mifortin 建议的最终工作版本
uniform mat4 Projection;
uniform mat4 Modelview;
uniform float MinimumHeight;
attribute vec4 Position;
attribute vec4 ObjectCenter;
attribute vec4 SourceColor;
varying vec4 DestinationColor;
void main(void) {
// screen-space position of this vertex
vec4 screenPosition = Projection * Modelview * Position;
// screen-space mid-point of the object this vertex belongs to
vec4 screenObjectCenter = Projection * Modelview * ObjectCenter;
// Z should be 0 by this time and the projective transform in w.
// scale so w = 1 (these two should be in screen-space)
vec2 newScreenPosition = screenPosition.xy / screenPosition.w;
vec2 newObjectCenter = screenObjectCenter.xy / screenObjectCenter.w;
float d = distance(newScreenPosition, newObjectCenter);
if (d < MinimumHeight && d > 0.0) {
// Direction of this object, this really only makes sense in the context
// of a line (eg: GL_LINES)
vec2 towards = normalize(newScreenPosition - newObjectCenter);
// Shift the center point then adjust the vertex position accordingly
// Basically this converts: *--x--* into *--------x--------*
newObjectCenter = newObjectCenter + towards * MinimumHeight;
screenPosition.xy = newObjectCenter.xy * screenPosition.w;
}
gl_Position = screenPosition;
DestinationColor = SourceColor;
}
最佳答案
请注意,我没有测试代码,但它应该说明了解决方案。
如果你想使用着色器,在你的线的中心位置添加另一个统一的 vec4。然后你可以做类似的事情(注意中心可以在 CPU 上预先计算一次):
uniform float MIN; //Minimum size of blob on-screen
uniform vec4 center; //Center of the line / blob
...
vec4 screenPos = Projection * Modelview * Position;
vec4 center = Projection * Modelview * Position;
//Z should be 0 by this time and the projective transform in w.
//scale so w = 1 (these two should be in screen-space)
vec2 nScreenPos = screenPos.xy / screenPos.w;
vec2 nCenter = center.xy / center.w;
float d = distance(nScreenPos, nCenter);
if (d < MIN && d > 0)
{
vec2 towards = normalize(nScreenPos - nCenter);
nCenter = nCenter + towards * MIN;
screenPos.xy = nCenter.xy * screenPos.w;
}
gl_Position = screenPos;
找到屏幕上要绘制顶点的位置,然后根据需要从 Blob 的中心拉伸(stretch)它以确保最小尺寸。
此示例适用于圆形对象。对于拐角,您可以将 MIN
设置为一个属性,这样距中心的距离会因顶点而异。
如果您只是想要更像盒子的东西,请分别检查 x 和 y 坐标的最小距离。
在 CPU 上,您可以计算屏幕空间中的坐标并相应地缩放,然后再提交给 GPU。
关于ios - 在 iOS Open GL ES 中使用 GL_LINES 渲染对象的最小尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9471197/
我在使用 GL_LINES block 时遇到问题……下面示例中的线条没有连接到末端(尽管有时它会随机决定连接一个或两个角)。相反,端点彼此相距 1 像素(留下一个未完全平方的角;如果这有意义的话)。
一段时间以来,我一直对 GL_LINES(以及 GL_LINE_LOOP 和 STRIP)感到好奇,我想知道 GL 实现通常如何处理它们。他们是否从线的端点和宽度推断出四边形?它会做一些更原始和更专业
我正在尝试使用 OpenGL 和 Pygame 创建一个 3d Rubiks Cube。但是,当我尝试在我创建的 GL_QUADS 表面上绘制线条时,它们没有显示出来。 我已经尝试将 GL_LINES
我想绘制一个删除隐藏边缘的对象的边缘。我想应用的想法是首先将对象的面渲染到深度缓冲区,然后在第二遍中绘制边缘并启用深度测试。 由于并非所有三角形边都应该可见,因此这些边是单独存储的(简单示例:在立方体
考虑以下代码: glColor4ub(255, 255, 255,255); glBegin(GL_QUADS); glVertex2i(100, 100); glVertex2i(300, 100)
我试图通过添加混合渲染到 FBO 来计算每个像素被写入的总次数来近似“轨迹密度”。 问题是 (1) 位移大的元素会因为线较长而被计数更多次,以及 (2) 由于用于绘制线的像素数也取决于线的角度。 我想
我试图用下面的代码画一条线,它的工作原理: // Include standard headers #include #include // Include GLEW #include // I
我所说的“类似钢笔”的笔画是这样的:当你用圆珠笔在纸上画画时。您将获得一 strip 有圆形开头和结尾的线条。如果我的解释不够好,请参阅 http://java.sun.com/docs/books/
glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glPolygonMode(GL_BACK,GL_LINE); 在上面的代码示例中,glPolygonMod
一段时间以来,我一直在使用 OpenGL 来制作 3D 应用程序,但我从来没有真正理解在生产阶段使用 GL_POINT 和 GL_LINES 原始绘图类型用于 3D 游戏。 (哪里)OpenGL 中的
我正在尝试以正常填充模式渲染图元,然后将其渲染为线框。 渲染代码: glClear(GL_COLOR_BUFFER_BIT); glClearColor(0.9f, 0.9f, 0.9f, 1); /
我被要求制作一个简单的旋转木马,通过鼠标左右点击来旋转。对于我使用的棍子 GL_LINES我遇到的问题是无法定义法线 vector 或类似我在 GL_POLYGON 中所做的事情使照明正确。我在网上搜
我无法让我的着色器在纹理的周边运行。我正在关注 https://learnopengl.com/作为指南并使用索引顶点绘制线基元。问题是只绘制了右侧和底部的线,缺少顶部和左侧。 我编写了一个小程序来尝
我正在努力开发一款仅使用程序生成的线条绘制的新 iOS 游戏。除了绘制一些图元时出现一些奇怪的问题外,一切都运行良好。 我正处于需要实现文本的位置,字符被设置为数组中的一系列点。当我去绘制点(CGPo
我正在尝试使用着色器在 OpenGL 中绘制 3D 线条...到目前为止还没有办法做到这一点。 我的工作正常: void draw_bonds (int nbonds) { glDisable (
我正在编写一个 OpenGL 库,它可以使用屏幕坐标绘制点、线和矩形。但是,我不知道如何将屏幕坐标转换为剪辑或相机坐标。我正在使用现代 OpenGL(顶点数组和顶点缓冲区,以及着色器)。 这基本上就是
如果我使用 GL_LINES 或 GL_LINE_STRIP 渲染多段线,我是否可以假设具有低顶点索引的线段首先绘制到帧缓冲区?比如,当我使用顶点 v0、v1、v2 指定线带时,我是否可以假定先渲染线
首先,这段代码的目的是什么? glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); 我可以放在那里 GL_DONT_CARE 但它不会让我画线,除非我使用 glDisable
我有以下程序,但我无法弄清楚为什么 glColor4f() 调用不会导致 glDrawArrays() 的输出出现红线,任何人都可以吗告诉我我做错了什么?由于某种原因正在输出一条白线。现代版本的 GL
我刚刚完成了我的 iOS 应用程序的第一个版本,Corebox ,现在正在开发一些新功能。 其中一项新功能是对 OpenGL 渲染进行“小”调整,以强制某些对象永远不会被绘制为小于最小尺寸。所有需要这
我是一名优秀的程序员,十分优秀!