【JAVA】OPENGL绕XYZ轴旋转立体图效果

2023-12-28 13:45:54

JAVA-OPENGL绕XYZ轴旋转立体图效果_哔哩哔哩_bilibiliJAVA-OPENGL绕XYZ轴旋转立体图效果开始显示的是绕X轴、Y轴、Z轴旋转,后边是同时绕两个轴旋转,头有点晕,反应不过来了。, 视频播放量 1、弹幕量 0、点赞数 0、投硬币枚数 0、收藏人数 0、转发人数 0, 视频作者 缘起性空aa, 作者简介 ,相关视频:iPhone空间视频转变为3d红蓝立体视频,手机VR身临其境玩《巨击大乱斗》体验奥特曼飞出屏幕!!【超级视觉】3D VR 立体出屏 震撼体验,地狱级“3D立体拼图”挑战!3D牛肉VS猪骨架,一个比一个更烧脑?,纯干货|手把手教你打造立体四区阳台,平面设计CDR教程-立体包装盒效果,认知水平越高,看不惯的东西越少!钩针编织创意立体手工花,【钩针编织】-玫瑰花/山茶花的立体花一体成型钩织教程,手机VR身临其境玩《刀魂6》时间暂停感受出屏瞬间!【超级视觉】3D VR 立体出屏 震撼体验,这是一种很新颖的东西,自动化立体仓库建模仿真与优化icon-default.png?t=N7T8https://www.bilibili.com/video/BV1je411q7n5/?vd_source=0578c14da2b84f0964bbee439d4fd921?

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;
import java.util.logging.Logger;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL32.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

public class HelloWorld {

    // The window handle
    private long window;
    public static Logger logger = MyLogger.logger;
    private float angle;

    public void run() {
        logger.info("Hello LWJGL " + Version.getVersion() + "!");

        init();
        loop();
        // Free the window callbacks and destroy the window
        glfwFreeCallbacks(window);
        glfwDestroyWindow(window);

        // Terminate GLFW and free the error callback
        glfwTerminate();
        glfwSetErrorCallback(null).free();
    }

    private void init() {
        logger.info("init");
        // Setup an error callback. The default implementation
        // will print the error message in System.err.
        GLFWErrorCallback.createPrint(System.err).set();

        angle = 5;

        // Initialize GLFW. Most GLFW functions will not work before doing this.
        if ( !glfwInit() )
            throw new IllegalStateException("Unable to initialize GLFW");

        // Configure GLFW
        glfwDefaultWindowHints(); // optional, the current window hints are already the default
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
        glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable

        // Create the window
        window = glfwCreateWindow(600, 600, "Hello World!", NULL, NULL);
        if ( window == NULL )
            throw new RuntimeException("Failed to create the GLFW window");

        // Setup a key callback. It will be called every time a key is pressed, repeated or released.
        glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
            if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
                glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
        });

        // Get the thread stack and push a new frame
        try ( MemoryStack stack = stackPush() ) {
            IntBuffer pWidth = stack.mallocInt(1); // int*
            IntBuffer pHeight = stack.mallocInt(1); // int*

            // Get the window size passed to glfwCreateWindow
            glfwGetWindowSize(window, pWidth, pHeight);

            // Get the resolution of the primary monitor
            GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

            // Center the window
            glfwSetWindowPos(
                    window,
                    (vidmode.width() - pWidth.get(0)) / 2,
                    (vidmode.height() - pHeight.get(0)) / 2
            );
        } // the stack frame is popped automatically

        // Make the OpenGL context current
        glfwMakeContextCurrent(window);
        // Enable v-sync
        glfwSwapInterval(1);

        // Make the window visible
        glfwShowWindow(window);
    }

    /**
     * 绘制三角形
     */
    public void DrawTriangles()
    {
        glTranslatef(0.1f, -0.2f, -0.20f); //平移矩阵
        glRotatef(angle, 1.0f, 0.0f, 1.0f); //绕X,Y, Z轴直线旋转XX度
        glLineWidth(2);
        glColor3f(1f, 0f, 0f);
        glBegin(GL_TRIANGLES);

        //前侧面
        glColor3f(0.5f, 0.0f, 0.0f);
        glVertex3f(0.0f, 0.5f, 0.0f);      //上顶点
        glVertex3f(-0.5f, -0.5f, 0.5f);    //左下顶点
        glVertex3f(0.5f, -0.5f, 0.5f);     //右下顶点

        //右侧面
        glColor3f(0.0f, 0.5f, 0.0f);
        glVertex3f(0.0f, 0.5f, 0.0f);      //上顶点
        glVertex3f(0.5f, -0.5f, 0.5f);     //左下顶点
        glVertex3f(0.5f, -0.5f, -0.5f);    //右下顶点

        //后侧面
        glColor3f(0.0f, 0.0f, 0.5f);
        glVertex3f(0.0f, 0.5f, 0.0f);      //上顶点
        glVertex3f(0.5f, -0.5f, -0.5f);    //左下顶点
        glVertex3f(-0.5f, -0.5f, -0.5f);   //右下顶点

        //左侧面
        glColor3f(1f, 0.0f, 0.0f);
        glVertex3f(0.0f, 0.5f, 0.0f);      //上顶点
        glVertex3f(-0.5f, -0.5f, -0.5f);   //左下顶点
        glVertex3f(-0.5f, -0.5f, 0.5f);    //右下顶点
        angle+=5;
        glEnd();
    }

    public void DrawLines()
    {
        glBegin(GL_LINES);
        glVertex2i(0, 0);
        glVertex2i(0, 1);
        glEnd();
    }

    public void DrawQuads()
    {
        glBegin(GL_QUADS);

        //顶面
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, 1.0f, -1.0f);     //右上顶点
        glVertex3f(-1.0f, 1.0f, -1.0f);    //左上顶点
        glVertex3f(-1.0f, 1.0f, 1.0f);     //左下顶点
        glVertex3f(1.0f, 1.0f, 1.0f);      //右下顶点

        //底面
        glColor3f(1.0f, 0.5f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);     //右上顶点
        glVertex3f(-1.0f, -1.0f, 1.0f);    //左上顶点
        glVertex3f(-1.0f, -1.0f, -1.0f);   //左下顶点
        glVertex3f(1.0f, -1.0f, -1.0f);    //右下顶点

        //前面
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(1.0f, 1.0f, 1.0f);      //右上顶点
        glVertex3f(-1.0f, 1.0f, 1.0f);     //左上顶点
        glVertex3f(-1.0f, -1.0f, 1.0f);    //左下顶点
        glVertex3f(1.0f, -1.0f, 1.0f);     //右下顶点

        //后面
        glColor3f(1.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);    //右上顶点
        glVertex3f(-1.0f, -1.0f, -1.0f);   //左上顶点
        glVertex3f(-1.0f, 1.0f, -1.0f);    //左下顶点
        glVertex3f(1.0f, 1.0f, -1.0f);     //右下顶点

        //左侧面
        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);     //右上顶点
        glVertex3f(-1.0f, 1.0f, -1.0f);    //左上顶点
        glVertex3f(-1.0f, -1.0f, -1.0f);   //左下顶点
        glVertex3f(-1.0f, -1.0f, 1.0f);     //右下顶点

        //右侧面
        glColor3f(1.0f, 0.0f, 1.0f);
        glVertex3f(1.0f, 1.0f, -1.0f);     //右上顶点
        glVertex3f(1.0f, 1.0f, 1.0f);      //左上顶点
        glVertex3f(1.0f, -1.0f, 1.0f);     //左下顶点
        glVertex3f(1.0f, -1.0f, -1.0f);    //右下顶点

        glEnd();
    }
    private void loop() {
        logger.info("loop");

        // This line is critical for LWJGL's interoperation with GLFW's
        // OpenGL context, or any context that is managed externally.
        // LWJGL detects the context that is current in the current thread,
        // creates the GLCapabilities instance and makes the OpenGL
        GL.createCapabilities();
        //DrawQuads();
        //-----------------------------------------
        //glLoadIdentity();   //重置当前的模型观察矩阵

        //————————————————
        //版权声明:本文为CSDN博主「贝勒里恩」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
        //原文链接:https://blog.csdn.net/Mr_robot_strange/article/details/123682686
        while (true)
        {
            glClearColor(1.0f, 1.0f, 0.0f, 0.0f);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除屏幕和深度缓存

            //-----------------------------------------
            glLoadIdentity();   //重置当前的模型观察矩阵

            //DrawLines();
            DrawTriangles();
            glfwSwapBuffers(window);
            glfwPollEvents();
        }

    }

    public static void main(String[] args) {
        logger.info("main");
        new HelloWorld().run();
    }

}
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;

/**
 * 可以自已定义日志打印格式,这样看起来比较方便些
 *
 */
class MyFormatter extends Formatter
{
    @Override
    public String format(LogRecord arg0)
    {
        //创建StringBuilder对象来存放后续需要打印的日志内容
        StringBuilder builder = new StringBuilder();

        //获取时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
        Date now = new Date();
        String dateStr = simpleDateFormat.format(now);

        builder.append("[");
        builder.append(dateStr);
        builder.append(" ");

        //拼接日志级别
        builder.append(arg0.getLevel()).append(" ");

        builder.append(arg0.getSourceClassName()).append(" ");

        //拼接方法名
        builder.append(arg0.getSourceMethodName()).append(" ");

        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        String line = stackTrace[8].toString();
        String lineNumber = line.substring(line.indexOf(":") + 1, line.length() - 1);

        //拼接方法名
        builder.append(lineNumber).append("] ");

        //拼接日志内容
        builder.append(arg0.getMessage());

        //日志换行
        builder.append("\r\n");

        return builder.toString();
    }
}

public class MyLogger {
    static Logger logger;

    static  {
        logger = Logger.getLogger(MyLogger.class.getName());
        logger.setUseParentHandlers(false);
        //如果需要将日志文件写到文件系统中,需要创建一个FileHandler对象
        Handler consoleHandler = new ConsoleHandler();

        //创建日志格式文件:本次采用自定义的Formatter
        consoleHandler.setFormatter(new MyFormatter());

        //将FileHandler对象添加到Logger对象中
        logger.addHandler(consoleHandler);
    }

    public static Logger getLogger() {
        return logger;
    }

    public static void main(String[] args) {
        MyLogger.logger.info("1");
        Logger logger = MyLogger.logger;
        logger.info("2");
    }
}

文章来源:https://blog.csdn.net/r77683962/article/details/135264558
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。