如何限制轴中两个坐标之间的OpenGL图形?(How does one restrict drawing in OpenGL between two coordinates in the axis?)

编程入门 行业动态 更新时间:2024-10-25 22:34:51
如何限制轴中两个坐标之间的OpenGL图形?(How does one restrict drawing in OpenGL between two coordinates in the axis?)

我是一名新的opengl程序员。 我正在绘制一幅由三维图形格式组成的数千个三角形的高度图。 它们被缩放以便它们在三轴中在-1和+1之间绘制。 现在,我只能放大X轴,并能够在X轴上进行平移,并通过应用适当的缩放和平移矩阵。 这实际上使我可以放大数据并按照我选择的方向在x方向上移动它。 问题是,一旦我缩放,x方向上的数据现在延伸到图的边界-1到+ 1区域之外。 我希望这些数据不被显示。 这在现代OpenGL中如何完成? 谢谢编辑:矩阵如下:

plottingProgram["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); plottingProgram["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, new Vector3(0, 1, 0)));

并且顶点着色器是

public static string VertexShader = @" #version 130 in vec3 vertexPosition; out vec2 textureCoordinate; uniform mat4 projection_matrix; uniform mat4 view_matrix; uniform mat4 model_matrix; void main(void) { textureCoordinate = vertexPosition.xy; gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1); } ";

以下是图表的链接: http : //va2fsq.com/wp-content/uploads/graph.jpg

谢谢

I'm a new opengl programmer. I am plotting a height map composed of thousands of triangles in a 3D graph format. They are scaled so that they are plotted between -1 and +1 in the three axis. Now I am able to zoom in the X axis only and am able to translate in the X axis as well by applying the appropriate scale and translation matrices. This effectively allows me to zoom right into the data and move it in the x direction as I choose. The problem is, once I zoom, the data in the x direction now extends outside the -1 to + 1 region which the boundaries of a graph. I want this data to not be shown. How is this done in modern OpenGL? Thank you Edit: The matrices are as follows:

plottingProgram["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); plottingProgram["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, new Vector3(0, 1, 0)));

and the vertex shader is

public static string VertexShader = @" #version 130 in vec3 vertexPosition; out vec2 textureCoordinate; uniform mat4 projection_matrix; uniform mat4 view_matrix; uniform mat4 model_matrix; void main(void) { textureCoordinate = vertexPosition.xy; gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1); } ";

Here is a link to the graph: http://va2fsq.com/wp-content/uploads/graph.jpg

Thanks

最满意答案

我解决了这个问题 在尝试了很多像glScissor之类的东西之后,我发现了glClip_distance。 所以我最初的尝试在着色器中放置了一个Uniform

in vec3 vertexPosition; uniform vec4 plane0 = (-1,0,0,1); uniform vec4 plane1 = (1,0,0,1); void main(void) { gl_ClipDistance[0] = dot(vec4(vertexPosition,1), plane0; gl_ClipDistance[1] = dot(vec4(vertexPosition,1), plane2;

现在问题在于vec4平面可以通过任何模型矩阵缩放或变换进行缩放和平移。 所以这是行不通的。 所以解决方法是将剪辑向量移动到顶点着色器之外,并按如下方式对其应用相反的缩放比例:

Vector4 clip1 = new Vector4(-1, 0, 0, 1.0f / scale); Vector4 clip2 = new Vector4(1, 0, 0, 1.0f / scale); plottingProgram["model_matrix"].SetValue(Matrix4.CreateScaling(new Vector3(scale,1,1)) * Matrix4.CreateTranslation(new Vector3(0,0,0)) *Matrix4.CreateRotationY(yangle) * Matrix4.CreateRotationX(xangle)); plottingProgram["plane0"].SetValue(clip1); plottingProgram["plane1"].SetValue(clip2);

并且完整的顶点着色器由给出

public static string VertexShader = @" #version 130 in vec3 vertexPosition; out vec2 textureCoordinate; uniform mat4 projection_matrix; uniform mat4 view_matrix; uniform mat4 model_matrix; uniform vec4 plane0; uniform vec4 plane1; void main(void) { textureCoordinate = vertexPosition.xy; gl_ClipDistance[0] = dot(vec4(vertexPosition,1), plane0); gl_ClipDistance[1] = dot(vec4(vertexPosition,1), plane1); gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1); } ";

你也可以用相同的方式翻译。

I solved this. After trying many things like glScissor, I happened upon the glClip_distance. So my initial try placed a Uniform in the shader which was set to

in vec3 vertexPosition; uniform vec4 plane0 = (-1,0,0,1); uniform vec4 plane1 = (1,0,0,1); void main(void) { gl_ClipDistance[0] = dot(vec4(vertexPosition,1), plane0; gl_ClipDistance[1] = dot(vec4(vertexPosition,1), plane2;

Now the problem with this is that the vec4 planes are scaled and translated by any model matrix scaling or transformations. So that wouldn't work. So the solution is to move the clip vectors outside of the vertex shader and apply the opposite scaling to them as follows:

Vector4 clip1 = new Vector4(-1, 0, 0, 1.0f / scale); Vector4 clip2 = new Vector4(1, 0, 0, 1.0f / scale); plottingProgram["model_matrix"].SetValue(Matrix4.CreateScaling(new Vector3(scale,1,1)) * Matrix4.CreateTranslation(new Vector3(0,0,0)) *Matrix4.CreateRotationY(yangle) * Matrix4.CreateRotationX(xangle)); plottingProgram["plane0"].SetValue(clip1); plottingProgram["plane1"].SetValue(clip2);

and the complete vertex shader is given by

public static string VertexShader = @" #version 130 in vec3 vertexPosition; out vec2 textureCoordinate; uniform mat4 projection_matrix; uniform mat4 view_matrix; uniform mat4 model_matrix; uniform vec4 plane0; uniform vec4 plane1; void main(void) { textureCoordinate = vertexPosition.xy; gl_ClipDistance[0] = dot(vec4(vertexPosition,1), plane0); gl_ClipDistance[1] = dot(vec4(vertexPosition,1), plane1); gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1); } ";

You can also translate in the same way.

更多推荐

本文发布于:2023-07-24 01:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240160.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:坐标   图形   两个   coordinates   axis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!