Keep whitespaces when auto-formatting Java code in IntelliJ - java

I have a small issue with IntelliJ not making my code more readable through auto-formatting, but more confusing.
Hence the following code excerpt:
private static float _triangleCoords[] = {
0.0f, 0.62f, 0f,
-0.5f, -0.31f, 0f,
0.5f, -0.31f, 0f
};
If I run Java auto-formatting, IntelliJ formats it like this:
private static float _triangleCoords[] = {
0.0f, 0.62f, 0f,
-0.5f, -0.31f, 0f,
0.5f, -0.31f, 0f
};
I don't want it to strip away the spaces since they align the floats nicely. I also don't want to write + signs in front of positive floats just to keep it that way.
How can I turn this formatting off?

You can disable reformatting for blocks of codes with comments, eg
private static float _triangleCoords[] = {
// #formatter:off
0.0f, 0.62f, 0f,
-0.5f, -0.31f, 0f,
0.5f, -0.31f, 0f
// #formatter:on
};
Go to Preferences -> Code Style -> General -> Formatter Control to enable format control with comments. You can also define a macro/template to surround a block of code with formatter comments to make life a bit easier.

Related

Vuforia + OBJ loader

I am looking to use Vuforia for an AR project.
I understand that it utilises OpenGL ES and therefore typically uses arrays of vertex data to construct the object.
I want ot be able to use a standard OBJ file instead.
I already looked at using this but have no clue how to use a *.h file in the Android Java SDK.
So, I started looking at loading in an OBJ. I want to try and use something like this but just use the obj reader. But I'm stuck on how to get the vertex array.
Does anyone have knowledge on how to load an OBJ and pass it into the Vuforia Sample application?
(it needs to work in the Java SDK, not the NDK)
.obj and openGL aren't directly compatible, openGL requires only 1 index for the vertices (all attributes get the same index) while .obj allows different indices for each attribute.
What that script does is pre-process the obj file so you can include it directly in the code without needing to (slowly) parse at runtime,
The equivalent java code of the .h file is:
package resource.models;
public class ModelName{
public int NumVerts = //exactly the number that is <name>NumVerts in the header
public float[] Verts = {
//copy paste the numbers and append f after each number like so:
// f 1//2 7//2 5//2
-0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
// f 1//2 3//2 7//2
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
//and so on
}
//repeat for Normals and textures:
public float[] Normals = {
// f 1//2 7//2 5//2
0f, 0f, -1f,
0f, 0f, -1f,
0f, 0f, -1f,
// f 1//2 3//2 7//2
0f, 0f, -1f,
0f, 0f, -1f,
0f, 0f, -1f,
//and so on
}
}
To fill the openGL buffers you wrap the float arrays in FloatBuffers and then pass them to the BufferData calls.
Changing the script in that article to be compatible with java requires changing the code from line 492 to emit the package and class declaration and append an f after each number plus the trailing closing brace.

glTranslate error in simple JOGL program

