简体   繁体   中英

OpenGL - Super basic rendering

I am starting to create GUI for my 3D OpenGL game and for that I use the fixed - pipeline functionality.

This is what I am trying to do:

Enable 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();
   }

Render the quad:

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();

Disable 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);
   }

This doesn't render the QUAD. Yet the weird part is that I am drawing text as well using Slick2D font rendering and it works perfectly. Just this quad doesn't get rendered.

Why is this happening?

EDIT: Here is my GL Init code, see if you can maybe spot something that would cause this behaviour:

    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);

What I ended up doing is use VBOs to render. Didn't take too long to implement and the result was awesome. I use a VBO per GUI Element type. I truly recommend not to use GL11 even for this basic type of stuff. Just stick with the 'advanced methods'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM