简体   繁体   中英

Generate texture from polygon (openGL)

I have a quad and I would like to use the gradient it produces as a texture for another polygon.

glPushMatrix();
glTranslatef(250,250,0);
    glBegin(GL_POLYGON);


    glColor3f(255,0,0);
glVertex2f(10,0);
glVertex2f(100,0);
glVertex2f(100,100);
glVertex2f(50,50);
glVertex2f(0,100);

    glEnd(); //End quadrilateral coordinates

    glPopMatrix();
    glBegin(GL_QUADS); //Begin quadrilateral coordinates

    glVertex2f(0,0);
glColor3f(0,255,0);
    glVertex2f(150,0);
    glVertex2f(150,150);
    glColor3f(255,0,0);
    glVertex2f(0,150);


    glEnd(); //End quadrilateral coordinates

My goal is to make the 5 vertex polygon have the gradient of the quad (maybe a texture is not the best bet) Thanks

Keep it simple!

It is very simple to create a gradient texture in code, eg:

// gradient white -> black
GLubyte gradient[2*3] = { 255,255,255, 0,0,0 };

// WARNING: check documentation, I am not quite sure about syntax and order:
glTexture1D( GL_TEXTURE_1D, 0,3, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, gradient );

// setup texture parameters, draw your polygon etc.

The graphics hardware and/or the GL will create a sweet looking gradient from color one to color two for you (remember: that's one of the basic advantages of having hardware accelerated polygon drawing, you don't have to do interpolation work in software).

Your real problem is: which texture coordinates do you use on the 5 vertex polygon. But that was not your question... ;-)

To do that, you'd have to do a render-to-texture. While this is commonplace and supported by practically every board, it's typically used for quite elaborate effects (eg mirrors).

If it's really just a gradient, I'd try to create the gradient in am app like Paint.Net. If you really need to create them at run-time, use a pixel shader to implement render-to-texture. However, I'm afraid explaining pixel shaders in a few words is a bit tough - there are lots of tutorials on this on the net, however.

With the pixel shader, you gain a lot of control over the graphic card. This allows you to render your scene to a temporary buffer and then apply that buffer as a texture quite easily, plus a lot more functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM