是否可以使用OpenGL点精灵来模拟广告牌精灵?

时间:2009-05-25 19:10:12

标签: opengl point-sprites

我试图在OpenGL中设置点精灵来改变距离,就像一个广告牌精灵那样,但是我无法在GL_POINT_DISTANCE_ATTENUATION_ARB中获取值来做任何有用的事情。是否存在与给定投影匹配的值的相关性?我正在努力做甚么可能吗?

渲染正在使用的代码:

glPointParameterfARB  = (PFNGLPOINTPARAMETERFARBPROC)wglGetProcAddress("glPointParameterfARB");
glPointParameterfvARB = (PFNGLPOINTPARAMETERFVARBPROC)wglGetProcAddress("glPointParameterfvARB");

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluPerspective(100.0, 800.0/600.0, 0.1, 10.0);

float quadratic[] =  { 5.0f, 0.1f, 10.0f };
glPointParameterfvARB( GL_POINT_DISTANCE_ATTENUATION_ARB, quadratic );

float maxSize = 0.0f;
glGetFloatv( GL_POINT_SIZE_MAX_ARB, &maxSize );
if( maxSize > 100.0f )  maxSize = 100.0f;
glPointSize( maxSize );

glPointParameterfARB( GL_POINT_FADE_THRESHOLD_SIZE_ARB, 0.1f );
glPointParameterfARB( GL_POINT_SIZE_MIN_ARB, 0.1f );
glPointParameterfARB( GL_POINT_SIZE_MAX_ARB, maxSize );

glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );

glEnable( GL_POINT_SPRITE_ARB );

glScalef(0.75,1,1);
glTranslatef(0.00,0.0,-1.0);
glScalef(0.5,0.5,0.5);
glRotatef(counter*0.1+0.5,1.0,1.0,0.0);

glBegin( GL_POINTS );

for( int i = 0; i < 100; ++i )
{
    glColor4f( i%10*0.1, i/10*0.1, 0.5, 1.0f );

    glVertex3f( i%10*0.2-1.0,i/10*0.2-1.0,
    ((i%10-5)*(i%10-5)+(i/10-5)*(i/10-5))*0.01 );
}

glEnd();

glDisable( GL_POINT_SPRITE_ARB );

4 个答案:

答案 0 :(得分:7)

这就是我如何让我的穷人缩小磅值的方法:

void render() {
    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
    glEnable(GL_POINT_SPRITE);
    glActiveTexture(GL_TEXTURE0);
    glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);

    /* Activate shader program here */
    /* Send pointSize to shader program */

    glBegin(GL_POINTS);
        /* Render points here */
        glVertex3f(...);
    glEnd(GL_POINTS);
}

顶点着色器:

uniform float pointSize;
void main() {
    gl_Position = ftransform();
    gl_PointSize = pointSize / gl_Position.w;
}

您可以在片段着色器中执行任何操作,但您必须自己计算颜色,光照和纹理。

答案 1 :(得分:2)

除了GLSL之外,随着距离衰减做你想做的事情非常简单。看看事物的投射大小如何随透视投影中的距离呈二次方式降低,您只需要使用二次因子。

如果你想使用你手动设置距离眼睛150个单位的点大小,只需使用1 /(150 ^ 2)作为二次因子(对于常数和线性因子,则为零 - - 如果有的话,你可能想要使用一些像0.01这样的小数字作为常数因子,以避免潜在的除数为零。)

答案 2 :(得分:1)

根据我的经验,点大小衰减不值得麻烦。你最好编写一个非常简单的GLSL顶点着色器,它根据你自己执行的一些计算手动设置点大小。我花了大约半天时间从头开始学习实现这一目标所需的所有GLSL。

GLSL代码可能就像这几行一样简单:

attribute float psize;

void main()
{   
    gl_FrontColor = gl_Color;
    gl_PointSize = psize;
    gl_Position = ftransform();
}

其中psize是用户选择的磅值参数。

答案 3 :(得分:0)

只需查看pmviewer.sourceforge.net,代码就是使用点精灵,每个点都有自己的颜色和大小来模拟体积渲染:  顶点着色器是:

<强> vertexShader

  // with ATI hardware, uniform variable MUST be used by output
    // variables. That's why win_height is used by gl_FrontColor
    attribute float a_hsml1;
    uniform float win_height;
    uniform vec4 cameralocin;
    void main()
    {
    vec4 position=gl_ModelViewMatrix*gl_Vertex;
    vec4 cameraloc=gl_ModelViewMatrix*cameralocin;
    float d=distance(vec3(cameraloc),vec3(position));
    float a_hsml=gl_Normal.x;
    float pointSize=win_height*a_hsml/d; // <- point diameter in
                                        //pixels (drops like sqrt(1/r^2))
    gl_PointSize=pointSize;
    gl_TexCoord[0]=gl_MultiTexCoord0;
    gl_Position=ftransform();
    gl_FrontColor=vec4(gl_Color.r,gl_Color.g,gl_Color.b,gl_Color.a);
    }

<强> pixelShader

uniform sampler2D splatTexture;
void main()
{
vec4 color = gl_Color * texture2D(splatTexture, gl_TexCoord[0].st);
gl_FragColor = color;\n"
}

只是将粒子发送到gpu:

   void PutOneArrayToGPU(unsigned int m_vbo, float *hArray, unsigned int num)
{
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER,  sizeof(float) * num, hArray, GL_STATIC_DRAW);
int size = 0;
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
if ((unsigned)size != (sizeof(float) *num))
    {
    fprintf(stderr, "WARNING: Pixel Buffer Object allocation failed!\n");
    fprintf(stderr, "TurningOff the GPU accelerated rendering\n");
    flag_GpuRender=false;
    }
return flag_GpuRender;
}

然后呈现它们:

  void DrawPointsByGPU()
    {
    glEnableClientState(GL_VERTEX_ARRAY);
    glBindBuffer(GL_ARRAY_BUFFER, m_vboPos);
    glVertexPointer(3, GL_FLOAT, 0, 0);

    glEnableClientState(GL_COLOR_ARRAY);
    glBindBuffer(GL_ARRAY_BUFFER, m_vboColor);
    glColorPointer(4, GL_FLOAT, 0, 0);

    glEnableClientState(GL_NORMAL_ARRAY);
    glBindBuffer(GL_ARRAY_BUFFER, m_vboHSML);
    glNormalPointer( GL_FLOAT, 3*sizeof(float), 0);

    glDrawArrays(GL_POINTS, 0, m_numParticles);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    };