How can I draw Triangles that are independent from each other? I wat to make a ship game and when the ship collides with the triangle I want it to disapear. But I can't find a good way to do this. Creating Mesh isn't the best solution because they end hardcoded, Using a shaperenderer doesn't seems to be good either because I can't control them separately. So I'm stuck, anyone has any idea?
I ended up solving my problem this way:
I created a object Triangle that created a Mesh. In that object I had another problem, the bind wasn't working, but I figured out how to solve it, I'm not sure how I solved it, but it works. Here's the final code that worked for me:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
public class Triangle {
ShaderProgram shader;
Mesh mesh;
Texture texture;
float[] attributes = new float[15];
static int screenWidth;
static int screenHeight;
public Triangle(float[] vertices, float[] color, float[] textureVertices, Texture texture){
this.texture = texture;
createShader();
int j = 0;
int k = 0;
int l = 0;
if ((screenWidth <= 0) || (screenHeight <= 0))
{
throw new NullPointerException("Invalid screen dimensions for triangles.");
}
for (int i = 0; i < vertices.length; i++) {
vertices[i] = (vertices[i]/screenWidth - 1); // (vertices[i] - width/2)/(width/2)
vertices[++i] = (vertices[i]/screenHeight - 1); // (vertices[i] - height/2)/(height/2)
}
for (int i = 0; i < attributes.length;) {
attributes[i++] = vertices[j++];
attributes[i++] = vertices[j++];
attributes[i++] = color[k++];
attributes[i++] = textureVertices[l++];
attributes[i++] = textureVertices[l++];
}
mesh = new Mesh(false, attributes.length, 0, new VertexAttribute(
Usage.Position, 2, "a_position"), new VertexAttribute(
Usage.ColorPacked, 4, "a_color"), new VertexAttribute(
Usage.TextureCoordinates, 2, "a_texCoords"));
mesh.setVertices(attributes);
}
public static void setDimensions(int paramWidth, int paramHeight)
{
screenWidth = paramWidth;
screenHeight = paramHeight;
}
public void createShader()
{
// this shader tells opengl where to put things
String vertexShader = "attribute vec4 a_position; \n"
+ "attribute vec4 a_color; \n"
+ "attribute vec2 a_texCoords; \n"
+ "varying vec4 v_color; \n"
+ "varying vec2 v_texCoords; \n"
+ "void main() \n"
+ "{ \n"
+ " v_color = a_color; \n"
+ " v_texCoords = a_texCoords; \n"
+ " gl_Position = a_position; \n"
+ "} \n";
// this one tells it what goes in between the points (i.e
// colour/texture)
String fragmentShader = "#ifdef GL_ES \n"
+ "precision mediump float; \n"
+ "#endif \n"
+ "varying vec4 v_color; \n"
+ "varying vec2 v_texCoords; \n"
+ "uniform sampler2D u_texture;\n"
+ "void main() \n"
+ "{ \n"
+ " gl_FragColor = v_color * texture2D(u_texture, v_texCoords); \n"
+ "} \n";
shader = new ShaderProgram(vertexShader, fragmentShader);
}
public void render() {
Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
Gdx.gl20.glEnable(GL20.GL_TEXTURE_2D);
Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE);
shader.begin();
texture.bind(0);
shader.setUniformi("u_texture", 0);
mesh.render(shader, GL20.GL_TRIANGLES);
shader.end();
}
public void dispose() {
texture.dispose();
mesh.dispose();
shader.dispose();
}
}
I hope it helps someone!
Related
Suppose I have a Texture and I want to split it in half on a line rotated with a given angle.
In result I expect to get two Texture objects with the corresponding halfs of the original texture. Would be very nice if it could be done with some kind of transformation/masking so that I wouldn't have to store copies of two new textures at runtime.
Is this even possible?
I tried to work around Gdx.gl.glSsisors() but couldn't make it work as it requires screen coords to be passed as parameters.
Also I tried to go with Pixmap but couldn't find anything that would indicate it's even possible.
This can be achieved by manually calculating two skewed quads and render those using a SpriteBatch.
Cutting a sprite along angle angle we get a directional vector representing the cut by
public void cut(float angle) {
Vector2 d = (new Vector2(1.0f, 0.0f)).rotate(angle);
if we also define the four corners for the UV Mapping of the Texture, along with the center and one vector la going d away from the center and lb going -d
Vector2 c = new Vector2(0.5f, 0.5f);
Vector2 la = (new Vector2(d)).scl( 1.0f).add(c);
Vector2 lb = (new Vector2(d)).scl(-1.0f).add(c);
Vector2 tl = new Vector2(0, 1);
Vector2 tr = new Vector2(1, 1);
Vector2 bl = new Vector2(0, 0);
Vector2 br = new Vector2(1, 0);
we can then calculate the intersection of the cut
Vector2 i1 = new Vector2();
Vector2 i2 = new Vector2();
if (Intersector.intersectSegments(c, la, tl, tr, i1) || Intersector.intersectSegments(c, lb, tl, tr, i1))
i2.set(1.0f - i1.x, 1.0f - i1.y);
else {
if (Intersector.intersectSegments(c, la, tl, bl, i1) || Intersector.intersectSegments(c, lb, tl, bl, i1))
i2.set(1.0f - i1.x, 1.0f - i1.y);
}
At this point we know that one half of the cut will consist of vertices i1, i2 and two of tl, tr, bl and br, so if we sort them by angle away from the cut and then take the first 4 from i1 we get the vertices required to build the skewed quad:
Vector2[] vertexList = new Vector2[] {
tl, tr, bl, br, i1, i2
};
Array<VertexAngle> vas = new Array<>();
for (Vector2 v : vertexList) {
Vector2 vd = (new Vector2(v)).sub(c);
float a = d.angle(vd);
VertexAngle va = new VertexAngle();
va.v = v;
va.a = a;
vas.add(va);
}
vas.sort(new Comparator<VertexAngle>() {
#Override
public int compare(VertexAngle a, VertexAngle b) {
return Float.compare(a.a, b.a);
}
});
Array<Vector2> nv = new Array<>();
for (VertexAngle va : vas)
nv.add(va.v);
int index = nv.indexOf(i1, true);
The a float array containing the vertex data for the draw call can be constructed and rendered with a call to draw on SpriteBatch.
For example:
Full source code for the above example is:
package com.bornander.sandbox;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import java.util.Comparator;
public class SandboxGame extends ApplicationAdapter {
OrthographicCamera camera;
SpriteBatch batch;
Texture texture;
CutTexture cutTexture;
public static class CutTexture
{
public static class VertexAngle {
public Vector2 v;
public float a;
}
public static class CutHalf {
public float[] vertices = new float[4 * 5];
public void translate(float x, float y) {
for(int i = 0; i < vertices.length; i += 5) {
vertices[i + 0] += x;
vertices[i + 1] += y;
}
}
}
public Vector2 position = new Vector2();
public Vector2 driftDirection = new Vector2();
public float drift = 0.0f;
public Texture source;
public CutHalf halfA = new CutHalf();
public CutHalf halfB = new CutHalf();
public void cut(float angle) {
Vector2 d = (new Vector2(1.0f, 0.0f)).rotate(angle);
Vector2 c = new Vector2(0.5f, 0.5f);
driftDirection.set(d).rotate(90.0f);
Vector2 la = (new Vector2(d)).scl( 1.0f).add(c);
Vector2 lb = (new Vector2(d)).scl(-1.0f).add(c);
Vector2 tl = new Vector2(0, 1);
Vector2 tr = new Vector2(1, 1);
Vector2 bl = new Vector2(0, 0);
Vector2 br = new Vector2(1, 0);
Vector2 i1 = new Vector2();
Vector2 i2 = new Vector2();
if (Intersector.intersectSegments(c, la, tl, tr, i1) || Intersector.intersectSegments(c, lb, tl, tr, i1))
i2.set(1.0f - i1.x, 1.0f - i1.y);
else {
if (Intersector.intersectSegments(c, la, tl, bl, i1) || Intersector.intersectSegments(c, lb, tl, bl, i1))
i2.set(1.0f - i1.x, 1.0f - i1.y);
}
Vector2[] vertexList = new Vector2[] {
tl, tr, bl, br, i1, i2
};
Array<VertexAngle> vas = new Array<>();
for (Vector2 v : vertexList) {
Vector2 vd = (new Vector2(v)).sub(c);
float a = d.angle(vd);
VertexAngle va = new VertexAngle();
va.v = v;
va.a = a;
vas.add(va);
}
vas.sort(new Comparator<VertexAngle>() {
#Override
public int compare(VertexAngle a, VertexAngle b) {
return Float.compare(a.a, b.a);
}
});
Array<Vector2> nv = new Array<>();
for (VertexAngle va : vas)
nv.add(va.v);
int index = nv.indexOf(i1, true);
int idx = 0;
int lastIndex = 0;
for(int j = 0; j < 4; ++j) {
lastIndex = (index + j) % nv.size;
Vector2 vertex = nv.get(lastIndex);
float width = source.getWidth();
float height = source.getWidth();
float fx2 = position.x + width * vertex.x - width / 2.0f;
float fy2 = position.y + height * vertex.y - height / 2.0f;
halfA.vertices[idx++] = fx2;
halfA.vertices[idx++] = fy2;
halfA.vertices[idx++] = Color.WHITE_FLOAT_BITS;
halfA.vertices[idx++] = vertex.x;
halfA.vertices[idx++] = 1.0f - vertex.y;
}
idx = 0;
for(int j = 0; j < 4; ++j) {
Vector2 vertex = nv.get((lastIndex + j) % nv.size);
float width = source.getWidth();
float height = source.getWidth();
float fx2 = position.x + width * vertex.x - width / 2.0f;
float fy2 = position.y + height * vertex.y - height / 2.0f;
halfB.vertices[idx++] = fx2;
halfB.vertices[idx++] = fy2;
halfB.vertices[idx++] = Color.WHITE_FLOAT_BITS;
halfB.vertices[idx++] = vertex.x;
halfB.vertices[idx++] = 1.0f - vertex.y;
}
}
public void render(SpriteBatch batch) {
float dx = driftDirection.x * drift;
float dy = driftDirection.y * drift;
halfA.translate(dx, dy);
halfB.translate(-dx, -dy);
batch.draw(source, halfA.vertices, 0, 20);
batch.draw(source, halfB.vertices, 0, 20);
halfA.translate(-dx, -dy);
halfB.translate(dx, dy);
}
}
#Override
public void create () {
float aspectRatio = (float)Gdx.graphics.getHeight()/(float)Gdx.graphics.getWidth();
camera = new OrthographicCamera(800, 800 * aspectRatio);
camera.position.set(camera.viewportWidth / 2.0f, camera.viewportHeight / 2.0f, 0.0f);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("badlogic.jpg"));
Gdx.gl.glCullFace(0);
cutTexture = new CutTexture();
cutTexture.position.set(camera.viewportWidth / 2.0f, camera.viewportHeight / 2.0f);
cutTexture.source = texture;
cutTexture.cut(0);
}
float[] cutAngles = new float[] { 0.0f, -22.5f, -45.0f, -12.0f, -75.0f, -90.0f};
int ai = 0;
#Override
public void render () {
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE))
{
cutTexture.drift = 0.0f;
cutTexture.cut(cutAngles[(ai++) % cutAngles.length]);
}
cutTexture.drift -= 64.0f * Gdx.graphics.getDeltaTime();
Gdx.gl.glClearColor(0.6f, 0.6f, 1.0f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glCullFace(GL20.GL_NONE);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
cutTexture.render(batch);
batch.end();
}
}
I have a shader program which will make a border line depends on alpha value arround each pixels. I hope to add a yellow border line along an image just like this:
However, it does not give me the expected answer. The thing that I cant understand the most is why there will always be a border line at one boundary side of image.
My fragment shader codes:
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform vec2 u_imageSize;
uniform vec4 u_borderColor;
uniform float u_borderSize;
void main() {
vec4 color = texture2D(u_texture, v_texCoords);
vec2 pixelToTextureCoords = 1. / u_imageSize;
bool isInteriorPoint = true;
bool isExteriorPoint = true;
for (float dx = -u_borderSize; dx < u_borderSize; dx++)
{
for (float dy = -u_borderSize; dy < u_borderSize; dy++){
vec2 point = v_texCoords + vec2(dx,dy) * pixelToTextureCoords;
float alpha = texture2D(u_texture, point).a;
if ( alpha < 0.5 )
isInteriorPoint = false;
if ( alpha > 0.5 )
isExteriorPoint = false;
}
}
if (!isInteriorPoint && !isExteriorPoint && color.a < 0.5)
gl_FragColor = u_borderColor;
else
gl_FragColor = v_color * color;
}
My vertex shader codes:
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
varying vec4 v_color;
varying vec2 v_texCoords;
uniform mat4 u_projTrans;
uniform vec2 u_viewportInverse;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position;
}
My definition codes:
shaderProgram.setUniformf( "u_imageSize", new Vector2(getWidth(), getHeight()) );
shaderProgram.setUniformf( "u_borderColor", Color.YELLOW );
shaderProgram.setUniformf( "u_borderSize", 1 );
Outcome image(shape above is without shader and shape below is with shader):
Please provide me any kind of guides.
The issue is caused, because the texture coordinates in the loop become < 0.0 respectively > 1.0. So the texture is looked up "out of bounds". What happens in this case depends on the wrap parameters (see glTexParameter. Add a range check to the loop and skip the lookup when coordinates are not in range [0.0, 1.0], to solve the issue:
for (float dx = -u_borderSize; dx < u_borderSize; dx++)
{
for (float dy = -u_borderSize; dy < u_borderSize; dy++){
vec2 point = v_texCoords + vec2(dx,dy) * pixelToTextureCoords;
// range check
if (point.x < 0.0 || point.x > 1.0 || point.y < 0.0 || point.y > 1.0)
continue;
float alpha = texture2D(u_texture, point).a;
if ( alpha < 0.5 )
isInteriorPoint = false;
if ( alpha > 0.5 )
isExteriorPoint = false;
}
}
I am trying to draw a texture on a triangle mesh. I get the mesh but not the picture on it. See on the picture below. As you can see that's a plain triangle but no texture in it.
String vertexShader = "attribute vec4 a_position; \n" +
"attribute vec4 a_color;\n" +
"attribute vec2 a_texCoord0;\n" +
"uniform mat4 u_worldView;\n" +
"varying vec4 v_color;" +
"varying vec2 v_texCoords;" +
"void main() \n" +
"{ \n" +
" v_color = vec4(1, 1, 1, 1); \n" +
" v_texCoords = a_texCoord0; \n" +
" gl_Position = u_worldView * a_position; \n" +
"} \n" ;
String fragmentShader = "#ifdef GL_ES\n" +
"precision mediump float;\n" +
"#endif\n" +
"varying vec4 v_color;\n" +
"varying vec2 v_texCoords;\n" +
"uniform sampler2D u_texture;\n" +
"void main() \n" +
"{ \n" +
" gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" +
"}";
shader = new ShaderProgram(vertexShader, fragmentShader);
if (shader.isCompiled() == false) {
Gdx.app.log("ShaderError", shader.getLog());
System.exit(0);
}
mesh = new Mesh(true, 3, 3,
new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
new VertexAttribute(Usage.Color, 4, ShaderProgram.COLOR_ATTRIBUTE),
new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));
mesh.setVertices(new float[] { -0.5f, -0.5f, 0, 0.2f, 0.3f, 0.4f, 1f, 0, 1,
0.5f, -0.5f, 0, 0.1f, 0.2f, 0.1f, 1f, 1, 1,
0, 0.5f, 0, 0, 0.4f, 0.5f, 0.5f, 1f, 0 });
mesh.setIndices(new short[] { 0, 1, 2 });
texture = new Texture(Gdx.files.internal("data/caveman.png"));
And in my render method:
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glEnable(GL20.GL_TEXTURE_2D);
texture.bind();
shader.begin();
shader.setUniformMatrix("u_worldView", viewport.getCamera().combined);
shader.setUniformi("u_texture", 0);
mesh.render(shader, GL20.GL_TRIANGLES);
shader.end();
}
Ok I fixed it by changing my shader to
String vertexShader =
"attribute vec4 a_position;\n" +
"attribute vec4 a_color;\n" +
"attribute vec2 a_texCoord;\n" +
"uniform mat4 u_worldView;\n" +
"varying vec4 v_color;\n" +
"varying vec2 v_texCoords;\n" +
"void main()\n" +
"{\n" +
" v_color = vec4(1, 1, 1, 1);\n" +
" v_texCoords = a_texCoord;\n" +
" gl_Position = u_worldView * a_position;\n" +
"}\n" ;
String fragmentShader =
"#ifdef GL_ES\n" +
"precision mediump float;\n" +
"#endif\n" +
"varying vec4 v_color;\n" +
"varying vec2 v_texCoords;\n" +
"uniform sampler2D u_texture;\n" +
"void main()\n" +
"{\n" +
"vec4 texColor = texture2D(u_texture, v_texCoords);\n" +
" gl_FragColor = texColor;\n" +
"}";
Now it draws textures inside the mesh.
I use FBO to render an image pass blur shader and the fps go down to 20.why? It was ok when I blured it through x-axis direction directly without FBO. It was also ok when I just use FBO to restore an image and draw it to sceen. Is there any thing I shoud pay attention to? Any help would be great! Thanks.
Here is my code:
private static final int FB_SIZE = 200;
public SpriteBatch batch;
protected ShaderProgram mShaderA;
protected ShaderProgram mShaderB;
protected Mesh mMeshA;
protected Mesh mMeshB;
RenderSurface blurTargetB;
RenderSurface blurTargetC;
Texture texture;
public void init() {
texture = new Texture(
Gdx.files.internal("mainMenuBack.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
texture.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
blurTargetB = new RenderSurface(Format.RGBA4444, 1080, 1920, true);
blurTargetC = new RenderSurface(Format.RGBA4444, 1080, 1920, true);
if (mMeshA != null)
mMeshA.dispose();
mMeshA = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 2,
ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(
Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE
+ "0"));
mMeshA.setVertices(new float[] { -1f, -1f, 0, 1, 1f, -1f, 1, 1, 1f, 1f,
1, 0, -1f, 1f, 0, 0 });
if (mMeshB != null)
mMeshB.dispose();
mMeshB = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 2,
ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(
Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE
+ "1"));
mMeshB.setVertices(new float[] { -1f, -1f, 0, 1, 1f, -1f, 1, 1, 1f, 1f,
1, 0, -1f, 1f, 0, 0 });
}
public BlurRenderer() {
mShaderA = createXBlurShader();
mShaderB = createYBlurShader();
init();
batch=new SpriteBatch();
}
public void render() {
drawAToB();
drawBToC();
drawCToSceen();
}
public void drawAToB() {
blurTargetB.begin(GL20.GL_COLOR_BUFFER_BIT);
batch.setShader(mShaderA);
batch.begin();
batch.draw(texture, 0,0,1080,1920);
batch.flush();
blurTargetB.end();
}
public void drawBToC() {
blurTargetC.begin(GL20.GL_COLOR_BUFFER_BIT);
batch.draw( blurTargetB.getTexture(), 0, 0);
batch.flush();
blurTargetC.end();
}
public void dispose() {
mMeshA.dispose();
mMeshB.dispose();
}
public void drawCToSceen() {
batch.draw( blurTargetC.getTexture(), 0, 0);
batch.end();
}
public ShaderProgram createXBlurShader() {
String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
+ "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
+ "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
+ "uniform float uBlurBufferSize;\n" // 1 / Size of the blur
+ "uniform mat4 u_projTrans;\n" //
+ "varying vec4 v_color;\n" //
+ "varying vec2 v_texCoords;\n" //
+ "varying vec2 vBlurTexCoords[5];\n" // output texture
+ "\n" //
+ "void main()\n" //
+ "{\n" //
+ " v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
+ " v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
+ " vBlurTexCoords[0] = v_texCoords + vec2(-2.0 * uBlurBufferSize, 0.0);\n"
+ " vBlurTexCoords[1] = v_texCoords + vec2(-1.0 * uBlurBufferSize, 0.0);\n"
+ " vBlurTexCoords[2] = v_texCoords;\n"
+ " vBlurTexCoords[3] = v_texCoords + vec2( 1.0 * uBlurBufferSize, 0.0);\n"
+ " vBlurTexCoords[4] = v_texCoords + vec2( 2.0 * uBlurBufferSize, 0.0);\n"
+ " gl_Position = u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
+ "}\n";
String fragmentShader = "#ifdef GL_ES\n" //
+ "#define LOWP lowp\n" //
+ "precision mediump float;\n" //
+ "#else\n" //
+ "#define LOWP \n" //
+ "#endif\n" //
+ "varying LOWP vec4 v_color;\n" //
+ "varying vec2 v_texCoords;\n" //
+ "varying vec2 vBlurTexCoords[5];\n" // input texture coords
+ "uniform sampler2D u_texture;\n" //
+ "void main()\n"//
+ "{\n" //
+ " vec4 sum = vec4(0.0);\n"
+ " sum += texture2D(u_texture, vBlurTexCoords[0]) * 0.164074;\n"
+ " sum += texture2D(u_texture, vBlurTexCoords[1]) * 0.216901;\n"
+ " sum += texture2D(u_texture, vBlurTexCoords[2]) * 0.23805;\n"
+ " sum += texture2D(u_texture, vBlurTexCoords[3]) * 0.216901;\n"
+ " sum += texture2D(u_texture, vBlurTexCoords[4]) * 0.164074;\n"
+ " gl_FragColor = sum;\n"
+ "}";
ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
if (shader.isCompiled() == false) {
Gdx.app.log("ERROR", shader.getLog());
}
return shader;
}
It's expensive to blur a 1080x1920 image, especially on Android! Usually, when doing a Gaussian blur, you can downsample the screen to half or a quarter of the width and height without much quality loss. That also lets you get away with a smaller sampling radius to achieve the same appearance.
Im trying to create a circle in LWJGL , using VBO's and VAO , and move it using an offset , but it seems one vertex is stuck in the center of the screen . I can't figure out how to move it to the new location . Any help is appreciated , thanks !
P.S : I have already tried debugging the program , but I can't locate the faulty vertex in my array
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
public class Test {
// Setup variables
private int WIDTH = 800;
private int HEIGHT = 600;
private String title = "Circle";
// Quad variables
private int vbo = 0; // Vertex Buffer Object
private int vao = 0; // Vertex Array Object
int SUBDIVISIONS = 100;
float[] vertex = new float[(SUBDIVISIONS + 1) * 4];
public Test() {
// Initialize
setupOpenGL();
setupQuad();
while (!Display.isCloseRequested()) {
loop();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public void setupOpenGL() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setTitle(title);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(-1); // If error , exit program
}
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
public void setupQuad() {
float r = 0.2f;
float x;
float y;
float offSetX = 0.3f;
float offSetY = 0.3f;
vertex[0] = (float) Math.sin(Math.PI*2*0/SUBDIVISIONS) * r + offSetX;
vertex[1] = (float) Math.cos(Math.PI*2*1/SUBDIVISIONS) * r + offSetY;
for (int i = 2; i < 360; i = i + 2) {
double angle = Math.PI * 2 * i / SUBDIVISIONS;
x = (float) Math.cos(angle) * r;
vertex[i] = x + offSetX;
}
for (int i = 3; i < 360; i = i + 2) {
double angle = Math.PI * 2 * i / SUBDIVISIONS;
y = (float) Math.sin(angle) * r;
vertex[i] = y + offSetY;
}
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(vertex.length);
vertexBuffer.put(vertex);
vertexBuffer.flip();
vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
vbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER,vertexBuffer,GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);
}
public void loop() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL30.glBindVertexArray(vao);
GL20.glEnableVertexAttribArray(0);
// Draw the vertices
GL11.glDrawArrays(GL11.GL_TRIANGLE_FAN, 0, vertex.length / 2);
// Put everything back to default (deselect)
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
}
public static void main(String[] args) {
new Test();
}
}
"I think I've found the problem . I was setting the positions of only 359 vertices out of 404 vertices (nr of subdivisions + 1 times 4) . It seems the rest of the vertices were stuck at 0,0 on the screen . Allowing both FOR statements to cycle up to 404 seems to solve the problem"