[英]OpenGL Render to texture Coordinates
我希望能夠渲染到紋理(對於着色器,字體),但是我有一個問題是正確定位四邊形(幀緩沖本身做它應該做的事情)。 生成的(復制的)紋理在大多數情況下顯示左上角,其余的被裁剪。 這對於矩形紋理來說更糟糕,對方形紋理幾乎沒有影響(所以我好幾天都沒有認識到這種行為)
示例代碼:
public Texture copy(final Texture tex) {
final int tempTex = glGenTextures();
Draw.bindTexture(tempTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.getImageWidth(), tex.getImageHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, (ByteBuffer)null);
// Adjust projection matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glViewport(0, 0, tex.getImageWidth(), tex.getImageHeight());
// Change blendmode to ignore the transparent background
Draw.blendingMode(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// Prepare fbo, add the texture as backend and clear it
final int fbo = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tempTex, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Finally draw
Draw.bindTexture(tex.getId());
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(1, 0);
glVertex2f(1, -1);
glTexCoord2f(1, 1);
glVertex2f(1, 1);
glTexCoord2f(0, 1);
glVertex2f(-1, 1);
}
glEnd();
// Unbind framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Reset projection matrix
glViewport(0, 0, 1920, 1080); // TODO Don't hardcode
glPopMatrix();
return new Texture(tempTex, tex.getImageWidth(), tex.getImageHeight());
}
示例圖片:
鞋面是復制版本下方渲染的原始圖像
我究竟做錯了什么?
更新:現在看起來紋理得到了下采樣
您不應該將實際紋理寬度和高度傳遞到texcoords。 紋理坐標范圍從0(底部和左側)到1(頂部和右側)。 您不會將實際紋理尺寸硬編碼到紋理坐標中。
嘗試用“1”替換texcoord函數中的getWidth / getHeight
最好的方法是使用尺寸與顯示器相同的正投影矩陣(1920x1080)。 你的紋理必須是2 pow大小的紋理,所以你必須使它2048x2048。 如果現在將結果渲染到紋理的矩形(不適合整個紋理),則可以在屏幕上使用相同的矩陣渲染它。 (像素正確)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.