Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ksg.lab1;
- import com.jogamp.common.nio.Buffers;
- import ksg.common.GameObject;
- import com.jogamp.opengl.GL2;
- import com.jogamp.opengl.util.texture.Texture;
- import java.awt.image.BufferedImage;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import ksg.common.Vector3;
- /**
- * @author MK
- */
- // Klasa reprezentująca model terenu otrzymanego za pomocą VBO.
- public class GroundVbo extends GameObject {
- int[] buffers;
- protected FloatBuffer vertices, texCoords, normals;
- protected IntBuffer indices;
- protected int verticesHandle, texCoordsHandle, normalsHandle, indicesHandle;
- protected Texture texture = null;
- protected int diffuseTextureHandle, displacementTextureHandle;
- protected BufferedImage displacementMap;
- //zad5
- private int columns = 128;
- private int rows = 128;
- private float[] vertexArray = new float[columns * rows * 3];
- private float[] texCoordArray = new float[columns * rows * 2];
- private float[] normalArray = new float[columns * rows * 3];
- private int[] indexArray = new int[columns * rows * 6];
- private float elevation = 0.00000000000000000000000000000000000001f;
- public GroundVbo(Vector3 position, Texture texture, BufferedImage displacementMap) {
- super();
- setPosition(position);
- this.texture = texture;
- this.displacementMap = displacementMap;
- }
- public GroundVbo(float x, float y, float z, Texture texture, BufferedImage displacementMap) {
- this(new Vector3(x, y, z), texture, displacementMap);
- }
- public void generateBuffers(GL2 gl) {
- for (int yy = 0; yy < rows; yy++) {
- elevation += elevation;
- for (int xx = 0; xx < columns; xx++) {
- int rgb = displacementMap.getRGB(xx * 7, yy * 7);
- int r = (rgb >> 16);
- int g = (rgb >> 8) & 0xff;
- int b = (rgb) & 0xff;
- int gray = (r + g + b) / 3;
- int offset = 3 * (yy * columns + xx);
- vertexArray[offset] = 100 - (xx * 10);
- vertexArray[offset + 1] += gray;
- vertexArray[offset + 2] = 100 - (yy * 10);
- }
- }
- int i = 0;
- for (int yy = 0; yy < rows - 1; yy++) {
- for (int xx = 0; xx < columns - 1; xx++) {
- int offset = yy * columns + xx;
- indexArray[i] = (short) (offset);
- indexArray[i + 1] = (short) (offset + columns);
- indexArray[i + 2] = (short) (offset + 1);
- indexArray[i + 3] = (short) (offset + 1);
- indexArray[i + 4] = (short) (offset + columns);
- indexArray[i + 5] = (short) (offset + columns + 1);
- i += 6;
- }
- }
- // Stworzenie bufora przechowującego informacje o położeniu wierzchołków (3 składowe: x,y,z).
- // float[] vertexArray = {-500, 0, -500,
- // 500, 0, -500,
- // -500, 0, 500,
- // 500, 0, 500};
- vertices = Buffers.newDirectFloatBuffer(vertexArray.length);
- vertices.put(vertexArray);
- vertices.flip();
- // Stworzenie bufora przechowującego informacje o mapowaniu tekstury (2 składowe: u,v).
- // float[] texCoordArray = {0f, 0f,
- // 1f, 0f,
- // 1f, 1f,
- // 0f, 1f};
- texCoords = Buffers.newDirectFloatBuffer(texCoordArray.length);
- texCoords.put(texCoordArray);
- texCoords.flip();
- // Stworzenie bufora przechowującego normalne wierzchołków (3 składowe: x,y,z).
- // float[] normalArray = {0, 0, 1,
- // 0, 0, 1,
- // 0, 0, 1,
- // 0, 0, 1};
- normals = Buffers.newDirectFloatBuffer(normalArray.length);
- normals.put(normalArray);
- normals.flip();
- // Stworzenie bufora opisującego sposób budowania trójkątów na podstawie indeksów wierzchołków.
- // 0---1
- // | /|
- // | / |
- // |/ |
- // 2---3
- // int[] indexArray = {0, 2, 1, 1, 2, 3};
- indices = Buffers.newDirectIntBuffer(indexArray.length);
- indices.put(indexArray);
- indices.flip();
- buffers = new int[4]; // "uchwyty" buforów przechowujących informacje o modelu
- gl.glGenBuffers(buffers.length, buffers, 0);
- // Podpięcie bufora pozycji wierzchołków.
- verticesHandle = buffers[0];
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, verticesHandle);
- gl.glBufferData(GL2.GL_ARRAY_BUFFER, vertices.capacity() * Buffers.SIZEOF_FLOAT,
- vertices, GL2.GL_STATIC_DRAW);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
- // Podpięcie bufora mapowania tekstury.
- texCoordsHandle = buffers[1];
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, texCoordsHandle);
- gl.glBufferData(GL2.GL_ARRAY_BUFFER, texCoords.capacity() * Buffers.SIZEOF_FLOAT,
- texCoords, GL2.GL_STATIC_DRAW);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
- // Podpięcie bufora normalnych.
- normalsHandle = buffers[2];
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, normalsHandle);
- gl.glBufferData(GL2.GL_ARRAY_BUFFER, normals.capacity() * Buffers.SIZEOF_FLOAT,
- normals, GL2.GL_STATIC_DRAW);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
- // Podpięcie bufora indeksów.
- indicesHandle = buffers[3];
- gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indicesHandle);
- gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.capacity() * Buffers.SIZEOF_INT,
- indices, GL2.GL_STATIC_DRAW);
- gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
- }
- @Override
- public void draw(GL2 gl) {
- gl.glPushMatrix(); // odłożenie aktualnego stanu macierzy transformacji na stos
- gl.glTranslatef(position.x, position.y, position.z); // ustawienie pozycji
- // Włączenie tekstury.
- gl.glEnable(texture.getTarget());
- gl.glActiveTexture(gl.GL_TEXTURE0);
- gl.glBindTexture(texture.getTarget(), texture.getTextureObject());
- // Włączenie poszczególnych buforów.
- gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
- gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, texCoordsHandle);
- gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, 0);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, normalsHandle);
- gl.glNormalPointer(GL2.GL_FLOAT, 0, 0);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, verticesHandle);
- gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
- gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indicesHandle);
- gl.glDrawElements(GL2.GL_TRIANGLES, indices.capacity(), GL2.GL_UNSIGNED_INT, 0); // narysowanie modelu
- // Wyłączenie buforów.
- gl.glDisableClientState(GL2.GL_NORMAL_ARRAY);
- gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
- gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
- gl.glDisable(texture.getTarget()); // wyłączenie tekstury
- gl.glUseProgram(0); // użycie domyślnego programu cieniującego
- gl.glPopMatrix(); // przywrócenie poprzedniego stanu macierzy transformacji modeli
- }
- @Override
- public void freeMemory(GL2 gl) {
- gl.glDeleteBuffers(buffers.length, buffers, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement