Why my code OpenGL android did't draw a triangle? - java

Why my code OpenGL android did't draw a triangle,I don't know where is the problem why forever render black screen, Someone can help me resolve this problem?
public class OpenGL implements GLSurfaceView.Renderer {
int width,height;
float blue = 0.f;
#Override
public void onDrawFrame(GL10 gl){
gl.glClearColor(.0f,.0f,blue,0.f);
gl.glClear(gl.GL_COLOR_BUFFER_BIT);
float Vertex[] = {
0.f, 1.f,
1.f, -1.f,
-1.f, -1.f,
};
ByteBuffer buffer;
FloatBuffer _float;
buffer = ByteBuffer.allocateDirect( Vertex.length * 4 );
buffer.order( ByteOrder.nativeOrder() );
_float = buffer.asFloatBuffer();
_float.put( Vertex );
_float.position(0);
gl.glColor4f(1.f,1.f,0.f,1.f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _float);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, Vertex.length / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height){
this.width = width;
this.height = height;
gl.glViewport(0,0,width,height);
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0,width,0,height,0,1);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
}
}

Related

How to draw a circle in OpenGL 1.0 in Android Studio that moves from the center to right and then to left

This is my code, before some changes it was drawing a blue circle on green background. I don't have any idea how to fix that problem. I was trying many things. The first problem is that it looks distorted, it it more oval that circle, first idea was to create seperate function which was called out in onSurfaceChanged
DrawCircle(gl, width, height) it helped me to get correct shape of circle, but I was not able to move it like in the pattern shown below.
package pl.varkame;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.os.SystemClock;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MyRenderer implements GLSurfaceView.Renderer {
private float kat=0;
public FloatBuffer vertexBuffer;
private DrawCircle Circlek;
private int n_seg=720; // number of segments
float[] vertices = new float[n_seg*2+2];
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Circlek = new DrawCircle();
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float ratio = (float) width/(float) height;
GLU.gluPerspective(gl,45.0f,(float)width/(float)height,-1.0f, -10.0f);
gl.glOrthof(-ratio, ratio, -1.0f, 1.0f, -1.0f, 1.0f);
gl.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
}
#Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(0.0f , 0.0f , 1.0f , 1.0f);
gl.glLoadIdentity();
Circlek.draw();
}
}
class DrawCircle{
public FloatBuffer vertexBuffer;
public int c=0;
private int n_seg=720; // number of segments
float[] vertices = new float[n_seg*2+2];
public float width = 100;
public float height= 50;
float ratio = width/height;
public void Circle(GL10 gl) {
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2 , GL10.GL_FLOAT , 0 , vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN , 0 , vertices.length / 2);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
public void draw(){
for (int i=0; i<n_seg;i++){
float radius=0.1f ;
double rad=(Math.PI*2*i/n_seg);
vertices[c]=(float)Math.cos(rad)*radius;
vertices[c+1]=(float) Math.sin(rad)*(radius/ratio);
c+=2;
}
}
}
I was trying to animate this movement with any success
This was last working code, circle was distorted in it
package pl.varkame;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MyRenderer implements GLSurfaceView.Renderer {
private float kat=0;
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl,45.0f,(float)width/(float)height,-1.0f, -10.0f);
gl.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
}
#Override
public void onDrawFrame(GL10 gl) {
FloatBuffer vertexBuffer;
int c=0;
int n_seg=720; // number of segments
float[] vertices = new float[n_seg*2+2];
for (int i=0; i<n_seg;i++){
float radius=0.1f ; // 10% z szerokości ekranu
double rad=(Math.PI*2*i/n_seg);
vertices[c]=(float)Math.cos(rad)*radius;
vertices[c+1]=(float) Math.sin(rad)*radius;
c+=2;
}
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
gl.glLoadIdentity();
gl.glTranslatef(0.0f,0.0f,0.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,0,vertices.length/2);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}

LWJGL Can't sample framebuffer texture in the shader

