Grid texturing problems

Hello!

I have some problems in texturing simple grids using com.sun.opengl.util.texture library.
I wrote simple program to illustrate the problem:


package gridtexturingtest;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import javax.media.opengl.DebugGL;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class TestGrid implements GLEventListener {

    private boolean newTexture;
    private boolean flushTexture;
    private File file;
    private Texture texture;
    private GLU glu = new GLU();
    private double[] texpts = {0, 1, 0, 0, 1, 1, 1, 0};
    private int gridSize = 2;
    private int xGridPos = 1;
    private int yGridPos = 0;
    private double surfaceCtrlPoints[];

    public static void main(String[] args) {
        new TestGrid().run(args);
    }

    private void run(String[] args) {

        recalculateSurfaceCtrlPoints();
        JFrame frame = new JFrame("Texture Loader Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final GLCanvas canvas = new GLCanvas();
        canvas.addGLEventListener(this);
        frame.getContentPane().add(canvas);
        frame.setSize(400, 400);
        frame.setVisible(true);
        setTextureFile(new File(args[0]));
    }

    public TestGrid(javax.media.opengl.GLCanvas canvas) {
        canvas.addGLEventListener(this);
    }

    public TestGrid() {
    }
    
    public void setTextureFile(File file) {
        this.file = file;
        newTexture = true;
    }

    public void flushTexture() {
        flushTexture = true;
    }

    public void init(GLAutoDrawable drawable) {
        drawable.setGL(new DebugGL(drawable.getGL()));

        GL gl = drawable.getGL();

        gl.glEnable(GL.GL_AUTO_NORMAL);
        gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

        gl.glDepthFunc(GL.GL_LEQUAL);
        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glEnable(GL.GL_NORMALIZE);

        gl.glShadeModel(GL.GL_SMOOTH);
        gl.glClearDepth(1.0f);
        gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);

        gl.glDisable(GL.GL_LIGHTING);
        gl.glEnable(GL.GL_DEPTH_TEST);

    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        setCamera(drawable);
        //glu.gluOrtho2D(0, 1, 0, 1);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        if (flushTexture) {
            flushTexture = false;
            if (texture != null) {
                texture.dispose();
                texture = null;
            }
        }

        if (newTexture) {
            newTexture = false;

            if (texture != null) {
                texture.dispose();
                texture = null;
            }

            try {
                System.err.println("Loading texture...");
                texture = TextureIO.newTexture(file, false);
            } catch (IOException e) {
                e.printStackTrace();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                e.printStackTrace(new PrintStream(bos));
                JOptionPane.showMessageDialog(null,
                        bos.toString(),
                        "Error loading texture",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
        }

        if (texture != null) {
            gl.glMatrixMode(GL.GL_PROJECTION);
            gl.glLoadIdentity();
            setCamera(drawable);

            gl.glMatrixMode(GL.GL_MODELVIEW);
            gl.glLoadIdentity();

            gl.glEnable(GL.GL_TEXTURE_2D);
            gl.glEnable(GL.GL_MAP2_VERTEX_3);
            gl.glEnable(GL.GL_MAP2_TEXTURE_COORD_2);

            gl.glMap2d(GL.GL_MAP2_VERTEX_3,
                    0, gridSize,
                    3,
                    2,
                    0, gridSize,
                    2 * 3,
                    2,
                    surfaceCtrlPoints,
                    0);

            texture.bind();

            gl.glMap2d(GL.GL_MAP2_TEXTURE_COORD_2,
                    0, 1,
                    2,
                    2,
                    0, 1,
                    4,
                    2,
                    texpts,
                    0);

            gl.glMapGrid2d(
                    1,
                    yGridPos, yGridPos + 1,
                    1,
                    xGridPos, xGridPos + 1);

            gl.glEvalMesh2(
                    GL.GL_FILL,
                    0, 1,
                    0, 1);

            gl.glDisable(GL.GL_TEXTURE_2D);
            gl.glMapGrid2d(
                    gridSize, 0.0f, gridSize,
                    gridSize, 0.0f, gridSize);
            gl.glColor3f(1.0f, 0.0f, 0.0f);
            gl.glEvalMesh2(GL.GL_LINE,
                    0, gridSize,
                    0, gridSize);
            gl.glColor3f(1.0f, 1.0f, 1.0f);
            gl.glEnable(GL.GL_TEXTURE_2D);

            gl.glDisable(GL.GL_MAP2_TEXTURE_COORD_2);
            gl.glDisable(GL.GL_MAP2_VERTEX_3);
            gl.glDisable(GL.GL_TEXTURE_2D);
            gl.glFlush();
        }
    }

    public void setCamera(GLAutoDrawable gLDrawable) {

        GL gl = gLDrawable.getGL();

        int zoom = 2;
        double bottom = 0 - zoom;
        double top = 1 + zoom;
        double left = 0 - zoom;
        double right = 1 + zoom;

        gl.glOrtho(
                left,
                right,
                bottom,
                top,
                0, 1000);
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }

    public void recalculateSurfaceCtrlPoints() {
        System.out.println("recalculateSurfaceCtrlPoints");
        surfaceCtrlPoints = new double[]{
            0, 0, 0,
            0, gridSize, 0,
            gridSize, 0, 0,
            gridSize, gridSize, 0
        };
        for (int i = 0; i < surfaceCtrlPoints.length; i++) {
            if (i % 3 == 0) {
                surfaceCtrlPoints[i] -= xGridPos;
            }
            if ((i + 2) % 3 == 0) {
                surfaceCtrlPoints[i] -= yGridPos;
            }
        }
        for (int i = 0; i < this.surfaceCtrlPoints.length; i++) {
            System.out.print(this.surfaceCtrlPoints[i] + ", ");
            if ((i + 1) % 3 == 0) {
                System.out.println();
            }
        }
    }

    public int getGridSize() {
        return gridSize;
    }

    public void setGridSize(int gridSize) {
//        System.out.println("gridSize=" + gridSize);
        this.gridSize = gridSize;
    }

    public int getXGridPos() {
        return xGridPos;
    }

    public void setXGridPos(int xGridPos) {
//        System.out.println("xGridPos=" + xGridPos);
        this.xGridPos = xGridPos;
    }

    public int getYGridPos() {
        return yGridPos;
    }

    public void setYGridPos(int yGridPos) {
//        System.out.println("yGridPos=" + yGridPos);
        this.yGridPos = yGridPos;
    }
}

This program is very simple: changing values of xGridPos and yGridPos variables changes place of texture on the grid.
It takes one parameter witch is the path to texture image.
My question is: why the texture is black when xGridPos != 0 and yGridPos != 0??
I use JOGL 1.1.1 rc1 on Windows XP platform

Greetings and a HAPPY NEW YEAR!
Latek

If you are using a texture with non-power-of-two dimensions, you may be running into the difference between texture coordinates that go from (0,0)-(1,1) and those which go from (0,0)-(xSize,ySize). Look into Texture.getImageTexCoords().

Thank you for reply Ken.

I use power-of-two texture (256x256px) and it’s not working. I’ve also tried it on JSR-231 1.1.0 release and I have no idea why it’s not working.
Method texture.getImageTexCoords() return values:
bottom=1.0
top=0.0
left=0.0
right=1.0

Do you have any ideas? Did you run my sample program?

Sorry, no, I am swamped at work and can’t run your sample program. Maybe someone else can offer more concrete advice.

Do you have to use mapping? Perhaps it’s a driver problem that doesn’t fully support that. You could try specifying the grid with more standard glVertex/glTexCoord commands just to simplify your test case.

Thank you for reply lhkbob. Unfortunately I have to use mapping in my application and I work on Nvidia GeForce 6200. My drivers supports OpenGL 2.1.1 (info from http://mr.myogl.org).

I’ve modified my sample program and I get it to work. I’ve replaced


                texture = TextureIO.newTexture(file, false);

by


                TextureData td = TextureIO.newTextureData(file, false, FileUtil.getFileSuffix(file));
                int target = GL.GL_TEXTURE_2D;
                texture = TextureIO.newTexture(target);
                texture.bind();
                gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
                gl.glTexParameteri(target, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
                gl.glTexParameteri(target, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
                gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
                gl.glTexImage2D(target, 0, td.getInternalFormat(),
                        td.getWidth(), td.getHeight(), td.getBorder(),
                        td.getPixelFormat(), td.getPixelType(), td.getBuffer());

I found that the key function in this problem is Texture.updateSubImageImpl(), because it is used in standard call of TextureIO.newTexture(File file, boolean useMipMap) function.
I still don’t understand why the simple call of TextureIO.newTexture(File file, boolean useMipMap) is not working properly with texture mapping?
Does the authors of com.sun.opengl.util.texture library did not take it into consideration?

Greetings
Latek