opengl:如何避免纹理缩放 [英] opengl: how to avoid texture scaling

查看:637
本文介绍了opengl:如何避免纹理缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何应用始终保持其原始比例(纹理中的1像素=屏幕上的1像素)的重复纹理,而不管所应用的顶点数据如何.

How do I apply a repeating texture that always maintains its original scale (1 pixel in the texture = 1 pixel on screen), regardless of the vertex data it is applied with.

我意识到这不是最常见的任务,但是可以很容易地设置opengl来做到这一点,还是我需要对尊重其原始外观的顶点数据应用某种掩码?

I realize this is not the most usual task, but is it possible to easily set opengl to do this, or do I need to apply some kind of mask to vertex data that respects its original appearance?

edit:在我的特定情况下,我正在尝试绘制具有相同像素图案的不同大小的2D椭圆.椭圆由三角形的扇子制成,我很难在上面画出任何重复的纹理.我希望有一些opengl配置组合可以轻松做到这一点.另外,现在我意识到必须要提到的是,我在iPhone上使用的是opengles,因此GLU不可用.

edit: in my specific case, I'm trying to draw 2D ellipses of different sizes, with the same pixel pattern. The ellipses are made of a triangle fan, and I'm having a hard time to draw a repeating texture of any kind on it. I was hoping there was some opengl configuration combination to do this easily. Also, now I realize it's important to mention that I'm using opengles, for the iphone, so GLU is not available.

推荐答案

创建有问题的3D对象,而不显示它.

您可以使用gluProject来获取对象的边界作为像素位置(以获取代表对象边缘的像素.然后可以使用gluUnProject将介入的像素映射到对象的坐标.

然后,开始绘制,并将自定义(即时)纹理映射到同一对象上并显示它.

不知道为什么要这样做,但这应该是一个很好的起点.



我自定义的意思是,如果对象的边界(在一个维度上)为-3.0至1.0,并且第一像素行从-3.0至-2.0,则纹理贴图将表明您的25%自定义纹理贴图在该点上,然后使用要在此处显示的像素的颜色全部创建.

仔细考虑之后,我意识到您可以在投影的屏幕坐标上方绘制纹理(使用2D绘制工具.)

我认为这可以传达您的想法的要点.如果对象"靠近并移开,并且纹理似乎没有按比例放大和缩小,我认为在交互式3D演示中效果不佳.但是您没有说您实际在做什么.



OpenGL 2D投影:


注意
小心函数名称,例如opengles 1.1具有glOrthox和glOrthof.确保检查gl.h头文件中的可用内容.

Create the 3D object in question, without displaying it.

You can get the bounds of the object as pixel locations by using gluProject (to get the pixels that represent the object's edges. You can then use gluUnProject to map the intervening pixels to the object's coordinates.

Then, you start your draw over, and map a custom (on-the-fly) texture over the same object and display it.

Not sure why you'd want to do this, but that should be a good starting point.



What I mean by custom, is if the bounds of your object (in one dimension,) are -3.0 to 1.0, and the first pixel row is from -3.0 to -2.0, your texture map is going to indicate that 25% of your custom texture maps over that spot, and you create it all with the color of the pixel you want to show there.

After thinking that through, I realized you could just draw a texture over the top of the projected screen coordinates (using the 2D drawing facilities.)

I think that gets the gist of your idea across. I don't think it would work well in an interactive 3D demo, if the 'object' comes closer and moves away, if the texture doesn't seem to scale up and down. But you didn't say what you were actually doing.

Edit 2:

OpenGL 2D Projection:


CAUTION
Careful with the function names, e.g., opengles 1.1 has glOrthox and glOrthof. Make sure you check what is available in your gl.h header file.

const XSize = 640, YSize = 480
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
glOrtho (0, XSize, YSize, 0, 0, 1)
glMatrixMode (GL_MODELVIEW)
glDisable(GL_DEPTH_TEST)
glClear(GL_COLOR_BUFFER_BIT)

// Now draw with 2i or 2f vertices instead of the normal vertex3f functions.
// And for ES, of course set up your data structures and call drawarrays ofr drawelements.

SwapBuffers()

这将使您可以在OpenGL中绘制2D形状(比使用3D投影要简单得多.)要混合两者,例如先绘制3D然后再绘制2D,请点击第二个链接.

这是有关2D绘图的出色教程:
http://basic4gl.wikispaces.com/2D+Drawing+in+OpenGL

以下是将两者混合使用的基础知识:
http://www.gamedev.net/community/forums/topic. asp?topic_id = 96440

我希望这就是你想要的.我从您的帖子中偷偷地怀疑您在跨三角形的点映射纹理以使其显示为笔直"时遇到了麻烦.您可能需要查看NeHe上的基本纹理映射:
http://www.gamedev.net/community/forums/topic. asp?topic_id = 96440
例如,gltexcoord2f根据映射到下一个绘制顶点的纹理的宽度和高度的百分比来指定纹理内的点(0.0-1.0).使用三角形风扇时,您可能会有一些数学上的误解,以找出要用顶点指定的整个对象的宽度和高度的百分比.

例如,通过计算纬度线作为基础三角形扇形顶点值的基础,可以最好地映射具有纹理贴图的球体(地球的墨卡托投影),因为它可以简化纹理坐标的计算.使多边形近似为简单的几何形状,使您可以使用三角函数更轻松地计算纹理坐标.

希望对您有所帮助.

在这里,我不再继续您必须修改的桌面示例.这是一个执行正确3D纹理映射的OpenGLES示例.您可以使用我在上面所说的内容以及本示例来进行2D纹理映射.
http://www.zeuscmd.com/tutorials/opengles/17-TextureMapping. php

This will allow you to draw 2D shapes in OpenGL (much more simply than using 3D projections.) To mix the two, e.g., draw in 3D then in 2D, follow the second link.

Here's an excellent tutorial on 2D drawing:
http://basic4gl.wikispaces.com/2D+Drawing+in+OpenGL

Here's the basics on mixing the two:
http://www.gamedev.net/community/forums/topic.asp?topic_id=96440

I hope that is what you want. I get a sneaking suspicion from your post that you're having trouble mapping your texture across triangle points to make it show up 'straight'. You might want to review basic texture mapping on NeHe:
http://www.gamedev.net/community/forums/topic.asp?topic_id=96440
E.g., gltexcoord2f specifies the point (0.0-1.0) within the texture in terms of the percentage of width and height of the texture that maps to the next drawn vertex. With triangle fans, you can have some mathematical conniptions to figure out what % of width and height of the overall object you are specifying with the vertex.

Take, for example, a sphere with a texture map (a mercator projection of the earth,) is best mapped by calculating the lines of latitude as a basis for your underlying triangle fan vertex values, as it eases calculation of the texture coordinates. Making your polygons approximate simple geometric shapes allows you to use trigonometry to more easily calculate texture coordinates.

I hope this is helpful.

Hehere, I'll quit going on with desktop examples you have to modifiy. Here's an OpenGLES example that does proper 3D texture mapping. You can use what I said above, and this example, to do 2D texture mapping.
http://www.zeuscmd.com/tutorials/opengles/17-TextureMapping.php

这篇关于opengl:如何避免纹理缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