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