像素空间中的 OpenGL 纹理坐标 [英] OpenGL Texture Coordinates in Pixel Space

查看:29
本文介绍了像素空间中的 OpenGL 纹理坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用 OpenGL ES 2 进行绘图的 iPhone 应用程序.我知道通常纹理坐标定义在 0-1 范围内,但理想情况下,为了可读性,我想将它们映射到 0-1023(我的 TextureAtlas 的大小).我已经看到以这种方式定义坐标的示例代码,但无法确定之前进行的哪些调用允许这样做.glMatrixMode(GL_TEXTURE) 好像可能涉及到它,但我不太确定如何实现它.

I'm working on an iPhone app that uses OpenGL ES 2 for its drawing. I know that typically texture coordinates are defined in the 0-1 range, but ideally I'd like to map them from 0-1023 (the size of my TextureAtlas) for readability's sake. I've seen sample code that defines coordinates in this manner, but haven't been able to suss out what previous calls were made that allowed for this. glMatrixMode(GL_TEXTURE) seems like it might be involved, but I'm not quite sure how to implement it.

我的最终目标是完成这样的事情,我将在图集中使用的纹理位于左上角 48 像素的正方形中:

My end goal would be to accomplish something like this, where the texture I'd be using within the atlas is in the upper left 48px square:

GLshort texcoords[]={
  48,48,
  0,48,
  48,0,
  0,0,
};
glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_SHORT, 0, 0, texcoords);
glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);

推荐答案

原来这在 OpenGL ES 2 中是可能的.我是这样实现的:

Turns out this is possible in OpenGl ES 2. Here's how I accomplished it:

片段着色器:

precision mediump float; 
varying vec2 v_texCoord;
uniform sampler2D textureSample;
uniform float texScale;
void main()
{
    vec2 mult=(2.0*v_texCoord - 1.0)/(2.0*texScale);
    gl_FragColor = texture2D(textureSample,mult);
}

对象-C:

GLshort texCoords[]={
    512,512,
    0,512,
    512,0,
    0,0,
};
GLfloat textureWidth = 512.0; //texture size, assumed square, power of 2
texCoordLoc = glGetAttribLocation ( program, "a_texCoord");
GLuint textureScale = glGetUniformLocation ( program, "texScale");
glUniform1f(textureScale, textureWidth);
glVertexAttribPointer ( texCoordLoc, 2, GL_SHORT, GL_FALSE, 0, texCoords);

如果有人对这在性能方面如何工作有意见,我会很感兴趣.

If anyone has comments on how this might work performance-wise, I would be interested.

这篇关于像素空间中的 OpenGL 纹理坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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