简体   繁体   English

从多边形(openGL)生成纹理

[英]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 我的目标是使5个顶点多边形具有四边形的渐变(也许纹理不是最好的选择)谢谢

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). 图形硬件和/或GL将为您创建从一种颜色到两种颜色的漂亮渐变(请记住:这是使用硬件加速多边形绘制的基本优点之一,您不必在软件中进行插值工作) 。

Your real problem is: which texture coordinates do you use on the 5 vertex polygon. 您的真正问题是:在5个顶点多边形上使用哪个纹理坐标。 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. 如果真的只是一个渐变,我会尝试在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. 这使您可以将场景渲染到临时缓冲区,然后非常轻松地将该缓冲区作为纹理应用,以及更多功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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