I'm creating shapes with extrude geometry on GWT, now I want to apply textures on it but I am not able to apply it directly. How can I do that?
My code looks like :
ExtrudeGeometry extrudeMaterialBoard = null;
shapeExtrude.getExtrudeSettings().amount = (int) getMaterialThickness();
shapeExtrude.getExtrudeSettings().curveSegments = 1;
shapeExtrude.getExtrudeSettings().bevelThickness= 5;
shapeExtrude.getExtrudeSettings().bevelSize= 5;
shapeExtrude.getExtrudeSettings().bevelEnabled= true;
shapeExtrude.getExtrudeSettings().material = 0;
shapeExtrude.getExtrudeSettings().extrudeMaterial = 1;
extrudeMaterialBoard = new ExtrudeGeometry(DrawShape.getBottomBoard(), shapeExtrude.getExtrudeSettings());
camera = new PerspectiveCamera(
70, // fov
getRenderer().getAbsoluteAspectRation(), // aspect
1, // near
1000 // far
);
camera.getPosition().setZ(400);
String texture1 = "uvgrid0.jpg";
MeshBasicMaterial material = new MeshBasicMaterial();
material.setColor(getColor(COLOR_TRANSPARENT));
Texture texture = new Texture(texture1);
material.setMap(texture);
mesh = new Mesh(extrudeMaterialBoard, material);
getScene().add(mesh);
getRenderer().render(getScene(), camera);
If I use cube geometry then its running properly. But in case of extrude geometry its not working.
Thanks in advance.
Related
I have a PDF viewer that doesn't show highlights if they do not have an appearance stream. I'm trying out iText 7 core in java to try and add highlight annotations to PDFs but these annotations do not have appearance streams, and thus I'm looking to try and add them myself when writing the annotations to the PDF file.
I've come across this old answer, but it's for C# and iText 5, and I can't seem to figure out how to replicate it in iText 7 with a successful result.
So my question is thus: how do you set appearance streams on the highlighting annotations in iText 7 core that are working?
The furthest I've gotten with the code is shown below. I'm using the RegexBasedLocationExtrationStrategy class to find the locations of all search words in the pdf.
RegexBasedLocationExtractionStrategy evntlstnr = new RegexBasedLocationExtractionStrategy(pattern);
for (int pIdx = 0; pIdx < pdfDoc.getNumberOfPages(); ++pIdx) {
final PdfPage page = pdfDoc.getPage(pIdx + 1);
new PdfCanvasProcessor(evntlstnr).processPageContent(page);
Collection<IPdfTextLocation> locations = evntlstnr.getResultantLocations();
for (IPdfTextLocation location : locations) {
Rectangle rect = location.getRectangle();
// Specify quad points in Z-like order
// [0,1] x1,y1 [2,3] x2,y2
// [4,5] x3,y3 [6,7] x4,y4
float[] quads = new float[8];
quads[0] = rect.getX();
quads[1] = rect.getY() + rect.getHeight();
quads[2] = rect.getX() + rect.getWidth();
quads[3] = quads[1];
quads[4] = quads[0];
quads[5] = rect.getY();
quads[6] = quads[2];
quads[7] = quads[5];
Color highlightColor = new DeviceRgb(0f, 0f, 1f);
PdfTextMarkupAnnotation highlight = PdfTextMarkupAnnotation.createHighLight(rect, quads);
highlight.setColor(highlightColor);
Rectangle appearRect = new Rectangle(0f, 0f, rect.getWidth(), rect.getHeight());
PdfFormXObject appearObj = new PdfFormXObject(appearRect);
final PdfResources appearRes = appearObj.getResources();
PdfExtGState extGState = new PdfExtGState();
extGState.setBlendMode(PdfExtGState.BM_MULTIPLY);
appearRes.addExtGState(extGState);
appearObj.setBBox(new PdfArray(new float[] {0f, 0f, rect.getWidth(), rect.getHeight()}));
PdfShading appearShading = new PdfShading.Axial(highlightColor.getColorSpace(), 0f, 0f, highlightColor.getColorValue(), 1f, 1f, highlightColor.getColorValue());
appearRes.addShading(appearShading);
appearRes.addColorSpace(highlightColor.getColorSpace());
PdfAnnotationAppearance appearance = new PdfAnnotationAppearance(appearObj.getPdfObject());
highlight.setNormalAppearance(appearance);
highlight.setFlag(PdfAnnotation.PRINT);
page.addAnnotation(highlight);
}
}
Using Samuel's answer I stumbled my way to a working answer.
I'm no expect in the PDF standard and this framework (iText), but my hypothesis, based on my working example below, is that the rectangle I was trying to write for the highlight is a crude fall-back method for "faking" the highlight rectangle when the viewer cannot render the annotations (since they have no appearance stream). Realizing this that the two operations are not linked, I came to the working example shown below. Hope this helps others in the future.
RegexBasedLocationExtractionStrategy evntlstnr = new RegexBasedLocationExtractionStrategy(pattern);
for (int pIdx = 0; pIdx < pdfDoc.getNumberOfPages(); ++pIdx) {
final PdfPage page = pdfDoc.getPage(pIdx + 1);
new PdfCanvasProcessor(evntlstnr).processPageContent(page);
Collection<IPdfTextLocation> locations = evntlstnr.getResultantLocations();
for (IPdfTextLocation location : locations) {
Rectangle rect = location.getRectangle();
// Specify quad points in Z-like order
// [0,1] x1,y1 [2,3] x2,y2
// [4,5] x3,y3 [6,7] x4,y4
float[] quads = new float[8];
quads[0] = rect.getX();
quads[1] = rect.getY() + rect.getHeight();
quads[2] = rect.getX() + rect.getWidth();
quads[3] = quads[1];
quads[4] = quads[0];
quads[5] = rect.getY();
quads[6] = quads[2];
quads[7] = quads[5];
Color highlightColor = new DeviceRgb(1f, 1f, 0f);
PdfTextMarkupAnnotation highlight = PdfTextMarkupAnnotation.createHighLight(rect, quads);
highlight.setColor(highlightColor);
highlight.setFlag(PdfAnnotation.PRINT);
page.addAnnotation(highlight);
PdfCanvas canvas = new PdfCanvas(page);
PdfExtGState extGState = new PdfExtGState();
extGState.setBlendMode(PdfExtGState.BM_MULTIPLY);
canvas.setExtGState(extGState);
canvas.rectangle(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
canvas.setFillColor(highlightColor);
canvas.fill();
canvas.release();
}
}
I have a method reads coordinates from a file and uses them to render a GameObject at the specified coordinates writen on the file, this code looks like this:
public void ProcessText()
{
String file_name = "C:/Users/Server/Desktop/textText.txt";
try
{
ProcessCoords file = new ProcessCoords(file_name);
String[] aryLines = file.OpenFile();
int i;
for (i = 0; i < aryLines.length; i++)
{
System.out.println(aryLines[i]);
if(aryLines[i].startsWith("makeGrass:")) {
String Arguments = aryLines[i].substring(aryLines[i].indexOf(":")+1, aryLines[i].length());
String[] ArgArray = Arguments.substring(1, Arguments.length() - 2).split(" ");
this.makeGrass(Double.parseDouble(ArgArray[0]),
Double.parseDouble(ArgArray[1]),
Double.parseDouble(ArgArray[2]));
}
}
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
and my text file would only have one line:
makeGrass:(x y z) // for example makeGrass:(1.22334 0.0 9.66678)
this all works fine now.. but there's no point in making this for only one single object. What I want to be able to do is have as many objects as I want from multiple coordinates, so my text file might look like this:
makeGrass:(0.0 1.0 5.0)
makeGrass:(8.0 1.0 2.0)
makeGrass:(4.0 1.0 7.0)
makeGrass:(0.0 1.0 2.0)
makeGrass:(2.0 1.0 7.0)
makeGrass:(5.0 1.0 6.0)
at the moment I use the code this.makeGrass only once and like that my grass model is only placed at those coordinates of the first line (the other lines cause a graphical glitch where they are just being put ontop of each other)
My question here is how I have to write the code so it gives me this.makeGrass multiple times: I want to render the grass model at the respective makeGrass coordinates as many times as there are makeGrass coordinates provided in the text file, how can I do that?
Thanks for any help!
edit: I was asked for more information.. here it is:
public class Vegetation extends GameComponent
{
private Game game;
GameObject grassLeaf1 = new GameObject();
Mesh grassLeaf1mesh = new Mesh("grassLeaf1.obj");
Material grassLeaf1material = new Material
(new Texture("GrassUVTex.png"), 1, 8, new Texture("GrassUVTex_NRM.jpg"), new Texture("GrassUVTex_DISP.jpg"), 0.008f, -0.5f);
MeshRenderer grassLeaf1Renderer = new MeshRenderer(grassLeaf1mesh, grassLeaf1material);
public Vegetation(Game game)
{
this.game = game;
}
public void makeGrass(double posX, double posY, double posZ)
{
grassLeaf1.AddComponent(grassLeaf1Renderer);
grassLeaf1.GetTransform().GetPos().Set((float)posX, (float)posY, (float)posZ);
grassLeaf1.GetTransform().SetScale(new Vector3f (2, 2, 2));
grassLeaf1.GetTransform().SetRot(new Quaternion(new Vector3f(0, 1, 0), (float) Math.toRadians(0)));
game.AddObject(grassLeaf1);
}
public void ProcessText()
{
grassLeaf1.AddComponent(new SaveGrass());
grassLeaf1.AddComponent(new ObjectManipulator(4.0f));
String file_name = "C:/Users/Server/Desktop/textText.txt";
try
{
ProcessCoords file = new ProcessCoords(file_name);
String[] aryLines = file.OpenFile();
int i;
for (i = 0; i < aryLines.length; i++)
{
System.out.println(aryLines[i]);
if(aryLines[i].startsWith("makeGrass:")) {
String Arguments = aryLines[i].substring(aryLines[i].indexOf(":")+1, aryLines[i].length());
String[] ArgArray = Arguments.substring(1, Arguments.length() - 2).split(" ");
this.makeGrass(Double.parseDouble(ArgArray[0]),
Double.parseDouble(ArgArray[1]),
Double.parseDouble(ArgArray[2]));
}
}
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
The Vegetation Class handles creating a grass model and reading the coordinates of it from a text file. This here is said text file:
makeGrass:(0.6 1.0 2.8)
makeGrass:(5.6 1.0 9.8)
makeGrass:(2.6 1.0 4.8)
makeGrass:(7.6 1.0 3.8)
makeGrass:(0.6 1.0 2.8)
makeGrass:(0.6 1.0 4.8)
makeGrass:(2.6 1.0 2.8)
makeGrass:(0.6 1.0 0.8)
Now this should enable me to render 8 grass models at different positions but when I hit run all I can see is this:
http://www.pic-upload.de/view-27752444/Untitled.png.html
the grass is also super shiny because all other grass models are being placed also at this location.. something went wrong..
Okay I've found the mistake my make grass function was messed up, it needs to be like this:
public void makeGrass(double posX, double posY, double posZ)
{
GameObject grassLeaf1 = new GameObject();
Mesh grassLeaf1mesh = new Mesh("grassLeaf1.obj");
Material grassLeaf1material = new Material
(new Texture("GrassUVTex.png"), 1, 8, new Texture("GrassUVTex_NRM.jpg"), new Texture("GrassUVTex_DISP.jpg"), 0.008f, -0.5f);
MeshRenderer grassLeaf1Renderer = new MeshRenderer(grassLeaf1mesh, grassLeaf1material);
grassLeaf1.AddComponent(grassLeaf1Renderer);
grassLeaf1.GetTransform().GetPos().Set((float)posX, (float)posY, (float)posZ);
grassLeaf1.GetTransform().SetScale(new Vector3f (2, 2, 2));
grassLeaf1.GetTransform().SetRot(new Quaternion(new Vector3f(0, 1, 0), (float) Math.toRadians(0)));
game.AddObject(grassLeaf1);
}
only strange that once I've added the code into the makeGrass method I lost my input methods.. strange but not that of a problem for now..
I'm getting almost exactly what I wanted now.. here's the final result and the basis of my world editor. Thank you all very much, you've really helped me!
Here's my ShowOff picture: http://www.pic-upload.de/view-27752521/ShowOff.png.html
I'm sure I'll be back for more soon enough but at the moment I know everything and can extend this world editor on my own, thanks a lot to all who have helped me so far, hopefully I can return the favour some time!
I am creating a car game and I want to rotate my car ( Image ) when I'm using the car but I don't know how. I've tried a few things that don't work. Here is what I have:
g2d.drawImage(DEFAULT_CAR_SKIN, DEFAULT_BOX.x, DEFAULT_BOX.y, null);
DEFAULT_BOX.setLocation(DEFAULT_BOX.x + 1, DEFAULT_BOX.y);
if (Game.rotateFront == true) {
AffineTransform identity = new AffineTransform();
AffineTransform trans = new AffineTransform();
trans.setTransform(identity);
trans.rotate(Math.toRadians(Game.Angle));
g2d.drawImage(DEFAULT_CAR_SKIN, trans, this);
}
But this code draws a new image but with the characteristics that I want.
I have my image (for the sake of explaining, lets call it Image.png) and I'm trying to get it to scale to properly fit in my Bodies I have drawn. The bodies and everything work with the debug but I'm not a fan of it.. I'm using a SpriteBatch to draw the background image onto the screen(it would be nice to scale that too). How would I scaleImage.png to the same size/position as a Dynamic body that has been rendered?
EDIT:
While drawing the images, I cant get them to match up with the debug render Bodies..
Creating Body:
BodyDef bodydef2 = new BodyDef();
bodydef2.type = BodyType.DynamicBody;
bodydef2.position.set(camera.viewportWidth + 30.0f, (int)(Math.random()*camera.viewportHeight)%(LastPlatformY+5)+20);
//System.out.println(bodydef2.position.y + " " + LastPlatformY);
Body block = world.createBody(bodydef2);
PolygonShape Shape2 = new PolygonShape();
Shape2.setAsBox(750*WORLD_TO_BOX, 200*WORLD_TO_BOX);
FixtureDef fixtureDef2 = new FixtureDef();
fixtureDef2.shape = Shape2;
fixtureDef2.density = 1000.0f;
fixtureDef2.friction = 0.0f;
fixtureDef2.restitution = 0;
block.setLinearVelocity(new Vector2((float) (Platform_Velocity*-1), 0));
block.createFixture(fixtureDef2);
//Lock 'block' to X-Axis, Relative to floor.
PrismaticJointDef jointDef = new PrismaticJointDef();
jointDef.collideConnected = true;
jointDef.initialize(block, groundBody, block.getWorldCenter(), new Vector2(1, 0));
world.createJoint(jointDef);
Platforms.add(block);
Platforms_Created++;
LastPlatformY = (int)bodydef2.position.y;
Drawing Image:
sp.draw(platforms, (float)(Platforms.get(i).getPosition().x), (float)(Platforms.get(i).getPosition().y), 75,20);
EDIT #2:
turns out that if your camera is smaller than the size of your screen you have to do some compensation to account for that variation in positions.. Problem solved, thanks!
Just save the size of your body in two variables while creating your body and then call the appropriate draw method.
To get the position of your object call body.getPosition().
For example
Texture t = assetManager.get("your texture", Texture.class);
BodyDef bodyDef = new BodyDef();
body.type = BodyType.DynamicBody
float width = 64 * WORLD_TO_BOX;
float height = 64 * WORLD_TO_BOX;
Shape shape = new PolygonShape();
float[] points = new float[]{0,0,width,0,width,height,0, height};
shape.set(points);
FixtureDef def = new FixtureDef();
def.shape = shape;
...
body.createFixture(def);
shape.dispose();
And later when you want to draw
public void render(Spritebatch batch) {
batch.draw(t, body.getPosition().x * BOX_TO_WORLD, body.getPosition().y * BOX_TO_WORLD, width * BOX_TO_WORLD, height * BOX_TO_WORLD);
}
I have been trying to figure out a way to center text on a button, but can't find an easy, multi-purpose way to. I can do it, but it will only work for a certain string, not for any string. i would like to know if there is a way to center any string on a button. My button in this case is 185x50.
I have been able to center this button on the screen, like so:
buttonX = WIDTH / 2 - (screen.getRegionWidth() / 2);
buttonY = HEIGHT / 2 - (screen.getRegionHeight() / 2);
Any help would be much appreciated. :)
Updated the answer to libgdx version 1.7.1-SNAPSHOT:
The easiest way to do this, is to use the TextButton class from libgdx. The text from a TextButton is centered by default. This still works!
Updated example:
final BitmapFont font = new BitmapFont();
final String text = "Test";
final GlyphLayout layout = new GlyphLayout(font, text);
// or for non final texts: layout.setText(font, text);
final float fontX = objectX + (objectWidth - layout.width) / 2;
final float fontY = objectY + (objectHeight + layout.height) / 2;
font.draw(batch, layout, fontX, fontY);
Outdated example:
This no longer works!
If you do not want to use it, you can get the font width and height with:
font.getBounds("Test text");
So you can do something like this:
String fontText = "";
fontX = buttonX + buttonWidth/2 - font.getBounds(fontText).width/2;
fontY = buttonY + buttonHeight/2 + font.getBounds(fontText).height/2;
For the newer version of libgdx the function BitMapFont.getBounds() isn't there in api anymore. You can use GlyphLayout to get bounds.For example,
BitmapFont font;
SpriteBatch spriteBatch;
//... Load any font of your choice first
FreeTypeFontGenerator fontGenerator = new FreeTypeFontGenerator(
Gdx.files.internal("myFont.ttf")
);
FreeTypeFontGenerator.FreeTypeFontParameter freeTypeFontParameter =
new FreeTypeFontGenerator.FreeTypeFontParameter();
freeTypeFontParameter.size = size;
fontGenerator.generateData(freeTypeFontParameter);
font = fontGenerator.generateFont(freeTypeFontParameter);
//Initialize the spriteBatch as requirement
GlyphLayout glyphLayout = new GlyphLayout();
String item = "Example";
glyphLayout.setText(font,item);
float w = glyphLayout.width;
font.draw(spriteBatch, glyphLayout, (Game.SCREEN_WIDTH - w)/2, y);
Try the following:
label.setPosition(Gdx.graphics.getWidth()/2-(label.getPrefWidth()/2),Gdx.graphics.getHeight()-(label.getPrefHeight()/2));