尽管更改了宽度和高度,Lwjgl/openGL窗口大小始终保持不变 [英] Lwjgl/openGL window size always stays the same despite changing width and height

查看:95
本文介绍了尽管更改了宽度和高度,Lwjgl/openGL窗口大小始终保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,即使我更改宽度和高度,如果窗口始终保持相同大小,则该大小也将保持不变.我认为问题出在视口上,因为窗口中的可见区域正确更改,但是窗口部分始终有黑色区域未被使用.我可能已经对此做了非常糟糕的解释,但是任何帮助将不胜感激.

For some reason the size if the window always stays the same size even when i change the width and the height. I think the problem is something to the viewport as the viewable area in the window changes correctly but there's always a black area in the part of the window that is not used. I've probably expalained this really badly but any help would be much appreciated.

我在下面放置了窗口的屏幕截图.

I've put a screenshot of the window below..

public class DisplayExample
{

    private int     width           = 800;
    private int     height          = 600;
    private int     colourDepth     = 32;
    private String  windowTitle     = "My First Window";
    public boolean  closeRequested  = false;
    private boolean fullscreen      = false;

    private float rtri = 0.0f;
    private float rquad = 0.0f;

    public void start()
    {

        createWindow(windowTitle, width, height, colourDepth, fullscreen);
        initGL();

        while (!closeRequested)
        {
            pollInput();
            updateLogic();
            renderGL();

            Display.update(); // flushes OpenGL pipeline and swaps back and
                                // front buffers. perhaps waits for v-sync.
        }

        cleanUp();
    }

    /**
     * Initialise OpenGL
     */
    private void initGL()
    {
        GL11.glViewport(0, 0, width, height);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f,
                100.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();

        GL11.glShadeModel(GL11.GL_SMOOTH); // Smooth shading
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black background
        GL11.glClearDepth(1.0f);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthFunc(GL11.GL_LEQUAL);
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    }

    /**
     * Update Logic
     */
    private void updateLogic()
    {

    }

