Hello everyone! Im newbie in the LWJGL 3d rendering and Im trying to texture some terrain generated using heightmap and VBO. But something is wrong with the rendered textures (look into generated terrain):
generated terrain: https://i.imgur.com/TrszUy1.png
used texture: https://i.imgur.com/0mFmqm3.png
(texture is green quad with black edges)
How can I fix this?
Here is full code:
public class NGINE4 {
int w = 800;
int h = 600;
double map[][];
int Size = 200;
Texture floor;
public static void main(String[] args) throws Exception {
NGINE4 ng = new NGINE4();
ng.start();
}
void initContext() throws Exception {
Camera.create();
Display.setDisplayMode(new DisplayMode(w, h));
Display.setFullscreen(false);
Display.create();
GL11.glViewport(0, 0, w, h);
Mouse.setGrabbed(true);
floor = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("a.png"));
NoiseHeightMap nsh = new NoiseHeightMap(500, 6548816);
map = nsh.getHeightmap();
}
void renderLoop() {
GL11.glEnable(GL11.GL_TEXTURE_2D);
long time = 0;
int frames = 0;
int vertex_size = 3; // X, Y, Z,
int color_size = 3; // R, G, B,
int texture_size = 3; //X, Y, Z
FloatBuffer vBuffer = BufferUtils.createFloatBuffer(9 * (Size * Size * 2));
FloatBuffer cBuffer = BufferUtils.createFloatBuffer(9 * (Size * Size * 2));
FloatBuffer tBuffer = BufferUtils.createFloatBuffer(9 * (Size * Size * 2));
int heightmapExaggeration = 20;
Random rand = new Random();
for (int x = 0; x < Size - 1; x++) {
for (int y = 0; y < Size - 1; y++) {
cBuffer.put(1).put(1).put(1);
cBuffer.put(1).put(1).put(1);
cBuffer.put(1).put(1).put(1);
vBuffer.put(x * 0.25f).put((float) (map[x][y] * heightmapExaggeration)).put(y * 0.25f);
vBuffer.put((x + 1) * 0.25f).put((float) (map[x + 1][y] * heightmapExaggeration)).put(y * 0.25f);
vBuffer.put(x * 0.25f).put((float) (map[x][y + 1] * heightmapExaggeration)).put((y + 1) * 0.25f);
tBuffer.put(x * 0.25f).put((float) (map[x][y] * heightmapExaggeration)).put(y * 0.25f);
tBuffer.put((x + 1) * 0.25f).put((float) (map[x + 1][y] * heightmapExaggeration)).put(y * 0.25f);
tBuffer.put(x * 0.25f).put((float) (map[x][y + 1] * heightmapExaggeration)).put((y + 1) * 0.25f);
cBuffer.put(1).put(1).put(1);
cBuffer.put(1).put(1).put(1);
cBuffer.put(1).put(1).put(1);
vBuffer.put((x + 1) * 0.25f).put((float) (map[x + 1][y + 1] * heightmapExaggeration)).put((y + 1) * 0.25f);
vBuffer.put((x + 1) * 0.25f).put((float) (map[x + 1][y] * heightmapExaggeration)).put(y * 0.25f);
vBuffer.put(x * 0.25f).put((float) (map[x][y + 1] * heightmapExaggeration)).put((y + 1) * 0.25f);
tBuffer.put((x + 1) * 0.25f).put((float) (map[x + 1][y + 1] * heightmapExaggeration)).put((y + 1) * 0.25f);
tBuffer.put((x + 1) * 0.25f).put((float) (map[x + 1][y] * heightmapExaggeration)).put(y * 0.25f);
tBuffer.put(x * 0.25f).put((float) (map[x][y + 1] * heightmapExaggeration)).put((y + 1) * 0.25f);
}
}
cBuffer.flip();
vBuffer.flip();
tBuffer.flip();
int vbo_vertex_handle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo_vertex_handle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vBuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
int vbo_color_handle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo_color_handle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, cBuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
int vbo_tex_coord_handle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo_tex_coord_handle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, tBuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo_vertex_handle);
GL11.glVertexPointer(vertex_size, GL11.GL_FLOAT, 0, 0l);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo_color_handle);
GL11.glColorPointer(color_size, GL11.GL_FLOAT, 0, 0l);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, floor.getTextureID());
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo_tex_coord_handle);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
GL11.glTexCoordPointer(texture_size, GL11.GL_FLOAT, 0, 0l);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY_POINTER);
while (!Display.isCloseRequested()) {
preRender();
render();
Display.update();
Display.sync(60 /* desired fps */);
frames++;
if ((System.currentTimeMillis() - time) >= 1000) {
Display.setTitle(" FPS: " + (int) ((frames * 1000) / (System.currentTimeMillis() - time)));
time = System.currentTimeMillis();
frames = 0;
}
}
GL15.glDeleteBuffers(vbo_vertex_handle);
GL15.glDeleteBuffers(vbo_color_handle);
GL15.glDeleteBuffers(vbo_tex_coord_handle);
floor.release();
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY_POINTER);
Display.destroy();
}
void preRender() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluPerspective(45.0f, ((float) w / (float) h), 0.1f, 100.0f); // fix na błąd z kamerą
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void render() {
GL11.glClearColor(1, 1, 1, 1);
Camera.acceptInput(0.6f);
Camera.apply();
//GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, (Size*Size)*2);
//floor.bind();
/*for (int x = 0; x < (Size*Size)*2; x++) {
glDrawArrays(GL_TRIANGLES, x * 3, 3);
}*/
glDrawArrays(GL_TRIANGLES, 0, ((Size*Size)*2)*3);
}
public void start() throws Exception {
initContext();
renderLoop();
}
EDIT:
Resolved