Drawing a 2D mesh grid using GLSL in 3D space

I hope to draw a 2D mesh in the final space on the X axis using OpengGL 4.0.

I want to use GLSL using vertex / snippet shaders, etc., to smooth out the light (by creating them).

This can be done using the simplest code using older OpenGL 2.0 methods, but, of course, it does not use highlighting / shaders to color 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(); } } 

But I could not find any textbooks other than this , which I do not understand enough to convert from a graph to a simple 2D grid in three-dimensional space.

+2
source share
2 answers

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

  • Do not bind VBOs.
  • Call glDrawArrays()
  • Use gl_VertexID in the vertex shader or gl_PrimitiveID in the geometric shader to procedurally generate your materials.

This can be faster because there are no vertex attributes or input. Not to mention that the time of saving and initialization is practically nothing left.

Here is an example of a vertex shader that draws a grid using 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'm having trouble drawing without VBOs tied to my ancient ATI card. This approach works fine on my Nvidia cards with new drivers. Discussed further: Opengl, DrawArrays without binding to VBO , where glDrawArraysInstanced / gl_InstanceID offered as an alternative.

One more note. I noticed that modulo % arithmetic can be a little slow in some cases. Using simpler bitwise operations or other tricks can speed up the process.

+4
source

GLSL is not intended for such actions. GLSL is designed to write shaders that:

  • tesselate simple geometries to somewhat more complex ones
  • converts simple primitives into abstract space into clip space.
  • control fragment rasterization

GLSL is not intended to actually create callbacks. Yes, you can use shaders to generate procedural geometry, but you always start with geometry and draw calls made on the client side.

-1
source

All Articles