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.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Locale;
- import java.util.Scanner;
- 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;
- }
- float[][] ZBuffer = new float[sirka][vyska];
- void trojuholnik(Vector4f troj[]) {
- // y1<=y2<=y3
- Vector4f pom;
- if (troj[1].y < troj[0].y) {
- pom = troj[1];
- troj[1] = troj[0];
- troj[0] = pom;
- }
- // horna cast
- int y1 = Math.round(troj[0].y);
- int y2 = Math.round(troj[1].y);
- int y3 = Math.round(troj[2].y);
- int x1 = Math.round(troj[0].x);
- int x2 = Math.round(troj[1].x);
- int x3 = Math.round(troj[2].x);
- int z1 = Math.round(troj[0].z);
- int z2 = Math.round(troj[1].z);
- int z3 = Math.round(troj[2].z);
- float xL = Math.round(troj[0].x), xR = Math.round(troj[0].x);
- float z = troj[0].z;
- float zL = z, zR = z;
- for (int y = y1; y < y2; y++) {
- z = zL;
- for (int x = Math.round(xL); x <= Math.round(xR); x++) {
- if (ZBuffer[x][y] < z) {
- glVertex2f(x, y);
- ZBuffer[x][y] = z;
- }
- z += (zR-zL)/(1.0f*xR-xL);
- }
- xL += (x2-x1)*1.0f/(y1-y2); xR += (x3-x1)*1.0f/(y3-y1);
- zL += (z2-z1)*1.0f/(y1-y2); zR += (z3-z1)*1.0f/(y3-y1);
- }
- // dolna cast
- for (int y = y2; y < y3; y++) {
- }
- }
- void vykresliGL() {
- // Clear the screen and depth buffer
- glLoadIdentity();
- for (int x = 0; x < sirka; x++) {
- for (int y = 0; y < vyska; y++) {
- ZBuffer[x][y] = -Float.MAX_VALUE;
- }
- }
- // glPointSize(3);
- glBegin(GL_QUADS);
- glColor3f(1, 1, 1);
- int poc = 0;
- for (int[] s : steny) {
- // glColor3f(farby[poc].x, farby[poc].y, farby[poc].z);
- Vector4f[] troj = new Vector4f[3];
- troj[0] = matica.multiply(cart2hom(vrcholy[s[0]]));
- troj[1] = matica.multiply(cart2hom(vrcholy[s[1]]));
- troj[2] = matica.multiply(cart2hom(vrcholy[s[2]]));
- 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();
- try {
- Scanner sc = new Scanner(new File("01.txt")).useLocale(Locale.ENGLISH);
- int n = sc.nextInt();
- vrcholy = new Vector3f[n];
- for (int i = 0; i < vrcholy.length; i++) {
- vrcholy[i] = new Vector3f(sc.nextFloat(), sc.nextFloat(), sc.nextFloat());
- }
- int m = sc.nextInt();
- steny = new int[m][3];
- for (int i = 0; i < steny.length; i++) {
- steny[i] = new int[3];
- steny[i][0] = sc.nextInt() - 1;
- steny[i][1] = sc.nextInt() - 1;
- steny[i][2] = sc.nextInt() - 1;
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- 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