Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.lwjgl.glfw.*;
- import org.lwjgl.opengl.GL;
- import static org.lwjgl.glfw.GLFW.*;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.system.MemoryUtil.*;
- public class cv04 {
- private int sirka = 800;
- private int vyska = 600;
- Vector3f[] vrcholy = { new Vector3f(-0.5f, -0.5f, 0.5f), new Vector3f(0.5f, -0.5f, 0.5f),
- new Vector3f(0.5f, -0.5f, -0.5f), new Vector3f(-0.5f, -0.5f, -0.5f), new Vector3f(-0.5f, 0.5f, 0.5f),
- new Vector3f(0.5f, 0.5f, 0.5f), new Vector3f(0.5f, 0.5f, -0.5f), new Vector3f(-0.5f, 0.5f, -0.5f) };
- int[][] hrany = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 }, { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 }, { 0, 4 },
- { 1, 5 }, { 2, 6 }, { 3, 7 } };
- int[][] steny = { { 0, 1, 5, 4 }, { 1, 2, 6, 5 }, { 2, 3, 7, 6 }, { 3, 0, 4, 7 }, { 4, 5, 6, 7 }, { 3, 2, 1, 0 } };
- Matrix4f matica;
- Vector4f cart2hom(Vector3f v) {
- return new Vector4f(v.x, v.y, v.z, 1.0f);
- }
- Vector2f priemet(Vector4f v) {
- // return new Vector2f(v.x + v.z * 0.5f, v.y + v.z * 0.5f);
- return new Vector2f(v.x, v.y);
- }
- Vector3f[] farby = { new Vector3f(0, 1, 1), new Vector3f(1, 0, 1), new Vector3f(1, 1, 0), new Vector3f(1, 1, 1),
- new Vector3f(1, 0, 0), new Vector3f(0, 0, 1), new Vector3f(0, 1, 0), new Vector3f(0.5f, 1, 1),
- new Vector3f(1, 0.5f, 1), new Vector3f(1, 1, 0.5f), new Vector3f(1, 0, 0.5f),
- new Vector3f(0.5f, 0.5f, 0.5f) };
- Matrix4f maticaVelkosti(float tx, float ty, float tz) {
- return new Matrix4f(new Vector4f(tx, 0, 0, 0), new Vector4f(0, ty, 0, 0), new Vector4f(0, 0, tz, 0),
- new Vector4f(0, 0, 0, 1));
- }
- Matrix4f maticaPos(float tx, float ty, float tz) {
- return new Matrix4f(new Vector4f(1, 0, 0, 0), new Vector4f(0, 1, 0, 0), new Vector4f(0, 0, 1, 0),
- new Vector4f(tx, ty, tz, 1));
- }
- Matrix4f maticaOtocX(float uhol) {
- float cos = (float) (Math.cos(uhol));
- float sin = (float) (Math.sin(uhol));
- return new Matrix4f(new Vector4f(1, 0, 0, 0), new Vector4f(0, cos, sin, 0), new Vector4f(0, -sin, cos, 0),
- new Vector4f(0, 0, 0, 1));
- }
- Matrix4f maticaOtocY(float uhol) {
- float cos = (float) (Math.cos(uhol));
- float sin = (float) (Math.sin(uhol));
- return new Matrix4f(new Vector4f(cos, 0, sin, 0), new Vector4f(0, 1, 0, 0), new Vector4f(-sin, 0, cos, 0),
- new Vector4f(0, 0, 0, 1));
- }
- Matrix4f maticaOtocZ(float uhol) {
- float cos = (float) (Math.cos(uhol));
- float sin = (float) (Math.sin(uhol));
- return new Matrix4f(new Vector4f(cos, sin, 0, 0), new Vector4f(-sin, cos, 0, 0), new Vector4f(0, 0, 1, 0),
- new Vector4f(0, 0, 0, 1));
- }
- boolean privratena(int[] s) {
- // Vector3f A = vrcholy[s[0]];
- // Vector3f B = vrcholy[s[1]];
- // Vector3f C = vrcholy[s[2]];
- Vector4f A_hom = cart2hom(vrcholy[s[0]]);
- Vector4f A_trans = matica.multiply(A_hom);
- Vector4f B_hom = cart2hom(vrcholy[s[1]]);
- Vector4f B_trans = matica.multiply(B_hom);
- Vector4f C_hom = cart2hom(vrcholy[s[2]]);
- Vector4f C_trans = matica.multiply(C_hom);
- Vector4f u = A_trans.subtract(B_trans);
- Vector4f v = C_trans.subtract(B_trans);
- Vector3f n = new Vector3f(u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z ,u.x * v.y - u.y * v.x);
- Vector3f svetlo = new Vector3f(0, 1, 0);
- double cosinus = (n.x*svetlo.x + n.y*svetlo.y + n.z*svetlo.z)/(Math.sqrt(n.x*n.x + n.y*n.y + n.z*n.z) * Math.sqrt(svetlo.x*svetlo.x + svetlo.y*svetlo.y + svetlo.z*svetlo.z));
- if (n.z > 0) {
- return true;
- }
- return false;
- }
- void vykresliGL() {
- // Clear the screen and depth buffer
- glLoadIdentity();
- glPointSize(3);
- glBegin(GL_QUADS);
- glColor3f(1, 1, 1);
- int poc = 0;
- for (int[] s : steny) {
- if (privratena(s)) {
- glColor3f(farby[poc].x, farby[poc].y, farby[poc].z);
- for (int v : s) {
- Vector4f v_hom = cart2hom(vrcholy[v]);
- Vector4f v_trans = matica.multiply(v_hom);
- Vector2f p = priemet(v_trans);
- glVertex2f(p.x, p.y);
- }
- }
- poc++;
- }
- 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_RIGHT)
- matica = matica.multiply(maticaPos(0.01f, 0, 0));
- if (key == GLFW_KEY_LEFT)
- matica = matica.multiply(maticaPos(-0.01f, 0, 0));
- if (key == GLFW_KEY_UP)
- matica = matica.multiply(maticaPos(0, 0.01f, 0));
- if (key == GLFW_KEY_DOWN)
- matica = matica.multiply(maticaPos(0, -0.01f, 0));
- if (key == GLFW_KEY_N)
- matica = matica.multiply(maticaPos(0, 0, 0.01f));
- if (key == GLFW_KEY_M)
- matica = matica.multiply(maticaPos(0, 0, -0.01f));
- if (key == GLFW_KEY_W)
- matica = matica.multiply(maticaOtocX(0.05f));
- if (key == GLFW_KEY_S)
- matica = matica.multiply(maticaOtocX(-0.05f));
- if (key == GLFW_KEY_A)
- matica = matica.multiply(maticaOtocY(-0.05f));
- if (key == GLFW_KEY_D)
- matica = matica.multiply(maticaOtocY(0.05f));
- if (key == GLFW_KEY_Q)
- matica = matica.multiply(maticaOtocZ(0.05f));
- if (key == GLFW_KEY_E)
- matica = matica.multiply(maticaOtocZ(-0.05f));
- if (key == GLFW_KEY_X)
- matica = matica.multiply(maticaVelkosti(1.1f, 1.1f, 1.1f));
- if (key == GLFW_KEY_Z)
- matica = matica.multiply(maticaVelkosti(1 / 1.1f, 1 / 1.1f, 1 / 1.1f));
- }
- });
- GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
- glfwSetWindowPos(window, (vidmode.width() - sirka) / 2, (vidmode.height() - vyska) / 2);
- glfwMakeContextCurrent(window);
- glfwSwapInterval(0);
- glfwShowWindow(window);
- GL.createCapabilities();
- matica = new Matrix4f();
- matica.setIdentity();
- }
- void loop() {
- glViewport(0, 0, sirka, vyska);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-1, 1, -1, 1, 0, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glClearColor(0.f, 0.f, 0.f, 1.f); // Initialize clear color
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- vykresliGL();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- }
- public static void main(String[] args) {
- new cv04().spusti();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement