views:

63

answers:

2

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

+1  A: 

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 (e.g. 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.

mnemosyn
Other than that theres no other way to get a linear gradient on a polygon?
Milo
"linear interpolation" (your gradient) is one of the most basic basics of hardware accelerated rendering, but check my answer.. ;)
frunsi
another hint: any render-to-texture method would be completely overkill for your application. its a bit sad, that practical render-to-texture methods were available so late. if you want to support many (consumer and professional) gfx cards, you still have to do a lot of low-level work to get it working and get it right. trust us, don't use render-to-texture methods for this task (it can be solved easily without rtt).
frunsi
+2  A: 

Keep it simple!

It is very simple to create a gradient texture in code, e.g.:

// 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... ;-)

frunsi
I agree this is a more simple solution, but technically not what the OP asked for: "...the gradient it [the poloygon] produces". A polygon does, in general, *not* produce a linear gradient, since it will typically vary on both axes. Moreover, that texture-mapping problem does not arise with a two-dimensonal gradient texture. To be more precise: For the simple solution you present, you could go even more simple by providing `glColor3f` to each vertex, which is used to produce the gradient in the first place. But the result of the first render pass will typically be way more complex.
mnemosyn