简体   繁体   English

在3D空间中使用GLSL绘制2D网格

[英]Draw 2D Mesh Grid using GLSL in 3D space

I'm hoping to draw a 2D Grid within a finite space on the X axis using OpengGL 4.0. 我希望使用OpengGL 4.0在X轴的有限空间内绘制2D网格。

I wish to use GLSL using vert/frag shaders etc for rending the light (making them appear). 我希望通过vert / frag着色器等使用GLSL来渲染光线(使它们出现)。

It can be done with the simplest code using older OpenGL 2.0 methods but then of course it doesn't use lighting/shaders to colour them: 可以使用较旧的OpenGL 2.0方法以最简单的代码完成此操作,但是当然它不使用照明/着色器为它们着色:

    void Draw_Grid()
    {
     for(float i = -500; i <= 500; i += 5)
        {
         glBegin(GL_LINES);
            glColor3ub(150, 190, 150);
            glVertex3f(-500, 0, i);
            glVertex3f(500, 0, i);
            glVertex3f(i, 0,-500);
            glVertex3f(i, 0, 500);
         glEnd();
        }
    }

But I can'd find any tutorials other than this one which I don't understand well enough to convert from a graph to a simple 2D Grid in 3D space. 但是,除了教程之外,我找不到其他教程,因为我对教程的理解不深,无法从图形转换为3D空间中的简单2D网格。

Yes, you can use just shaders to generate your geometry... 是的,您可以仅使用着色器来生成几何图形...

  1. Don't bind any VBOs. 不要绑定任何VBO。
  2. Call glDrawArrays() 调用glDrawArrays()
  3. Use gl_VertexID in the vertex shader or gl_PrimitiveID in the geometry shader to procedurally generate your stuff. 使用gl_VertexID在顶点着色器或gl_PrimitiveID在几何着色器程序上,以产生你的东西。

It can be faster exactly because there are no vertex attributes or input data. 由于没有顶点属性或输入数据,因此可以精确得多。 Not to mention the space saved and initialization time is next to nothing. 更不用说节省空间了,初始化时间几乎没有。

Here's an example of a vertex shader that draws a grid with GL_TRIANGLES : 这是一个顶点着色器的示例,该着色器使用GL_TRIANGLES绘制网格:

#version 150

uniform mat4 modelviewMat;
uniform mat3 normalMat;
uniform mat4 projectionMat;

uniform ivec2 dim;

out vec3 esNorm;

const vec2 triOffset[] = vec2[](
    vec2(0,0),
    vec2(0,1),
    vec2(1,1),
    vec2(0,0),
    vec2(1,1),
    vec2(1,0));

void main()
{
    int triVert = gl_VertexID % 6;
    int gridIndex = gl_VertexID / 6;
    vec2 coord = vec2(gridIndex / dim.x, gridIndex % dim.x);
    coord = (coord + triOffset[triVert]) / vec2(dim);
    vec4 esVert = modelviewMat * vec4(coord * 2.0 - 1.0, 0.0, 1.0);
    gl_Position = projectionMat * esVert;
    esNorm = normalMat * vec3(0.0, 0.0, 1.0);
}

I did have some trouble drawing without VBOs bound on my ancient ATI card. 没有在老式ATI卡上绑定VBO的情况下,我确实遇到了一些绘制问题。 This approach works fine on my Nvidia cards with new drivers. 这种方法在带有新驱动程序的Nvidia卡上正常工作。 Discussed further here: Opengl, DrawArrays without binding VBO , where glDrawArraysInstanced / gl_InstanceID is suggested as an alternative. 这里进一步讨论: Opengl,不绑定VBO的DrawArrays ,其中建议使用glDrawArraysInstanced / gl_InstanceID作为替代方案。

A further note. 进一步说明。 I've noticed modulo % arithmetic can be a little slow in some cases. 我注意到在某些情况下,模%算术可能会有点慢。 Using simpler bitwise operations or other tricks may speed things up. 使用更简单的按位运算或其他技巧可能会加快速度。

GLSL is not meant for doing things like that. GLSL并非旨在执行此类操作。 GLSL is meant for writing shaders which: GLSL用于编写以下着色器:

  • tesselate simple geometries into slightly more complex ones 将简单的几何体细分为稍微复杂的几何体
  • transform simple primitives in abstract space into clip space 将抽象空间中的简单基元转换为剪贴空间
  • control the fragment rasterization process 控制片段栅格化过程

GLSL isn't intended to be used for actually making draw calls. GLSL并非用于实际进行绘图调用。 Yes, you can use shaders for procedural geometry generation, but you always start off with geometry and draw calls made on the client side. 是的,可以将着色器用于过程几何的生成,但是始终要从几何开始,并绘制在客户端进行的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM