Does everything have to be a polygon in openGL (java)? - java

I am extremely new to JOGL, and I have two main questions about JOGL, the first is about sprites and animations and the next is about textures.
(I am not doing 3d, this is all in the contexts of 2d)
1.) I have been able to create quads, polygons, and so forth in JOGL, but I am very curious, is that the only way to get a type of image that you can manipulate out there? For instance if you want a sprite of a man to appear in JOGL, and when you move the sprite it will move in the direction you make it but also display an animation. Is there a way to do this efficiently in JOGL, or do I have to scalp a shape out of polygons, one vertex at a time, and then paint a texture over it? Or is there a simpler method of just loading the sprite and displaying it right then and there and of course changing the image depending on user input. If there is a way to do it without creating a vertex for every corner of the sprite could you display some code, or a tutorial so I can grasp the idea of what you are doing.
2.) I have been searching around for a way of displaying textures in JOGL, but I am still confused on how to do so. Let me first show you the way I found of loading and display a texture on a shape. First I create 7 variables:
Texture image;
private String textureFileName = "images/crate.png";
private String textureFileType = ".png";
private float textureTop, textureBottom, textureLeft, textureRight;
Next in my init() method I load my image by doing the following:
GL2 gl = drawable.getGL().getGL2();
gl.glShadeModel(GL_SMOOTH);
image = TextureIO.newTexture(getClass().getClassLoader().getResource(textureFileName),false, textureFileType);
// Use linear filter for texture if image is larger than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Use linear filter for texture if image is smaller than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
TextureCoords textureCoords = image.getImageTexCoords();
textureTop = textureCoords.top();
textureBottom = textureCoords.bottom();
textureLeft = textureCoords.left();
textureRight = textureCoords.right();
Then in the display method I do the following
GL2 gl = drawable.getGL().getGL2();
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
image.enable(gl);
image.bind(gl);
gl.glBegin(gl.GL_QUADS);
gl.glTexCoord2f(textureLeft,textureTop);
gl.glVertex2d(c,a); // vertex 1
gl.glTexCoord2f(textureLeft, textureBottom);
gl.glVertex2d(c,b); // vertex 2
gl.glTexCoord2f(textureRight, textureBottom);
gl.glVertex2d(d,b); // vertex 3
gl.glTexCoord2f(textureRight, textureTop);
gl.glVertex2d(d,a); // vertex 4
gl.glEnd();
//Variables a,b,c,d are just doubles with certain values for each point
Now I just wish to know if this is an effective way to load images and display them if I am going to be having multiple textures. Seeing this way I have devised a plan on how to do that using this method but I want to know if this is the best way of doing it. I have see people talk about bmp loaders but hearing of what they do, mapping everything single integer value the image has, seems really complex. Is there no simple way to do it? But if not could someone show me how or point me into a direction of the most correct, and efficient way to load textures and display those textures.
One last thing, when I put the line:
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
Isnt this just basically just clearing out an integers for pixels, etc. in the buffer? But what does the | do anyways? Does it shift the bits?
Also what do these two lines of code do? Im confused by the comments. These two lines appear in my init() method for loading a texture:
// Use linear filter for texture if image is larger than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Use linear filter for texture if image is smaller than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Thank you for taking the time to read and respond to this message, Thank you.
-Dan

Related

How to create a gaussian blur effect in libGDX without shaders

How do you create a gaussian blur effect without any shaders? I tried many shader tutorials but I never got it to work. I also tried this link. https://github.com/mattdesl/lwjgl-basics/wiki/OpenGL-ES-Blurs
But, I couldn't get the screen to be transformed into a pixmap. Can you guys point me in the right direction on where to go to get the right answer?
Thanks in advance!
To draw blur without shaders you would need to draw the original scene to some texture. Then draw this texture NxN times on your main buffer with some blending. Alpha must correspond to the factor that depends on the offset in the NxN matrix. Just as pseudo:
for(int heightDiff=-N; heightDiff<=N; heightDiff++) {
for(int widthDiff=-N; widthDiff<=N; widthDiff++) {
float alpha = 1.0/max(width, height); // Not a correct equation
glColor(1.0, 1.0, 1.0, alpha):
drawFullscreenWithOffset(widthDiff, heightDiff);
}
}
It might be an optimization to separate vertical and horizontal blur though. That would mean you need to choose one of them and draw it to a new texture and then use that one to draw the other blur on the main buffer. You gain an extra texture but you reduce NxN draws to 2*N.

Sprite Cropping LibDGX

I've been trying to draw an image of a block with an eye on the screen. I want to be able to animate more than one texture in the same way, so drawing the animations by hand is not an option. The problem is that when I move the eyelid sprite higher up than completely closed, part of the eyelid shows above the expected bounds like this. Obviously, this is a problem as it looks quite unnatural. I'd like to either have an alternative solution to this problem, or be able to crop the eyelid's Sprite object to fit behind the rest of the image. The final image consists of the eyeball, pupil (as I want to be able to animate this too), eyelid, main body and the outlines, drawn in that order. The render function looks like this:
public void render(float delta) {
float height = this.eyelid.getHeight();
float eyeHeight = height*0.7f;
this.eyelid.setY(this.eye.getY()+(eyeHeight*((100-this.lid)/100f)));
batch.begin();
this.eye.draw(batch);
this.pupil.draw(batch);
this.eyelid.draw(batch);
this.main.draw(batch);
this.shade.draw(batch);
batch.end();
}
this.lid is the % of how closed the eye is, and the image for the eyelid itself can be found here. How could I solve this problem, or how could I crop the sprite? I don't want to have to reload the texture as a sprite every frame.
I think what you're looking for is the ScissorStack class, which is documented on the libGDX wiki. You can use this object to clip around the eye's frame so that the top of the lid doesn't show.

Flipping a Texture in Libgdx Java

I am trying to figure out how to flip a Texture in libgdx using Java. I can't find any methods of doing this and I can't just do some code to flip the Texture itself. Is there any way that I can flip the texture some way in libgdx?
You can make a Sprite object and when you initialize it, you can pass in the Texture object. Then you can call methods such as rotate, flip, etc. Here is a code example
Texture tex = new Texture("path");
Sprite sprite = new Sprite(tex);
sprite.flip(true, false);
true as the first parameter for flipping over x-axis and false for y-axis, which is what you are looking for in your specific example.

Texture Region 2D Array - Why are my sprites drawing so small?

Using the Libgdx Framework, I'm generating an Actor from an Assets.java class and then drawing him depending on the state he is currently in. The problem i'm having is that he's coming out ridiculously small. I'm noticing that when I split the Texture into regions the .Split() function doesn't provide width and height parameters in comparison to a singular declaration of the texture region function here: new TextureRegion(Texture, x, y, width, height);
Note: I've also set the size of the world, the actor size etc they don't need to change how small he is. I'm assuming it's something to do with the sprite size or assets class. But the way i'm slicing the Texture works perfectly for the engine i've created.
I'm wondering is there something I'm missing or something to add to define the width/height of the TextureRegions inside theAssets.java code snippet?
All my sprites are in a 512x512 Texture with a height and width of 85px
Relevant Code:
rubenSprite = loadTexture("data/rubenSprite.png");
rubenFrames = TextureRegion.split(rubenSprite, 85, 85);
WalkF1 = rubenFrames[0][0];
WalkF2 = rubenFrames[0][1];
WalkF3 = rubenFrames[0][2];
WalkF4 = rubenFrames[0][3];
WalkF5 = rubenFrames[0][4];
... More TextureRegions
If more code is needed, or anything elaborated let me know. Thanks.
The size of actor is not size of texture inside. Actor has boundaries. When this boundaries are too low image cannot fit inside. On to the contrary, it is analogously.
You should set your actor as:
setWidth(width);
setHeight(height);
setBounds(left, right, width, height);
And make sure, that texture is also set to width and height.
btw.
I recommend, use drawables with Actors. It is more clearer. You create skin, where you define all your moves and then you call your textures from the skin.

Rendering lighting

I have a small block engine similar to a very early version of Minecraft using LWJGL. I now want to actually implement lighting. I understand how it works I'm just confused as to how I'm supposed to render lighting. Am I supposed to change the "brightness" of the texture to simulate bright terrain? I'm asking how to actually change the light value of a quad, maybe there are some tutorials out there? I want it to be block by block, no smooth lighting. I have figured out that blocks need to have a light value, and for every block next to it, you decrease that light value by a little bit until its "black".
(At least) two options, assuming a fixed-function renderer:
Use a single texture (your basic texture atlas, with the default GL_MODULATE texenv.) and set a per-vertex gray-scale color to darken the texture in proportion to the light level. With GL_MODULATE the texture RGB channels are multiplied by the vertex color RGB channels. So a vertex color of RGB(255,255,255) would be fully lit, RGB(0,0,0) would be pure black, and RGB(128,128,128) would be somewhere in the middle.
Use two textures (appearance atlas and lightmap atlas) and multitexture. The light level is set for a given face by supplying texture coordinates that select the appropriate lightmap square. If you animate the lightmap you can get a day/night cycle "for free" without having to iterate over the entire volume fixing up vertex colors like in #1.

Categories

Resources