Frage

For some reason the texture I load will not display/render. I've gotten a texture to render on previous projects with almost identical code, but for some reason it will not render with the below code. I've posted the init() and createGreenFields() methods, the display method calls gl.glCallList(1) to create the field.

public void init(GLAutoDrawable drawable) {
    // Initialize object state.

    glu = new GLU();
    GL2 gl = drawable.getGL().getGL2();
    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glDepthFunc(GL.GL_LEQUAL);
    gl.glShadeModel(GL2.GL_SMOOTH);

    try {
        grassTex = TextureIO.newTexture(new File(grassFile), true);
        grassTex.setTexParameteri(gl, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
        grassTex.setTexParameteri(gl, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
    } catch (Exception e) {
        System.out.println("Error loading texture " + e);
    }
    gl.glClearDepth(1.0f);

    createGreenFields(gl);
}

.
.
.

private void createGreenFields(GL2 gl) {
    int index = gl.glGenLists(1);
    gl.glNewList(index, GL2.GL_COMPILE);
    gl.glBegin(GL2.GL_QUADS); // Four vertices
    .
    .
    // do not draw the transparent parts of the texture
    gl.glEnable(GL.GL_BLEND);
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
    // don't show source alpha parts in the destination

    // determine which areas of the polygon are to be rendered
    gl.glEnable(GL2.GL_ALPHA_TEST);
    gl.glAlphaFunc(GL.GL_GREATER, 0); // only render if alpha > 0

    gl.glDisable(GL.GL_BLEND);
    // enable texturing
    //gl.glEnable(GL2.GL_TEXTURE_3D);
    grassTex.enable(gl);
    grassTex.bind(gl);

    // replace the quad colors with the texture
    gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);

    TextureCoords tc = grassTex.getImageTexCoords();

    // lower right field
    gl.glColor3f(0.0f, 0.3f, 0.0f); // Dark Green

    gl.glTexCoord2f(tc.left(), tc.bottom());
    gl.glVertex3f(-1.66f, 15.0f, 0f);

    gl.glTexCoord2f(tc.right(), tc.bottom());
    gl.glVertex3f(-15.0f, 15.0f, 0f);

    gl.glTexCoord2f(tc.right(), tc.top());
    gl.glVertex3f(-15.0f, 1.66f, 0f);

    gl.glTexCoord2f(tc.left(), tc.top());
    gl.glVertex3f(-1.66f, 1.66f, 0f);
    .
    .

public void display(GLAutoDrawable drawable) {
    .
    .
    gl.glCallList(1); // Creates Green Fields
    .
    .
}
War es hilfreich?

Lösung

The problem is happening because of the way you call these two OpenGL functions.

glBegin(GL_QUADS);

... etc ...

glEnd();

After call glBegin() you can only use the following OpenGL functions, until you call glEnd()

  • glVertex()
  • glColor()
  • glSecondaryColor()
  • glIndex()
  • glNormal()
  • glFogCoord()
  • glTexCoord()
  • glMultiTexCoord()
  • glVertexAttrib()
  • glEvalCoord()
  • glEvalPoint()
  • glArrayElement()
  • glMaterial()
  • glEdgeFlag()

It's also acceptable to use.

  • glCallList()
  • glCallLists()

So as we can see in your code you call glEnable(), glDisable(), glBlendFunc(), glAlphaFunc() and bind the texture, etc. all these things you need to do them before you call glBegin()

Extra

Also you're using deprecated OpenGL functions in case you don't know. You should learn about and use VBOs and VAOs and also learn to use the modern functions and ways of doing things in OpenGL.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top