在OpenGL 3.2中绘制全屏四边形的最佳方法是什么? [英] What's the best way to draw a fullscreen quad in OpenGL 3.2?
问题描述
我正在片段着色器中进行射线投射.我可以想到几种方法来为此目的绘制全屏四边形.在投影空间中将投影矩阵设置为单位矩阵的情况下,在剪辑空间中绘制一个四边形,或者使用几何着色器将一个点变成三角形带.前者使用立即模式,OpenGL 3.2中已弃用该模式.出于新颖性考虑,我使用了后者,但仍然使用立即模式来画点.
I'm doing ray casting in the fragment shader. I can think of a couple ways to draw a fullscreen quad for this purpose. Either draw a quad in clip space with the projection matrix set to the identity matrix, or use the geometry shader to turn a point into a triangle strip. The former uses immediate mode, deprecated in OpenGL 3.2. The latter I use out of novelty, but it still uses immediate mode to draw a point.
推荐答案
您可以发送两个三角形来创建一个四边形,其顶点属性分别设置为-1/1.
You can send two triangles creating a quad, with their vertex attributes set to -1/1 respectively.
您不需要将它们与顶点/片段着色器中的任何矩阵相乘.
You do not need to multiply them with any matrix in the vertex/fragment shader.
这里有一些代码示例,很简单:)
Here are some code samples, simple as it is :)
顶点着色器:
const vec2 madd=vec2(0.5,0.5);
attribute vec2 vertexIn;
varying vec2 textureCoord;
void main() {
textureCoord = vertexIn.xy*madd+madd; // scale vertex attribute to [0-1] range
gl_Position = vec4(vertexIn.xy,0.0,1.0);
}
片段着色器:
varying vec2 textureCoord;
void main() {
vec4 color1 = texture2D(t,textureCoord);
gl_FragColor = color1;
}
这篇关于在OpenGL 3.2中绘制全屏四边形的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!