Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.lwjgl.glfw.*;
- import org.lwjgl.opengl.GL;
- import org.lwjgl.opengl.GL12;
- import org.lwjgl.system.MemoryStack;
- import static org.lwjgl.stb.STBImage.*;
- import static org.lwjgl.stb.STBImageResize.*;
- import static org.lwjgl.glfw.GLFW.*;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.system.MemoryUtil.*;
- import static org.lwjgl.system.MemoryStack.*;
- import static org.lwjgl.BufferUtils.*;
- import static org.lwjgl.opengl.ARBMultitexture.*;
- import java.io.InputStream;
- import java.nio.*;
- import java.nio.channels.*;
- import java.nio.file.*;
- import java.io.*;
- public class cv091 {
- private int pfupjs;
- private int stena;
- private int podstava;
- private int voda;
- private int back, bottom, top, left, right, front;
- private double posX = 0;
- private double posZ = 0;
- private double uhol = 0;
- private double x = 0;
- private double z = 0.5;
- private float fogColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
- float[] farba_ambient = {1.0f, 1.0f, 1.0f, 1.0f};
- float[] farba_diffuse = {1.0f, 1.0f, 1.0f, 1.0f};
- float[] pozicia = {0, 0, 0, 1};
- float[] farba_red = {1.0f, 0f, 0f, 1.0f};
- float[] farba_green = {0f, 1.0f, 0f, 1.0f};
- float[] farba_blue = {0f, 0f, 1.0f, 1.0f};
- private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) {
- ByteBuffer newBuffer = org.lwjgl.BufferUtils.createByteBuffer(newCapacity);
- buffer.flip();
- newBuffer.put(buffer);
- return newBuffer;
- }
- private ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
- ByteBuffer buffer;
- Path path = Paths.get(resource);
- if (Files.isReadable(path)) {
- try (SeekableByteChannel fc = Files.newByteChannel(path)) {
- buffer = org.lwjgl.BufferUtils.createByteBuffer((int) fc.size() + 1);
- while (fc.read(buffer) != -1) {
- ;
- }
- }
- } else {
- try (InputStream source = this.getClass().getClassLoader().getResourceAsStream(resource);
- ReadableByteChannel rbc = Channels.newChannel(source)) {
- buffer = createByteBuffer(bufferSize);
- while (true) {
- int bytes = rbc.read(buffer);
- if (bytes == -1) {
- break;
- }
- if (buffer.remaining() == 0) {
- buffer = resizeBuffer(buffer, buffer.capacity() * 3 / 2); // 50%
- }
- }
- }
- }
- buffer.flip();
- return buffer.slice();
- }
- private int createTexture(String Path) {
- int texID = glGenTextures();
- ByteBuffer imageBuffer;
- try {
- imageBuffer = ioResourceToByteBuffer(Path, 8 * 1024);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- int w, h, comp;
- ByteBuffer image;
- try (MemoryStack stack = stackPush()) {
- IntBuffer wB = stack.mallocInt(1);
- IntBuffer hB = stack.mallocInt(1);
- IntBuffer compB = stack.mallocInt(1);
- // Use info to read image metadata without decoding the entire image.
- // We don't need this for this demo, just testing the API.
- if (!stbi_info_from_memory
- (imageBuffer, wB, hB, compB)) {
- throw new RuntimeException("Failed to read image information: " + stbi_failure_reason());
- }
- /*
- * System.out.println("Image width: " + wB.get(0));
- * System.out.println("Image height: " + hB.get(0));
- * System.out.println("Image components: " + compB.get(0));
- * System.out.println("Image HDR: " + stbi_is_hdr_from_memory(imageBuffer));
- */
- // Decode the image
- image = stbi_load_from_memory(imageBuffer, wB, hB, compB, 0);
- if (image == null) {
- throw new RuntimeException("Failed to load image: " + stbi_failure_reason());
- }
- w = wB.get(0);
- h = hB.get(0);
- comp = compB.get(0);
- }
- glBindTexture(GL_TEXTURE_2D, texID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- int format;
- if (comp == 3) {
- if ((w & 3) != 0) {
- glPixelStorei(GL_UNPACK_ALIGNMENT, 2 - (w & 1));
- }
- format = GL_RGB;
- } else {
- // premultiplyAlpha();
- // glEnable(GL_BLEND);
- // glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- format = GL_RGBA;
- }
- glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, image);
- ByteBuffer input_pixels = image;
- int input_w = w;
- int input_h = h;
- int mipmapLevel = 0;
- while (1 < input_w || 1 < input_h) {
- int output_w = Math.max(1, input_w >> 1);
- int output_h = Math.max(1, input_h >> 1);
- ByteBuffer output_pixels = memAlloc(output_w * output_h * comp);
- stbir_resize_uint8_generic(input_pixels, input_w, input_h, input_w * comp, output_pixels, output_w,
- output_h, output_w * comp, comp, comp == 4 ? 3 : STBIR_ALPHA_CHANNEL_NONE,
- STBIR_FLAG_ALPHA_PREMULTIPLIED, STBIR_EDGE_CLAMP, STBIR_FILTER_MITCHELL, STBIR_COLORSPACE_SRGB);
- if (mipmapLevel == 0) {
- stbi_image_free(image);
- } else {
- memFree(input_pixels);
- }
- glTexImage2D(GL_TEXTURE_2D, ++mipmapLevel, format, output_w, output_h, 0, format, GL_UNSIGNED_BYTE,
- output_pixels);
- input_pixels = output_pixels;
- input_w = output_w;
- input_h = output_h;
- }
- if (mipmapLevel == 0) {
- stbi_image_free(image);
- } else {
- memFree(input_pixels);
- }
- return texID;
- }
- private int sirka = 800;
- private int vyska = 600;
- Vector3f[] vrcholy = { new Vector3f(-1, 0, +1), new Vector3f(+1, 0, +1), new Vector3f(+1, 0, -1),
- new Vector3f(-1, 0, -1), new Vector3f(0, 2, 0), };
- Vector3f[] farba = { new Vector3f(1, 0, 0), new Vector3f(0, 1, 0), new Vector3f(0, 0, 1), new Vector3f(1, 0, 1),
- new Vector3f(1, 1, 0), new Vector3f(0, 1, 1) };
- int[][] steny = { { 0, 1, 4 }, { 1, 2, 4 }, { 2, 3, 4 }, { 3, 0, 4 }, { 0, 3, 2, 1 } };
- double t = 0;
- void vykresliGL() {
- // glLoadIdentity();
- // glRotated(-0.03, 1, 1, 1);
- // kockaPF();
- // pyramida();
- // t += 0.0001;
- // glTranslated(-1, 0, 0);
- // trojuholnik(voda, t);
- // glTranslated(2, 0, 0);
- // trojuholnik(stena, 0);
- // glTranslated(-1, -2, 0);
- // trojuholnik(stena, voda, t);
- // glTranslated(0, 2, 0);
- kocka();
- for (int ax = -50; ax <= 50; ax += 2) {
- for (int az = -50; az <= 50; az += 2) {
- if (Math.abs(ax + az) % 7 == Math.abs(ax - az) % 5) {
- ihlan(ax, az);
- } else {
- podlaha(ax, az);
- }
- }
- }
- glPointSize(100);
- glBegin(GL_POINTS);
- glVertex3d(pozicia[0], 0, pozicia[3]);
- glEnd();
- }
- private void podlaha(int ax, int az) {
- glBindTexture(GL_TEXTURE_2D, podstava);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 0);
- glVertex3f(ax - 1f, 0f, az + 1f);
- glTexCoord2d(1, 0);
- glVertex3f(ax + 1f, 0, az + 1f);
- glTexCoord2d(1, 1);
- glVertex3f(ax + 1f, 0, az - 1f);
- glTexCoord2d(0, 1);
- glVertex3f(ax - 1f, 0, az - 1f);
- glEnd();
- }
- private void ihlan(int ax, int az) {
- glBindTexture(GL_TEXTURE_2D, stena);
- glBegin(GL_TRIANGLES);
- glTexCoord2d(0, 1);
- glNormal3f(ax - 1.0f, 0.0f, az + 1.0f);
- glVertex3f(ax - 1.0f, 0.0f, az + 1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(ax + 1.0f, 0.0f, az + 1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(ax + 0.0f, 2.0f, az + 0.0f);
- glTexCoord2d(0, 1);
- glNormal3f(ax + 1.0f, 0.0f, az + 1.0f);
- glVertex3f(ax + 1.0f, 0.0f, az + 1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(ax + 1.0f, 0.0f, az - 1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(ax + 0.0f, 2.0f, az + 0.0f);
- glTexCoord2d(0, 1);
- glNormal3f(ax + 1.0f, 0.0f, az - 1.0f);
- glVertex3f(ax + 1.0f, 0.0f, az - 1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(ax - 1.0f, 0.0f, az - 1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(ax + 0.0f, 2.0f, az + 0.0f);
- glTexCoord2d(0, 1);
- glNormal3f(ax - 1.0f, 0.0f, az - 1.0f);
- glVertex3f(ax - 1.0f, 0.0f, az - 1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(ax - 1.0f, 0.0f, az + 1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(ax + 0.0f, 2.0f, az + 0.0f);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, podstava);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 0);
- glVertex3f(ax - 1.0f, 0.0f, az - 1.0f);
- glTexCoord2d(1, 0);
- glVertex3f(ax + 1.0f, 0.0f, az - 1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(ax + 1.0f, 0.0f, az + 1.0f);
- glTexCoord2d(0, 1);
- glVertex3f(ax - 1.0f, 0.0f, az + 1.0f);
- glEnd();
- }
- void trojuholnik(int textura, double t) {
- glActiveTextureARB(GL_TEXTURE0_ARB);
- glBindTexture(GL_TEXTURE_2D, textura);
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_TRIANGLES);
- glTexCoord2d(0 + t, 1 + t);
- glVertex2f(-1.0f, 0.0f);
- glTexCoord2d(1 + t, 1 + t);
- glVertex2f(1.0f, 0.0f);
- glTexCoord2d(0.5 + t, 0 + t);
- glVertex2f(0.0f, 2.0f);
- glEnd();
- glActiveTextureARB(GL_TEXTURE0_ARB);
- glDisable(GL_TEXTURE_2D);
- }
- void trojuholnik(int textura1, int textura2, double t) {
- glActiveTextureARB(GL_TEXTURE1_ARB);
- glBindTexture(GL_TEXTURE_2D, textura1);
- glEnable(GL_TEXTURE_2D);
- glActiveTextureARB(GL_TEXTURE2_ARB);
- glBindTexture(GL_TEXTURE_2D, textura2);
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_TRIANGLES);
- glMultiTexCoord2dARB(GL_TEXTURE1_ARB, 0, 1);
- glMultiTexCoord2dARB(GL_TEXTURE2_ARB, 0 + t, 1 + t);
- glVertex2f(-1.0f, 0.0f);
- glMultiTexCoord2dARB(GL_TEXTURE1_ARB, 1, 1);
- glMultiTexCoord2dARB(GL_TEXTURE2_ARB, 1 + t, 1 + t);
- glVertex2f(1.0f, 0.0f);
- glMultiTexCoord2dARB(GL_TEXTURE1_ARB, 0.5, 0);
- glMultiTexCoord2dARB(GL_TEXTURE2_ARB, 0.5 + t, 0 + t);
- glVertex2f(0.0f, 2.0f);
- glEnd();
- glActiveTextureARB(GL_TEXTURE1_ARB);
- glDisable(GL_TEXTURE_2D);
- glActiveTextureARB(GL_TEXTURE2_ARB);
- glDisable(GL_TEXTURE_2D);
- }
- void kockaPF() {
- glBindTexture(GL_TEXTURE_2D, pfupjs);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 1);
- glVertex3d(-1, -1, 0);
- glTexCoord2d(1, 1);
- glVertex3d(+1, -1, 0);
- glTexCoord2d(1, 0);
- glVertex3d(+1, +1, 0);
- glTexCoord2d(0, 0);
- glVertex3d(-1, +1, 0);
- glTexCoord2d(0, 2);
- glVertex3d(-1, -1, -2);
- glTexCoord2d(2, 2);
- glVertex3d(-1, -1, 0);
- glTexCoord2d(2, 0);
- glVertex3d(-1, 1, 0);
- glTexCoord2d(0, 0);
- glVertex3d(-1, 1, -2);
- glTexCoord2d(0, 0);
- glVertex3d(-1, -1, -2);
- glTexCoord2d(0, 3);
- glVertex3d(-1, 1, -2);
- glTexCoord2d(3, 3);
- glVertex3d(1, 1, -2);
- glTexCoord2d(3, 0);
- glVertex3d(1, -1, -2);
- glTexCoord2d(0, 4);
- glVertex3d(1, -1, 0);
- glTexCoord2d(4, 4);
- glVertex3d(1, -1, -2);
- glTexCoord2d(4, 0);
- glVertex3d(1, 1, -2);
- glTexCoord2d(0, 0);
- glVertex3d(1, 1, 0);
- glTexCoord2d(0, 5);
- glVertex3d(-1, 1, 0);
- glTexCoord2d(5, 5);
- glVertex3d(1, 1, 0);
- glTexCoord2d(5, 0);
- glVertex3d(1, 1, -2);
- glTexCoord2d(0, 0);
- glVertex3d(-1, 1, -2);
- glTexCoord2d(0, 6);
- glVertex3d(-1, -1, -2);
- glTexCoord2d(6, 6);
- glVertex3d(1, -1, -2);
- glTexCoord2d(6, 0);
- glVertex3d(1, -1, 0);
- glTexCoord2d(0, 0);
- glVertex3d(-1, -1, 0);
- glEnd();
- }
- void kocka() {
- // back
- glBindTexture(GL_TEXTURE_2D, front);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 1);
- glVertex3d(50, -50, 50);
- glTexCoord2d(1, 1);
- glVertex3d(-50, -50, 50);
- glTexCoord2d(1, 0);
- glVertex3d(-50, 50, 50);
- glTexCoord2d(0, 0);
- glVertex3d(50, 50, 50);
- glEnd();
- // left
- glBindTexture(GL_TEXTURE_2D, left);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 1);
- glVertex3d(-50, -50, 50);
- glTexCoord2d(1, 1);
- glVertex3d(-50, -50, -50);
- glTexCoord2d(1, 0);
- glVertex3d(-50, 50, -50);
- glTexCoord2d(0, 0);
- glVertex3d(-50, 50, 50);
- glEnd();
- // front
- glBindTexture(GL_TEXTURE_2D, back);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 1);
- glVertex3d(-50, -50, -50);
- glTexCoord2d(1, 1);
- glVertex3d(50, -50, -50);
- glTexCoord2d(1, 0);
- glVertex3d(50, 50, -50);
- glTexCoord2d(0, 0);
- glVertex3d(-50, 50, -50);
- glEnd();
- // right
- glBindTexture(GL_TEXTURE_2D, right);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 1);
- glVertex3d(50, -50, -50);
- glTexCoord2d(1, 1);
- glVertex3d(50, -50, 50);
- glTexCoord2d(1, 0);
- glVertex3d(50, 50, 50);
- glTexCoord2d(0, 0);
- glVertex3d(50, 50, -50);
- glEnd();
- // top
- glBindTexture(GL_TEXTURE_2D, top);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 1);
- glVertex3d(-50, 50, -50);
- glTexCoord2d(1, 1);
- glVertex3d(50, 50, -50);
- glTexCoord2d(1, 0);
- glVertex3d(50, 50, 50);
- glTexCoord2d(0, 0);
- glVertex3d(-50, 50, 50);
- glEnd();
- // bottom
- glBindTexture(GL_TEXTURE_2D, bottom);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 1);
- glVertex3d(-50, -50, 50);
- glTexCoord2d(1, 1);
- glVertex3d(50, -50, 50);
- glTexCoord2d(1, 0);
- glVertex3d(50, -50, -50);
- glTexCoord2d(0, 0);
- glVertex3d(-50, -50, -50);
- glEnd();
- }
- void pyramidaVoda() {
- glBindTexture(GL_TEXTURE_2D, voda);
- glBegin(GL_TRIANGLES);
- glTexCoord2d(0, 1);
- glVertex3f(-1.0f, 0.0f, 1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(1.0f, 0.0f, 1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(0.0f, 2.0f, 0.0f);
- glTexCoord2d(0, 1);
- glVertex3f(1.0f, 0.0f, 1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(1.0f, 0.0f, -1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(0.0f, 2.0f, 0.0f);
- glTexCoord2d(0, 1);
- glVertex3f(1.0f, 0.0f, -1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(-1.0f, 0.0f, -1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(0.0f, 2.0f, 0.0f);
- glTexCoord2d(0, 1);
- glVertex3f(-1.0f, 0.0f, -1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(-1.0f, 0.0f, 1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(0.0f, 2.0f, 0.0f);
- glEnd();
- glBegin(GL_QUADS);
- glTexCoord2d(0, 0);
- glVertex3f(-1.0f, 0.0f, -1.0f);
- glTexCoord2d(1, 0);
- glVertex3f(1.0f, 0.0f, -1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(1.0f, 0.0f, 1.0f);
- glTexCoord2d(0, 1);
- glVertex3f(-1.0f, 0.0f, 1.0f);
- glEnd();
- }
- void pyramida() {
- glBindTexture(GL_TEXTURE_2D, stena);
- glBegin(GL_TRIANGLES);
- glTexCoord2d(0, 1);
- glVertex3f(-1.0f, 0.0f, 1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(1.0f, 0.0f, 1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(0.0f, 2.0f, 0.0f);
- glTexCoord2d(0, 1);
- glVertex3f(1.0f, 0.0f, 1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(1.0f, 0.0f, -1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(0.0f, 2.0f, 0.0f);
- glTexCoord2d(0, 1);
- glVertex3f(1.0f, 0.0f, -1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(-1.0f, 0.0f, -1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(0.0f, 2.0f, 0.0f);
- glTexCoord2d(0, 1);
- glVertex3f(-1.0f, 0.0f, -1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(-1.0f, 0.0f, 1.0f);
- glTexCoord2d(0.5, 0);
- glVertex3f(0.0f, 2.0f, 0.0f);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, podstava);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 0);
- glVertex3f(-1.0f, 0.0f, -1.0f);
- glTexCoord2d(1, 0);
- glVertex3f(1.0f, 0.0f, -1.0f);
- glTexCoord2d(1, 1);
- glVertex3f(1.0f, 0.0f, 1.0f);
- glTexCoord2d(0, 1);
- glVertex3f(-1.0f, 0.0f, 1.0f);
- glEnd();
- }
- long window;
- GLFWErrorCallback errorCallback;
- GLFWKeyCallback keyCallback;
- void spusti() {
- try {
- init();
- loop();
- glfwDestroyWindow(window);
- keyCallback.free();
- } finally {
- glfwTerminate();
- errorCallback.free();
- }
- }
- void init() {
- glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
- if (!glfwInit())
- throw new IllegalStateException("Chyba pri inicializacii GLFW!!!");
- window = glfwCreateWindow(sirka, vyska, "UGR1", NULL, NULL);
- if (window == NULL)
- throw new RuntimeException("Chyba pri vytvoreni GLFW okna!!!");
- glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
- @Override
- public void invoke(long window, int key, int scancode, int action, int mods) {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
- glfwSetWindowShouldClose(window, true);
- if (key == GLFW_KEY_W) {
- glTranslated(x, 0, z);
- posX += x;
- posZ += z;
- System.out.println("posX = " + posX + " posZ = " + posZ);
- }
- if (key == GLFW_KEY_S) {
- glTranslated(-x, 0, -z);
- posX -= x;
- posZ -= z;
- System.out.println("posX = " + posX + " posZ = " + posZ);
- }
- if (key == GLFW_KEY_A) {
- glTranslated(-posX, 0, -posZ);
- glRotated(-1, 0, 1, 0);
- glTranslated(posX, 0, posZ);
- if (uhol == -180) {
- uhol += 360;
- }
- uhol -= 1;
- x = -0.5 * Math.sin(Math.toRadians(uhol));
- z = 0.5 * Math.cos(Math.toRadians(uhol));
- System.out.println(uhol);
- }
- if (key == GLFW_KEY_D) {
- glTranslated(-posX, 0, -posZ);
- glRotated(1, 0, 1, 0);
- glTranslated(posX, 0, posZ);
- if (uhol == 180) {
- uhol -= 360;
- }
- uhol += 1;
- x = -0.5 * Math.sin(Math.toRadians(uhol));
- z = 0.5 * Math.cos(Math.toRadians(uhol));
- System.out.println(uhol);
- }
- if (key == GLFW_KEY_Z) {
- glTranslated(0, -1, 0);
- }
- if (key == GLFW_KEY_X) {
- glTranslated(0, 1, 0);
- }
- if (key == GLFW_KEY_C) {
- glRotated(1, -1, 0, 0);
- }
- if (key == GLFW_KEY_V) {
- glRotated(1, 1, 0, 0);
- }
- }
- });
- GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
- glfwSetWindowPos(window, (vidmode.width() - sirka) / 2, (vidmode.height() - vyska) / 2);
- glfwMakeContextCurrent(window);
- glfwSwapInterval(0);
- glfwShowWindow(window);
- GL.createCapabilities();
- glEnable(GL_TEXTURE_2D);
- // glBindTexture(GL_TEXTURE_2D, createTexture("D:\\pfupjs.png"));
- pfupjs = createTexture("D:\\pfupjs.png");
- stena = createTexture("D:\\stena.jpg");
- podstava = createTexture("D:\\podstava.jpg");
- voda = createTexture("D:\\voda.jpg");
- back = createTexture("D:\\back.jpg");
- bottom = createTexture("D:\\bottom.jpg");
- top = createTexture("D:\\top.jpg");
- left = createTexture("D:\\left.jpg");
- right = createTexture("D:\\right.jpg");
- front = createTexture("D:\\front.jpg");
- glClearColor(fogColor[0], fogColor[1], fogColor[2], fogColor[3]);
- glFogi(GL_FOG_MODE, GL_LINEAR); //GL_EXP, GL_EXP2, GL_LINEAR
- glFogfv(GL_FOG_COLOR, fogColor);
- glHint(GL_FOG_HINT, GL_DONT_CARE);
- //near/far (GL_LINEAR);
- glFogf(GL_FOG_START, 10f);
- glFogf(GL_FOG_END, 20f);
- //density (GL_EXP/GL_EXP2);
- glFogf(GL_FOG_DENSITY, 0.5f);
- //glEnable(GL_FOG);
- glLightfv(GL_LIGHT0, GL_AMBIENT, farba_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, farba_diffuse);
- glLightfv(GL_LIGHT0, GL_POSITION, pozicia);
- glEnable(GL_LIGHTING); //Zapnutie osvetlenia
- glEnable(GL_LIGHT0); //Zapnutie prvého svetla
- glEnable(GL_NORMALIZE);
- }
- private static void gluPerspective(double fov, double aspect, double zNear, double zFar) {
- double fH = Math.tan(fov * Math.PI / 360.0) * zNear;
- double fW = fH * aspect;
- glFrustum(-fW, fW, -fH, fH, zNear, zFar);
- }
- void loop() {
- glViewport(0, 0, sirka, vyska);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0f, (sirka / (1.0 * vyska)), 0.1f, 100.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glClearColor(0.f, 0.f, 0.f, 1.f); // Initialize clear color
- glTranslated(0, -1, 0);
- glShadeModel(GL_SMOOTH);
- glCullFace(GL_BACK);
- glEnable(GL_CULL_FACE); // Zneviditeľnenie odvrátených strán
- glDepthFunc(GL_LEQUAL);
- glEnable(GL_DEPTH_TEST);
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- vykresliGL();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- }
- public static void main(String[] args) {
- new cv091().spusti();
- }
- }
Add Comment
Please, Sign In to add comment