    /**
     * Render OpenGL
     */
    private void renderGL()
    {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear
                                                                            // The
                                                                            // Screen
                                                                            // And
                                                                            // The
                                                                            // Depth
                                                                            // Buffer
        GL11.glLoadIdentity(); // Reset The View

        GL11.glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 Units And Into
                                                // The Screen 6.0
        GL11.glRotatef(rtri, 0.0f, 1.0f, 0.0f);
        GL11.glBegin(GL11.GL_TRIANGLES); // Drawing Using Triangles

        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right

        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
        GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right

        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right

        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
        GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right




        GL11.glEnd(); // Finished Drawing The Triangle


        GL11.glLoadIdentity(); // Reset The View

        GL11.glTranslatef(1.5f, 0.0f, -2.5f); // Move Right 3 Units

        GL11.glRotatef(rquad, 1.0f, 1.0f, 1.0f);

        GL11.glBegin(GL11.GL_QUADS); // Draw A Quad

        //TOP
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
        GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left
        GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right
        GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left

        //FRONT
        GL11.glColor3f(1.0f, 0.5f, 0.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left
        GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right
        GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left

        //LEFT
        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left

        //RIGHT
        GL11.glColor3f(1.0f, 1.0f, 0.0f);
        GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left
        GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right
        GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right
        GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left

        //BACK
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left
        GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right
        GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left

        //BOTTOM
        GL11.glColor3f(1.0f, 0.0f, 1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Top Left
        GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Top Right
        GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left




        GL11.glEnd(); // Done Drawing The Quad

        rtri += 0.5f;
        rquad += -0.5f;
    }

    /**
     * Poll Input
     */
    public void pollInput()
    {

        // scroll through key events
        while (Keyboard.next())
        {
            if (Keyboard.getEventKeyState())
            {
                if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE)
                    closeRequested = true;
                else if (Keyboard.getEventKey() == Keyboard.KEY_F1)
                {
                        fullscreen = !fullscreen;
                    System.out.println(fullscreen);
                    setDisplayMode(width, height, fullscreen);
                }
            }

        }

        if (Display.isCloseRequested())
        {
            closeRequested = true;
        }
    }

    private void createWindow(String title, int width, int height,
            int colourDepth, boolean fullscreeen)
    {
        try
        {
            DisplayMode[] modes = Display.getAvailableDisplayModes();

            for (int i = 0; i < modes.length; i++)
            {

                DisplayMode current = modes[i];

                if (current.isFullscreenCapable())
                {
                    Display.setDisplayMode(current);
                }
                else
                    Display.setDisplayMode(new DisplayMode(width, height));
            }
            Display.setFullscreen(fullscreeen);
            Display.setTitle(title);
            Display.setVSyncEnabled(true);
            Display.create();
        }
        catch (LWJGLException e)
        {
            Sys.alert("Error", "Initialisation failed!\n\n" + e.getMessage());
        }
    }

    private void cleanUp()
    {
        Display.destroy();
    }

    public void setDisplayMode(int width, int height, boolean fullscreen)
    {

        // return if requested DisplayMode is already set
        if ((Display.getDisplayMode().getWidth() == width)
                && (Display.getDisplayMode().getHeight() == height)
                && (Display.isFullscreen() == fullscreen))
        {
            return;
        }

        try
        {
            DisplayMode targetDisplayMode = null;

            if (fullscreen)
            {
                DisplayMode[] modes = Display.getAvailableDisplayModes();
                int freq = 0;

                for (int i = 0; i < modes.length; i++)
                {
                    DisplayMode current = modes[i];

                    if ((current.getWidth() == width)
                            && (current.getHeight() == height))
                    {
                        if ((targetDisplayMode == null)
                                || (current.getFrequency() >= freq))
                        {
                            if ((targetDisplayMode == null)
                                    || (current.getBitsPerPixel() > targetDisplayMode
                                            .getBitsPerPixel()))
                            {
                                targetDisplayMode = current;
                                freq = targetDisplayMode.getFrequency();
                            }
                        }

                        // if we've found a match for bpp and frequence against
                        // the
                        // original display mode then it's probably best to go
                        // for this one
                        // since it's most likely compatible with the monitor
                        if ((current.getBitsPerPixel() == Display
                                .getDesktopDisplayMode().getBitsPerPixel())
                                && (current.getFrequency() == Display
                                        .getDesktopDisplayMode().getFrequency()))
                        {
                            targetDisplayMode = current;
                            break;
                        }
                    }
                }
            }
            else
            {
                targetDisplayMode = new DisplayMode(width, height);
            }

            if (targetDisplayMode == null)
            {
                System.out.println("Failed to find value mode: " + width + "x"
                        + height + " fs=" + fullscreen);
                return;
            }

            Display.setDisplayMode(targetDisplayMode);
            Display.setFullscreen(fullscreen);
        }
        catch (LWJGLException e)
        {
            System.out.println("Unable to setup mode " + width + "x" + height
                    + " fullscreen=" + fullscreen + e);
        }
    }

    public static void main(String[] argv)
    {
        DisplayExample displayExample = new DisplayExample();
        displayExample.start();
    }
}

推荐答案

setDisplayMode仅以初始大小创建窗口.但是,当调整大小时,必须将更改应用于OpenGL.

The setDisplayMode only creates the window at an initial size. But when it gets resized the change must be applied to OpenGL.

我总是建议您忘记一次性OpenGL初始化"的想法.恕我直言,最佳做法是设置直接影响绘图操作的所有状态,并且视口和投影确实属于该状态,在绘图代码中为您在InitGL中拥有的整个代码应位于renderGL的开头. widthheight是要由窗口大小调整处理程序设置或通过查询窗口状态确定的变量.

I always recommend to forget about the idea of a "one time OpenGL initialization". IMHO the best practice is to set all state that directly affects drawing operations, and viewport and projection do belong to that state, in the drawing code! The whole code you have in InitGL should be at the beginning of renderGL. width and height are variables to be set by the window resizing handler, or determined by querying window state.

这篇关于尽管更改了宽度和高度,Lwjgl/openGL窗口大小始终保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