fullscreen triangle

Started by
1 comment, last by sap 9 years, 8 months ago

Hello,

so using gl_VertexID we can create a fullscreen triangle without using any vertex buffer, index buffer or vertex array.

heres the code for the directx one:


out_texture = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2);
gl_Position = vec4(out_texture * vec2(2.0f, -2.0f) + vec2(-1.0f, 1.0f), 0.0f, 1.0f);

please ignore the fact that i used gl_Position instead of whatever they use in directx.

anyway, we can just draw 3 vertices (glDrawArrays(GL_TRIANGLES, 0, 3)) and the vertex shader for each vertex will create a vertex position and UV coord.

problem is, that vertex shader creates UV coords with [0,0] at top left instead of bottom left like opengl requires. so i think a change is needed when setting the gl_Position.

if i understand correctly this is what happens:


gl_VertexID = 0 makes out_texture = 0,0 and gl_Position = -1,1 (top left) and i want gl_Position = -1,-1 (bottom left)
gl_VertexID = 1 makes out_texture = 2,0 and gl_Position = 3,1 (top right) and i want gl_Position = 3,-1 (bottom right)
gl_VertexID = 2 makes out_texture = 0,2 and gl_Position = -1,-3 (bottom left) and i want gl_Position = -1,3 (top left)

google is full of search results showing the directx vertex shader, but i cant find a single one using opengl..

any help is welcome.

Advertisement

with (p.x,p.y) being the x-y co-ordinates of gl_Position and (t.x,t.y) those of out_texture

from: gl_Position = vec4(out_texture * vec2(2.0f, -2.0f) + vec2(-1.0f, 1.0f), 0.0f, 1.0f)

you have currently: ( p.x , p.y ) = ( t.x , t.y ) * ( +2 , -2 ) + ( -1 , +1 )

but you want y being negated: ( p.x , -p.y ) = ( p.x , p.y ) * ( +1, -1 )

hence: ( ( t.x , t.y ) * ( +2 , -2 ) + ( -1 , +1 ) ) * ( +1, -1 ) = ( t.x , t.y ) * ( +2 , -2 ) * ( +1, -1 ) + ( -1 , +1 ) * ( +1, -1 ) = ( t.x , t.y ) * ( +2 , +2 ) + ( -1 , -1 )

gives you: gl_Position = vec4(out_texture * vec2(2.0f, 2.0f) + vec2(-1.0f, -1.0f), 0.0f, 1.0f)

it works prefect now!

i forgot to write in my post that i did try to solve it to be negated and i did get the -1, -1 sum vector but for some reason i got -2,2 on the multiplication vector..

anyway thanks a lot!

This topic is closed to new replies.

Advertisement