2d texture from image

Have jReality programming problems or questions? Post them here.
Post Reply
kadirardic
Posts: 3
Joined: Thu 3. Dec 2009, 16:12

2d texture from image

Post by kadirardic » Thu 3. Dec 2009, 16:33

Hi everyone,

I want to apply a 2d texture (which will be created from some image) to a cube, i created an example like cubeunwrapped.java in tutorials section, but i can't apply the texture to the cube properly. i want to show some image for each face of the cube, i can't understand the setVertexTextureCoordinates method what coordinates i must pass to this function to set an image texture for each face. below is the part of my code which i tried to apply texture to the cube.

final double [][] vertices = new double[][]
{
{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0},
{0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1}
};

final int [][] indices = new int [][] {
{ 0, 1, 2, 3 }, { 7, 6, 5, 4 }, { 0, 1, 5, 4 },
{ 1, 2, 6, 5 }, { 2, 3, 7, 6 }, { 3, 0, 4, 7 }
};

final double [][] TextureCoordinates = new double[][] {
{ .25, .5},{ .5, .5},{ .5, .75},{ .25, .75},
{ .25, .5},{ .5, .5},{ .5, .75},{ .25, .75}
};

final IndexedFaceSetFactory ifsf = new IndexedFaceSetFactory();

ifsf.setVertexCount( 8 );

ifsf.setVertexCoordinates(vertices);

ifsf.setVertexTextureCoordinates(TextureCoordinates);

ifsf.setFaceCount( 6 );
ifsf.setFaceIndices( indices );

ifsf.setGenerateVertexNormals( true );
ifsf.setGenerateEdgesFromFaces( true );

sceneCompParent.setGeometry(ifsf.getGeometry());

Texture2D tex =TextureUtility.createTexture(sceneCompParent.getAppearance(),
"polygonShader",
ImageData.load(Input.getInput("C:/some.jpg")),
false);
tex.setTextureMatrix(MatrixBuilder.euclidean().scale(8).getMatrix());

Thanks for any help,
Kadir Ardıç

User avatar
gunn
Posts: 323
Joined: Thu 14. Dec 2006, 09:56
Location: TU Berlin
Contact:

Re: 2d texture from image

Post by gunn » Sat 5. Dec 2009, 21:22

Hello Kadir,

It's not possible to attach different textures to the faces of a single IndexedFaceSet, so you'll have to create different IndexedFaceSets for each face. Also, be sure to call update() on the IndexedFaceSetFactory before getting the attached geometry. I've revised your code to do what I think you want to do.

I've also set the texture coordinates to be the "default" ones: they map to the corners of the provided texture image.

Code: Select all

import java.awt.Color;

import de.jreality.math.MatrixBuilder;
import de.jreality.scene.IndexedFaceSet;
import de.jreality.scene.SceneGraphComponent;
import de.jreality.shader.Texture2D;
import de.jreality.shader.TextureUtility;
import de.jreality.tutorial.util.SimpleTextureFactory;
import de.jreality.tutorial.util.SimpleTextureFactory.TextureType;
import de.jreality.util.SceneGraphUtility;
   ...
		final double[][] vertices = new double[][] { { 0, 0, 0 }, { 1, 0, 0 },
				{ 1, 1, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 1, 0, 1 },
				{ 1, 1, 1 }, { 0, 1, 1 } };

		final int[][] indices = new int[][] { { 0, 1, 2, 3 }, { 7, 6, 5, 4 },
				{ 0, 1, 5, 4 }, { 1, 2, 6, 5 }, { 2, 3, 7, 6 }, { 3, 0, 4, 7 } };

		final double[][] TextureCoordinates = new double[][] { { 0,0},
				{ 1,0 }, { 1,1 }, {0,1 }};

		SceneGraphComponent sceneCompParent = SceneGraphUtility.createFullSceneGraphComponent();
		//		sceneCompParent.setGeometry(ifsf.getGeometry());
		sceneCompParent.getAppearance().setAttribute("diffuseColor", Color.white);
		TextureType[] tts = {TextureType.ANTI_DISK, TextureType.GRAPH_PAPER,
				TextureType.WEAVE,TextureType.GRAPH_PAPER, TextureType.DISK, TextureType.GRADIENT
		};
		for (int i = 0; i<6; ++i)	{
			IndexedFaceSetFactory ifsf = new IndexedFaceSetFactory();
	
			ifsf.setVertexCount(4);
	
			double[][] verts = {vertices[indices[i][0]],
					vertices[indices[i][1]],
					vertices[indices[i][2]],
					vertices[indices[i][3]]
			};
			ifsf.setVertexCoordinates(verts);
	
			ifsf.setVertexTextureCoordinates(TextureCoordinates);
	
			ifsf.setFaceCount(1);
			ifsf.setFaceIndices(new int[][]{{0,1,2,3}});
	
			ifsf.setGenerateVertexNormals(true);
			ifsf.setGenerateEdgesFromFaces(true);
	
			ifsf.update();
			
			IndexedFaceSet face = ifsf.getIndexedFaceSet();
			SceneGraphComponent child = SceneGraphUtility.createFullSceneGraphComponent("child"+i);
			sceneCompParent.addChild(child);
			child.setGeometry(face);
			SimpleTextureFactory stf = new SimpleTextureFactory();
			stf.setType(tts[i]);
			stf.update();
			Texture2D tex = TextureUtility.createTexture(child
					.getAppearance(), "polygonShader", stf.getImageData(), false);
			tex.setTextureMatrix(MatrixBuilder.euclidean().scale(i+2).getMatrix());
		}
   ...
jReality core developer

kadirardic
Posts: 3
Joined: Thu 3. Dec 2009, 16:12

Re: 2d texture from image

Post by kadirardic » Mon 7. Dec 2009, 10:26

Hello gunn,

thanks for the example code, that is exactly what i want and works great, thanks for your help.

Kadir Ardıç

Post Reply