Jump to content

  • Log In with Google      Sign In   
  • Create Account

fullscreen triangle


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 sap   Members   -  Reputation: 111

Like
0Likes
Like

Posted 06 August 2014 - 01:53 PM

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.


Edited by sap, 06 August 2014 - 01:56 PM.


Sponsor:

#2 haegarr   Crossbones+   -  Reputation: 4311

Like
1Likes
Like

Posted 06 August 2014 - 02:22 PM

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)


Edited by haegarr, 06 August 2014 - 02:22 PM.


#3 sap   Members   -  Reputation: 111

Like
0Likes
Like

Posted 06 August 2014 - 02:59 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS