我试图使用这两个类的代码从整数数组中生成纹理。当我把纹理绑起来的时候,我就变黑了。
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代码在某种程度上是错误的。
发布于 2015-04-04 01:36:11
你有几个问题:
glTexImage2D()的类型将不像您所希望的那样工作:
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,getWidth(),getHeight(),0,GL_RGBA,GL_UNSIGNED_INT,iBuffer);
将GL_UNSIGNED_INT用于具有GL_RGBA内部格式的纹理意味着来自缓冲区的GLuint值将用于纹理的每个组件。也就是说,每个纹理将使用四个值,R、G、B和A各使用一个值。
根据构建值的方式,看起来每个texel的RGBA组件都打包在一个值中。要以这种方式解释数据,需要指定指定打包值的格式:
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,getWidth(),getHeight(),0,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8_REV,iBuffer);https://stackoverflow.com/questions/29437363
复制相似问题