LWJGL: glTexImage2D 实现
LWJGL: glTexImage2D implementation
我正在尝试使用这两个 类 中的代码从整数数组制作纹理。当我绑定纹理时,我只是变黑了。
public class PTexture {
private int id;
private int width;
private int height;
public PTexture(int id, int width, int height)
{
this.id = id;
this.width = width;
this.height = height;
}
public Vector2f[] getRectPortionCoords(int x, int y, int w, int h)
{
Vector2f[] res = new Vector2f[4];
res[0] = new Vector2f((float)x / w, (float)y / height);
res[1] = new Vector2f((float)(x + w) / width,(float)y / height);
res[2] = new Vector2f((float)(x + w) / width, (float)(y + h) / height);
res[3] = new Vector2f((float)x / w, (float)(y + h) / height);
return res;
}
public void bind()
{
glBindTexture(GL_TEXTURE_2D, id);
}
public int getId() {
return id;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setId(int id) {
this.id = id;
}
}
public class TerrainBlend extends PTexture {
private int[] pixels;
public TerrainBlend(int id, int width, int height) {
super(id, width, height);
pixels = new int[width * height];
}
public void genPixelsFromHeight(float[][] hMap, Vector3f colorHeights)
{
for (int y = 0; y < getHeight(); y++)
{
for (int x = 0; x < getWidth(); x++)
{
if (hMap[x][y] >= colorHeights.getX())
{
genPixel(x, y, 0xFF0000FF);
if (hMap[x][y] >= colorHeights.getY())
{
genPixel(x, y, 0x00FF00FF);
if (hMap[x][y] >= colorHeights.getZ())
{
genPixel(x, y, 0x0000FFFF);
}
}
}
}
}
genTexture();
}
private void genPixel(int x, int y, int color)
{
pixels[x + y * getWidth()] = color;
}
public void genTexture()
{
IntBuffer iBuffer = BufferUtils.createIntBuffer(getWidth() * getHeight() * 4);
for (int i = 0; i < pixels.length; i++)
{
iBuffer.put(pixels[i]);
}
iBuffer.position(0);
setId(glGenTextures());
glBindTexture(GL_TEXTURE_2D, getId());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0, GL_RGBA, GL_UNSIGNED_INT, iBuffer);
}
}
我已经确定数组中的值是正确的,所以我假设 OpenGL 代码在某些方面是错误的。
你有几个问题:
您传递给 glTexImage2D()
的类型将无法按您预期的方式工作:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0,
GL_RGBA, GL_UNSIGNED_INT, iBuffer);
对内部格式为GL_RGBA
的纹理使用GL_UNSIGNED_INT
意味着缓冲区中的GLuint
值将用于每个组件 的纹理。 IE。每个纹素将使用四个值,R、G、B 和 A 各一个。
根据您构建值的方式,您似乎将每个纹素的 RGBA 组件打包在一个值中。要以这种方式解释数据,您需要指定一种格式来指定压缩值:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0,
GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, iBuffer);
您需要指定纹理参数。特别是,默认采样属性假定纹理具有 mipmap,而您的纹理没有。至少,您需要调用将缩小掩码设置为不使用 mipmaps:
glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
我正在尝试使用这两个 类 中的代码从整数数组制作纹理。当我绑定纹理时,我只是变黑了。
public class PTexture {
private int id;
private int width;
private int height;
public PTexture(int id, int width, int height)
{
this.id = id;
this.width = width;
this.height = height;
}
public Vector2f[] getRectPortionCoords(int x, int y, int w, int h)
{
Vector2f[] res = new Vector2f[4];
res[0] = new Vector2f((float)x / w, (float)y / height);
res[1] = new Vector2f((float)(x + w) / width,(float)y / height);
res[2] = new Vector2f((float)(x + w) / width, (float)(y + h) / height);
res[3] = new Vector2f((float)x / w, (float)(y + h) / height);
return res;
}
public void bind()
{
glBindTexture(GL_TEXTURE_2D, id);
}
public int getId() {
return id;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setId(int id) {
this.id = id;
}
}
public class TerrainBlend extends PTexture {
private int[] pixels;
public TerrainBlend(int id, int width, int height) {
super(id, width, height);
pixels = new int[width * height];
}
public void genPixelsFromHeight(float[][] hMap, Vector3f colorHeights)
{
for (int y = 0; y < getHeight(); y++)
{
for (int x = 0; x < getWidth(); x++)
{
if (hMap[x][y] >= colorHeights.getX())
{
genPixel(x, y, 0xFF0000FF);
if (hMap[x][y] >= colorHeights.getY())
{
genPixel(x, y, 0x00FF00FF);
if (hMap[x][y] >= colorHeights.getZ())
{
genPixel(x, y, 0x0000FFFF);
}
}
}
}
}
genTexture();
}
private void genPixel(int x, int y, int color)
{
pixels[x + y * getWidth()] = color;
}
public void genTexture()
{
IntBuffer iBuffer = BufferUtils.createIntBuffer(getWidth() * getHeight() * 4);
for (int i = 0; i < pixels.length; i++)
{
iBuffer.put(pixels[i]);
}
iBuffer.position(0);
setId(glGenTextures());
glBindTexture(GL_TEXTURE_2D, getId());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0, GL_RGBA, GL_UNSIGNED_INT, iBuffer);
}
}
我已经确定数组中的值是正确的,所以我假设 OpenGL 代码在某些方面是错误的。
你有几个问题:
您传递给
glTexImage2D()
的类型将无法按您预期的方式工作:glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0, GL_RGBA, GL_UNSIGNED_INT, iBuffer);
对内部格式为
GL_RGBA
的纹理使用GL_UNSIGNED_INT
意味着缓冲区中的GLuint
值将用于每个组件 的纹理。 IE。每个纹素将使用四个值,R、G、B 和 A 各一个。根据您构建值的方式,您似乎将每个纹素的 RGBA 组件打包在一个值中。要以这种方式解释数据,您需要指定一种格式来指定压缩值:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, iBuffer);
您需要指定纹理参数。特别是,默认采样属性假定纹理具有 mipmap,而您的纹理没有。至少,您需要调用将缩小掩码设置为不使用 mipmaps:
glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);