在3D空间中使用GLSL绘制2D网格 [英] Draw 2D Mesh Grid using GLSL in 3D space

查看:78
本文介绍了在3D空间中使用GLSL绘制2D网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用OpengGL 4.0在X轴上的有限空间内绘制2D网格.

I'm hoping to draw a 2D Grid within a finite space on the X axis using OpengGL 4.0.

我希望通过vert/frag着色器等使用GLSL来投射光线(使它们出现).

I wish to use GLSL using vert/frag shaders etc for rending the light (making them appear).

可以使用较旧的OpenGL 2.0方法以最简单的代码完成操作,但是当然它不使用照明/着色器为它们着色:

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:

    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();
        }
    }

但是我找不到除之外的任何教程对于将图从图转换为3D空间中的简单2D网格了解得不够充分.

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.

推荐答案

是的,您可以仅使用着色器来生成几何图形...

Yes, you can use just shaders to generate your geometry...

  1. 不要绑定任何VBO.
  2. 致电glDrawArrays()
  3. 在顶点着色器中使用gl_VertexID或在几何着色器中使用gl_PrimitiveID来按程序生成内容.
  1. Don't bind any VBOs.
  2. Call glDrawArrays()
  3. Use gl_VertexID in the vertex shader or gl_PrimitiveID in the geometry shader to procedurally generate your stuff.

由于没有顶点属性或输入数据,因此它可能会更快.更不用说节省了空间,而且初始化时间几乎没有.

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.

这是一个顶点着色器的示例,该着色器使用GL_TRIANGLES绘制网格:

Here's an example of a vertex shader that draws a grid with 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);
}

在古老的ATI卡上没有绑定VBO的情况下,我确实遇到了一些麻烦.这种方法在带有新驱动程序的Nvidia卡上正常工作.此处进一步讨论: Opengl,不绑定VBO的DrawArrays ,其中glDrawArraysInstanced/.

I did have some trouble drawing without VBOs bound on my ancient ATI card. This approach works fine on my Nvidia cards with new drivers. Discussed further here: Opengl, DrawArrays without binding VBO, where glDrawArraysInstanced/gl_InstanceID is suggested as an alternative.

进一步说明.我注意到模%算术在某些情况下可能会有点慢.使用更简单的按位运算或其他技巧 可以加快速度.

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.

这篇关于在3D空间中使用GLSL绘制2D网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