OpenGL - 超级基本渲染

时间:2014-08-24 23:20:56

标签: java opengl 3d 2d lwjgl

我开始为我的3D OpenGL游戏创建GUI,为此我使用了固定的管道功能。

这就是我想要做的事情:

启用2d:

public void Enable2D() {
      //GL11.glDisable(GL11.GL_LIGHTING);
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glPushMatrix();
      GL11.glLoadIdentity();
      GL11.glOrtho(0, Display.getDisplayMode().getWidth(),
                Display.getDisplayMode().getHeight(), 0, -1, 1);
      GL11.glDisable(GL11.GL_DEPTH_TEST);
      //GL11.glDisable(GL11.GL_CULL_FACE);
      GL11.glEnable(GL11.GL_TEXTURE_2D);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
      GL11.glPushMatrix();
      GL11.glLoadIdentity();
   }

渲染四边形:

GL11.glColor3f(0.5f,0.5f,1.0f);
    // draw quad
    GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2f(boundingBox.getLocation().x,boundingBox.getLocation().y);
        GL11.glVertex2f(boundingBox.getLocation().x+boundingBox.getSize().x,boundingBox.getLocation().y);
        GL11.glVertex2f(boundingBox.getLocation().x+boundingBox.getSize().x,boundingBox.getLocation().y+boundingBox.getSize().y);
        GL11.glVertex2f(boundingBox.getLocation().x,boundingBox.getLocation().y+boundingBox.getSize().y);
    GL11.glEnd();

禁用2d:

  public void Disable2D() {
     // GL11.glEnable(GL11.GL_LIGHTING);
      GL11.glEnable(GL11.GL_DEPTH_TEST);
      GL11.glDisable(GL11.GL_TEXTURE_2D);
      //GL11.glEnable(GL11.GL_CULL_FACE);
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glPopMatrix();
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
      GL11.glPopMatrix();
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
   }

这不会渲染QUAD。然而奇怪的部分是我使用Slick2D字体渲染绘制文本并且它完美地工作。只是这个四边形不会被渲染。

为什么会这样?

编辑:这是我的GL Init代码,看看你是否可以找到会导致这种行为的东西:

    GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GLU.gluPerspective(FIELD_OF_VIEW,
            ((float) Display.getWidth() / (float) Display.getHeight()),
            NEAR_DISTANCE, FAR_DISTANCE);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
    GL11.glShadeModel(GL11.GL_SMOOTH);
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);

    // FUNCTIONS
    GL11.glClearColor(204f / 255f, 1f, 248f / 255f, 0.0f); // Blueish
                                                            // Background
    GL11.glClearDepth(1.0f); // Depth Buffer Setup
    GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
    GL11.glDepthMask(true);
    GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Test To Do
    GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST); // Really Nice
    // Perspective Calculations
    GL11.glEnable(GL11.GL_CULL_FACE);
    GL11.glCullFace(GL11.GL_BACK);
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    GL11.glEnable(GL11.GL_LIGHTING);
    // LIGHTING
    float lightAmbient[] = { 0.7f, 0.7f, 0.7f, 1.0f }; // Ambient Light
                                                        // Values
    float lightDiffuse[] = { 1.3f, 1.3f, 1.3f, 1.0f }; // Diffuse Light
                                                        // Values
    float lightPosition[] = { 35f, 500f, 35f, 1.0f }; // Diffuse Light
                                                        // Values

    ByteBuffer temp = ByteBuffer.allocateDirect(16);
    temp.order(ByteOrder.nativeOrder());
    GL11.glEnable(GL11.GL_LIGHT0);
    GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, (FloatBuffer) temp
            .asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient
                                                        // Light
    GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, (FloatBuffer) temp
            .asFloatBuffer().put(lightDiffuse).flip()); // Setup The Diffuse
                                                        // Light
    GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, (FloatBuffer) temp
            .asFloatBuffer().put(lightPosition).flip()); // Setup The
                                                            // Diffuse Light

    // calculate the lighting for both front and back faces correctly
    //GL11.glLightModeli(GL11.GL_LIGHT_MODEL_TWO_SIDE, GL11.GL_TRUE);

    GL11.glEnable(GL11.GL_COLOR_MATERIAL);
    GL11.glColorMaterial(GL11.GL_FRONT_AND_BACK,
            GL11.GL_AMBIENT_AND_DIFFUSE);


    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glEnable(GL11.GL_NORMALIZE);

1 个答案:

答案 0 :(得分:0)

我最终做的是使用VBO进行渲染。没有花太长时间来实施,结果很棒。我按GUI元素类型使用VBO。我真的建议不要将GL11用于这种基本类型的东西。坚持使用'先进方法'。