I'm trying to make a post processing shader using a framebuffer. But when I attempt to sample the texture in the shaders it doesn't do anything.
As soon as I comment the line(in the fragment shader)
vec4 textureColour = texture(screenTexture, textureCoords);
out everything works again
FrameBuffer.java
package postprocessing;
import java.nio.ByteBuffer;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GL32;
public class FrameBuffer {
protected static final int WIDTH = Display.getWidth();
private static final int HEIGHT = Display.getHeight();
private int framebuffer;
private int texture;
private int depthBuffer;
private int depthTexture;
public FrameBuffer()
{
initialise();
}
public void cleanUp()
{
GL30.glDeleteFramebuffers(framebuffer);
GL11.glDeleteTextures(texture);
GL30.glDeleteRenderbuffers(depthBuffer);
GL11.glDeleteTextures(depthTexture);
}
public int getTexture()
{
return texture;
}
public int getDepthTexture()
{
return depthTexture;
}
private void initialise()
{
framebuffer = createFrameBuffer();
texture = createTextureAttachment(WIDTH, HEIGHT);
depthBuffer = createDepthBufferAttachment(WIDTH, HEIGHT);
depthTexture = createDepthTextureAttachment(WIDTH, HEIGHT);
unbindCurrentFrameBuffer();
}
public void bindFrameBuffer(){
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);//To make sure the texture isn't bound
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, framebuffer);
GL11.glViewport(0, 0, WIDTH, HEIGHT);
}
public void unbindCurrentFrameBuffer() {//call to switch to default frame buffer
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
}
private int createFrameBuffer() {
int frameBuffer = GL30.glGenFramebuffers();
//generate name for frame buffer
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
//create the framebuffer
GL11.glDrawBuffer(GL30.GL_COLOR_ATTACHMENT0);
//indicate that we will always render to color attachment 0
return frameBuffer;
}
private int createTextureAttachment( int width, int height) {
int texture = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height,
0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0,
texture, 0);
return texture;
}
private int createDepthTextureAttachment(int width, int height){
int texture = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT32, width, height, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (ByteBuffer) null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, texture, 0);
return texture;
}
private int createDepthBufferAttachment(int width, int height) {
int depthBuffer = GL30.glGenRenderbuffers();
GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, width,
height);
GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT,
GL30.GL_RENDERBUFFER, depthBuffer);
return depthBuffer;
}
}
FrameBufferShader.java
package postprocessing;
import shaders.ShaderProgram;
public class FrameBufferShader extends ShaderProgram {
private static final String VERTEX_FILE = "src/postprocessing/vertexShader.txt";
private static final String FRAGMENT_FILE = "src/postprocessing/fragmentShader.txt";
private int location_texture;
private int location_depthMap;
public FrameBufferShader()
{
super(VERTEX_FILE, FRAGMENT_FILE);
}
#Override
protected void getAllUniformLocations()
{
bindAttributes(0, "position");
}
#Override
protected void bindAttributes()
{
location_texture = super.getUniformLocation("screenTexture");
location_depthMap = super.getUniformLocation("depthMap");
}
public void connectTextureUnits()
{
super.loadInt(location_texture, 0);
super.loadInt(location_depthMap, 1);
}
}
FrameBufferRenderer.java
package postprocessing;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import models.RawModel;
import renderEngine.Loader;
public class FrameBufferRenderer {
private RawModel quad;
private FrameBufferShader shader;
private FrameBuffer fbo;
public FrameBufferRenderer(Loader loader, FrameBufferShader shader, FrameBuffer fbo)
{
this.fbo = fbo;
this.shader = shader;
setUpVAO(loader);
shader.start();
shader.connectTextureUnits();
shader.stop();
}
public void render()
{
prepareRender();
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, quad.getVertexCount());
unbind();
}
private void prepareRender()
{
shader.start();
GL30.glBindVertexArray(quad.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.getTexture());
GL13.glActiveTexture(GL13.GL_TEXTURE1);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.getDepthTexture());
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}
private void unbind()
{
GL11.glDisable(GL11.GL_BLEND);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
shader.stop();
}
private void setUpVAO(Loader loader) {
float[] vertices = { -1, -1, 1, -1, 1, 1, -1, 1 };
quad = loader.loadtoVAO(vertices, 2);
}
}
Vertex Shader
#version 400
in vec2 position;
void main()
{
gl_Position = vec4(position.x, position.y, 0.0, 1.0);
}
Fragment Shader
#version 400
in vec2 textureCoords;
out vec4 outColor;
uniform sampler2D screenTexture;
uniform sampler2D depthTexture;
void main()
{
vec4 textureColour = texture(screenTexture, textureCoords);
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
You can't sample the destination texture in the shader.
What you can do is make a new texture and make that the destination texture for the previous step and use it as a normal texture in the current step.

android opengl es 1.1 - drawing glitch on certain devices - screenshots inside

The top image is the undesired result (it also seems to flicker).
The bottom image is what I would like the render to look like on ALL devices.
Hello, I seem to be having problems with rendering a textured square to my galaxy s4, but not on my my gt p3113 tablet...
Here is the code from my GLRenderer:
#Override
public void onDrawFrame(GL10 gl)
{
manObjects.draw(gl, 0, 0);
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
if (height == 0)
{ // Prevent A Divide By Zero By
height = 1; // Making Height Equal One
}
gl.glViewport(0, 0, width, height); // Reset The Current Viewport
gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix
gl.glLoadIdentity(); // Reset The Projection Matrix
// respect the height:width ratio of the window.
GLU.gluOrtho2D(gl, -1f, 1f, 1.6f, -1.6f);
gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
gl.glLoadIdentity(); // Reset The Modelview Matrix
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
// Settings
gl.glEnable(GL10.GL_TEXTURE_2D); // Enable Texture Mapping
gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
gl.glClearDepthf(1.0f); // Depth Buffer Setup
// Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glEnable(GL10.GL_BLEND); // Enable blending
gl.glDisable(GL10.GL_DEPTH_TEST); // Disable depth test
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
// Load textures
manObjects.load(gl, mContext);
}
Anyone have any ideas as to why this is happening? And more specifically why it works on one device and not on the other?
Draw code:
public void draw(GL10 gl, float x, float y)
{
gl.glLoadIdentity();
gl.glBindTexture(GL10.GL_TEXTURE_2D, texPointers[0]);
tSquare[0].draw(gl);
}
public void drawBLACK(GL10 gl)
{
gl.glLoadIdentity();
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
gl.glScalef(1f, 1.6f, 1);
gl.glColor4f(0, 0, 0, 1);
tSquare[0].draw(gl);
gl.glColor4f(1, 1, 1, 1);
}
tSquare[0].draw(gl):
public void draw(GL10 gl)
{
//Enable the vertex, texture and normal state
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//Point to our buffers
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
//Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
here is an example of my onDrawFrame, try making your gl calls in onDrawFrame instead
first make
public int width, height;
then
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
this.width = width;
this.height = height;
}
then onDrawFrame something like this
#Override
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, width, 0.0f, height, -1.0f, 1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_TEXTURE_2D);
manObjects.draw(gl, 0, 0);
}
Possible solution:
I have added another draw before the call that draws the star. This added draw call draws a giant black rectangle that covers the entire screen...
I feel like I'm only masking the problem and not really fixing it...
Thoughts?

How do i create a list of Objects in Java

I have a class file called MarkerCustom. MarkerCustom has a constructor that takes three variables of different types.
public MarkerCustom(int myInt, String, myString, BitmapData myBitmap) {
From my main Main activity i want to load a GLSurface view that will take an ArrayList of every instance of MarkerCustom that i will want to load into the GLSurface along with the data that will be passed into each instance of MarkerCustom's constructor.
Lets call the Array List myMarkers;
i need myMarkers to look like this:
myMarkers[0] = [1, "hello", bitMapData1]
myMarkers[1] = [66, "johnHandy", bitmapData2]
i am fairly new to java and its casting ect confuses me a bit having come from AS3
EDIT
So following AKhill's answer below i have edited my GLSurfaceView to accept an ArrayList as shown below. But the MarkerCustom Class needs to be created from each listed in that ArrayList in the constructor in a manner that its is accessible in the onSurfaceCreate and the onDrawFrame method of the GLSurfaceView
Please see those methods and the for loops/comments in this class below:
public class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback, Renderer {
private Context context;
ArrayList <MarkerCustom> locationTags;
private PhoneOrientation phoneOri;
public GLLayer(Context context, int orientation, ArrayList<MarkerCustom> custMarkers) {
super(context);
locationTags = custMarkers;
this.context = context;
phoneOri=new PhoneOrientation(context); // sensor manager and interpreter
for(int i =0; i<locationTags.size(); i++){
//Need to create a an instance of each MarkerCustom
// something like MarkerCustom locationTags[i]; = new MarkerCustom (its params);
}
this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
this.setRenderer(this);
phoneOri.start(context, orientation);
}
#Override
public void onDrawFrame(GL10 gl) {
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
GLU.gluLookAt(gl,0, 0, 0, 0, 0, 0, 0, 0, 0);
float floatMat[]=phoneOri.getMatrix();
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadMatrixf(floatMat, 0);
for(int i=0;i<loacationTags.size();i++){
gl.glPushMatrix();
//locationTags[i].draw(gl);
gl.glLoadMatrixf(floatMat,0);
}
gl.glPushMatrix();
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) {
height = 1;
}
float ratio = (float) width / height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 35.0f, (float)width / (float)height, 5.0f, 200.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 1.0f, 5.0f, 0, 0, 0, 0, 1.0f, 0);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
for(int i=0;i<locationTags.size();i++){
//call each MarkerCustom's loadGLTexture Method
//locationTags[i].loadGLTexture(gl, this.context);
}
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_SMOOTH);
}
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
}
}
And Just for reference here is my MarkerCustom class
public class MarkerCustom {
public float xPos;
public float yPos;
public float zPos;
public float yaw;
public float pitch;
public Bitmap tagImage;
private FloatBuffer vertexBuffer; // buffer holding the vertices
private float vertices[] = {
0.0f, -10.0f, -10.0f, // V1 - bottom left
0.0f, -10.0f, 10.0f, // V2 - top left
0.0f, 10.0f, -10.0f, // V3 - bottom right
0.0f, 10.0f, 10.0f // V4 - top right
};
private FloatBuffer textureBuffer; // buffer holding the texture coordinates
private float texture[] = {
// Mapping coordinates for the vertices
0.0f, 1.0f, // top left (V2)
0.0f, 0.0f, // bottom left (V1)
1.0f, 1.0f, // top right (V4)
1.0f, 0.0f // bottom right (V3)
};
public MarkerCustom(float x, float y, float z, float yawAngle, float pitchAngle, Bitmap bitmap) {
xPos = x;
yPos = y;
zPos = z;
yaw = yawAngle;
pitch = pitchAngle;
tagImage = bitmap;
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuffer.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
textureBuffer = byteBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
/** The texture pointer */
private int[] textures = new int[1];
public void loadGLTexture(GL10 gl, Context context) {
// loading texture
// Enable blending using premultiplied alpha.
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
// generate one texture pointer
gl.glGenTextures(1, textures, 0);
// ...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// create nearest filtered texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, tagImage, 0);
// Clean up
tagImage.recycle();
}
public void draw(GL10 gl) {
// bind the previously generated texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// Point to our buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Set the face rotation
gl.glFrontFace(GL10.GL_CW);
// Point to our vertex buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
}
Try this.
List<MarkerCustom> myList=new ArrayList<MarkerCustom>();
MarkerCustom entry1=new MarkerCustom(myInt, myString, myBitmap);
MarkerCustom entry2=new MarkerCustom(myInt, myString, myBitmap);
myList.add(entry1);
myList.add(entry2);
Shorthand:
List<MarkerCustom> markerList = Arrays.asList(
new MarkerCustom(1, "hello", bitMapData1),
new MarkerCustom(66, "johnHandy", bitMapData2))
};
In this case I think it's easier to create a class Marker (for instance) that has three attributes.
For example:
class Marker {
int var1;
String var2;
BitmapData var3; }
This way, you are able to store an ArrayList of Markers (ArrayList), and you can access all the information you need.

Using OpenGL to replace Canvas - Android

I am attempting to replace the Canvas-based rendering system that I already have with the faster opengl-es surface, however, I can't seem to get an openGL renderer to conform in such a way that it acts as 2d field, rather than a perspective view.
My current code for the renderer looks as follows:
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, width, 0.0f, height, 0.0f, 1.0f);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
gl.glEnable(GL10.GL_TEXTURE_2D);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glDisable(GL10.GL_DITHER);
gl.glDisable(GL10.GL_LIGHTING);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}
How would I setup the renderer so that the translate transformation would match up with the pixels on the screen? (so translating 5 to the right would move it 5 pixels)
Please note that in openGL that Y co-ordinate is inverted. Otherwise all is the same.
As for the correct flags, I recommend you check out the open source android game replica island: http://code.google.com/p/replicaisland/
Here's what I use in my own code:
public void onSurfaceChanged(GL10 gl, int width, int height)
{
mViewWidth = width;
mViewHeight = height;
gl.glViewport(0, 0, mViewWidth, mViewHeight);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glViewport(0, 0, mViewWidth, mViewHeight);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_TEXTURE_2D);
GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
}
Where mViewWidth & mViewHeight are the size of the display.

Categories

Resources