I am trying to create a simple JOGL program, and have run into a problem. I just imported all of the necessary packages into my class file, however now, every time I use the glTranslate function, it is being flagged red as an error, for example at the end of this block of code.
public void lab2a(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glTranslatef(-1.0f, -1.0f, -6f);
// Drawing first rectangle (blue)
gl.glBegin(GL.GL_QUADS);
gl.glColor3f(0f, 0f, 1f); // sets color to blue
gl.glVertex3f(-0.5f, 0.5f, 0.0f); //Top left vertice
gl.glVertex3f(0.5f, 0.5f, 0.0f); //Top right vertice
gl.glVertex3f(-0.5f, -0.5f, 0.0f); //Bottom left vertice
gl.glVertex3f(0.5f, -0.5f, 0.0f); //Bottom right vertice
gl.glEnd();
gl.glTranslate(1.1f, 0f, 0f);
The flag reads: "cannot find symbol", and is present for each use of glTranslate I use. Does anybody have any idea how to fix this?
Your source code is obsolete, it uses JOGL 1 whose maintenance was stopped in 2010.
Please switch to JOGL 2, go to jogamp.org. Replace GL.GL_QUADS by GL2.GL_QUADS, replace GL gl = drawable.getGL() by GL2 gl = drawable.getGL().getGL2(), etc... Look at the API documentation here.

JOGL - lighting/camera

Ive added a light source to my JOGL project which seems to work quite well when the object is stationary, when I move the camera it gradually gets darker as it rotates, which is what id expect but as soon as it rotates 90 degree the screen goes completely black, does anyone know why this is? Do I need to another light source for the other side? I was hoping it would kind of act like the sun, i.e. light up the whole scene but be slightly darker when the camera is on the other side of the object.
Lighting
float light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, light_ambient, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
gl.glDepthFunc(GL.GL_LESS);
gl.glEnable(GL.GL_DEPTH_TEST);
Secondly, when the camera rotates some of the shapes seem to deform and look like completely different shapes, i.e. cubes turning pincushion like, sides being stretched an incredible amount and its making my whole object look slightly deformed. Is there an easy way to change this? Ive tried messing with gluPerspective and that doesnt seem to do change what I want either. Is there any way around this?
You have added diffuse and specular light to your scene, but these will not reach surfaces that are facing away from the light source. You could add some ambient light (currently set to 0, 0, 0 in your code snippet) so that all surfaces receive some illumination.
As for the deformed shapes, that is really a separate question, and there is not enough detail given to know why this is happening.

Java openGL - drawing a 3D object

Im looking to draw a 3D object in openGL but whats the best way to approach this? I was thinking of drawing the side profile of it in 2D and maybe then fleshing it out to become 3D but is that possible? I think it would be easier to do it that way then just go straight into 3D but if you cant then id just be wasting my time.
I also cant figure out how to add the sky and maybe even the sea with a reflection, is this easily done?
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glPushMatrix();
gl.glTranslatef(-1.0f, 0.0f, 0.0f);
gl.glRotatef((float) shoulder, 0.0f, 0.0f, 1.0f);
gl.glTranslatef(1.0f, 0.0f, 0.0f);
// gl.glPushMatrix();
gl.glScalef(2.0f, 0.4f, 1.0f);
glut.glutWireCube(1.0f);
// gl.glPopMatrix();
gl.glTranslatef(1.0f, 0.0f, 0.0f);
gl.glRotatef((float) elbow, 0.0f, 0.0f, 1.0f);
gl.glTranslatef(1.0f, 0.0f, 0.0f);
// gl.glPushMatrix();
gl.glScalef(2.0f, 0.4f, 1.0f);
glut.glutWireCube(1.0f);
// gl.glPopMatrix();
gl.glTranslatef(1.0f, 1.0f, 1.0f);
gl.glRotatef((float) hand, 0.0f, 0.0f, 1.0f);
gl.glTranslatef(1.0f, 0.0f, 0.0f);
// gl.glPushMatrix();
gl.glScalef(2.0f, 0.4f, 1.0f);
glut.glutWireCube(1.0f);
// gl.glPopMatrix();
Ive just been trying random numbers to try and get it to work but not such luck so far!
Ok first, reflections can be hard depending on how you want to do them. You will definitely need to learn more OpenGL before attempting something like that. Second, 3D objects require some more matrix stuff, this is an example from my init method in my 3D game:
private void initGl() {
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluPerspective(45.0f, Display.getWidth() / Display.getHeight(), 1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_EXP2);
glFogf(GL_FOG_DENSITY, density);
glHint(GL_FOG_DENSITY, GL_FASTEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
}
You will also need to clear the buffer like this before drawing:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
You are almost correct when you say just flesh out a 2D object. If we were still using immediate mode (glBegin()/glEnd()), your approach would be correct. However, immediate mode is deprecated now and we usually use VBOs. I would suggest going on YouTube and searching for theCodingUniverse if you are using LWJGL, he has a video on VBOs and advanced rendering, its how I learned them!
Good luck in the world of 3D, its not easy at all, but its (in my opinion), much more satisfying than 2D when you get something working.
Also, consider investing in the RedBook, its all about LWJGL.

How do I blur an image?

I'm trying to implement a blurring mechanic on a java game. How do I create a blur effect on runtime?
Google "Gaussian Blur", try this: http://www.jhlabs.com/ip/blurring.html
Read about/Google "Convolution Filters", it's a method of changing a pixels value based on the values of pixels around it. So apart from blurring, you can also do image sharpening and line-finding.
If you are doing java game development, I'm willing to bet you are using java2d.
You want to create a convolution filter like so:
// Create the kernel.
kernel = new KernelJAI
float[] = { 0.0F, -1.0F, 0.0F,
-1.0F, 5.0F, -1.0F,
0.0F, -1.0F, 0.0F };
// Create the convolve operation.
blurredImage = JAI.create("convolve", originalImage, kernel);
You can find more information at: http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Image-enhance.doc.html#51172 (which is where the code is from too)

Categories

Resources