Related
I am creating a bar graph in android using opengl
from an activity I'm calling the renderer and from the renderer I'm calling a cube(im using cubes for showing the bars)
Here is my renderer code:
public class BarRenderer implements Renderer {
int[] vals;
private float translatex, translatez, scaly;
public BarRenderer(boolean useTranslucentBackground, int[] vals) {
mTranslucentBackground = useTranslucentBackground;
this.vals = vals;
for (int i = 0; i < this.vals.length; i++) {
mcube[i] = new Cube();
}
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
translatex = 0.5f;
scaly = 0.8f;
for (int i = 0; i < vals.length; i++) {
gl.glTranslatef((-1.0f + (translatex * i)), 0.0f, translatez);
gl.glRotatef(mAngle, 0.0f, 1.0f, 0.0f);
gl.glScalef(0.4f, scaly * vals[i], 0.6f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
mcube[i].draw(gl);
}
mTransY = .075f;
mAngle = -60.0f;
translatez = -7.0f;
Log.i("Draw", "called");
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float aspectRatio;
float zNear = .1f;
float zFar = 1000;
float fieldOfView = 80.0f / 57.3f;
float size;
gl.glEnable(GL10.GL_NORMALIZE);
aspectRatio = (float) width / (float) height;
gl.glMatrixMode(GL10.GL_PROJECTION);
size = zNear * (float) (Math.tan((double) (fieldOfView / 2.0f)));
gl.glFrustumf(-size, size, -size / aspectRatio, size / aspectRatio,
zNear, zFar);
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
if (mTranslucentBackground) {
gl.glClearColor(0, 0, 0, 0);
} else {
gl.glClearColor(1, 1, 1, 1);
}
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_POINT_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
private boolean mTranslucentBackground;
private Cube[] mcube = new Cube[4];
private float mTransY;
private float mAngle;
}
but unfortunately it is giving the bar graph like this:
anyone will understand this is not exactly like a bar graph
but please point out my faults here, where am i doing wrong:
1>the bar heights are 4 1 2 1 but they r not accurately of their sizes
2>the space between the bars are supposed to be .5f apart but they start with a big gap but reduce repeatedly after every bar
3>how to start them from one base plane
EDIT:
4> can I animate the growth of this bars?how to do that
My cube code:
public class Cube {
private ByteBuffer mTfan1;
private ByteBuffer mTfan2;
private int bar;
public Cube(int i) {
this.bar = i;
float vertices[] = {
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f,1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f
};
byte maxColor = (byte) 255;
byte colors[][] =
{
{maxColor,maxColor, 0,maxColor},
{0, maxColor,maxColor,maxColor},
{0, 0, 0,maxColor},
{maxColor, 0,maxColor,maxColor},
{maxColor, 0, 0,maxColor},
{0, maxColor, 0,maxColor},
{0, 0,maxColor,maxColor},
{0, 0, 0,maxColor}
};
byte tfan1[] =
{
1,0,3,
1,3,2,
1,2,6,
1,6,5,
1,5,4,
1,4,0
};
byte tfan2[] =
{
7,4,5,
7,5,6,
7,6,2,
7,2,3,
7,3,0,
7,0,4
};
byte indices[] =
{ 0, 3, 1, 0, 2, 3 };
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mFVertexBuffer = vbb.asFloatBuffer();
mFVertexBuffer.put(vertices);
mFVertexBuffer.position(0);
mColorBuffer = ByteBuffer.allocateDirect(colors.length);
mColorBuffer.put(colors[bar]);
mColorBuffer.position(0);
mTfan1 = ByteBuffer.allocateDirect(tfan1.length);
mTfan1.put(tfan1);
mTfan1.position(0);
mTfan2 = ByteBuffer.allocateDirect(tfan2.length);
mTfan2.put(tfan2);
mTfan2.position(0);
}
public void draw(GL10 gl)
{
gl.glVertexPointer(3, GL11.GL_FLOAT, 0, mFVertexBuffer);
gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);
gl.glDrawElements( gl.GL_TRIANGLE_FAN, 6 * 3, gl.GL_UNSIGNED_BYTE, mTfan1);
gl.glDrawElements( gl.GL_TRIANGLE_FAN, 6 * 3, gl.GL_UNSIGNED_BYTE, mTfan2);
}
private FloatBuffer mFVertexBuffer;
private ByteBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
}
i think something has to be done in the draw method but what??how to animate here?
please help
thanks
2 Before glTranslate and rotate calls you should use glPushMatrix and after that glPopMatrix functions, or call glTranslate without increment. Because glTranslate multiply existing projection matrix on new matrix from glTranslate parameters
3
gl.glPushMatrix() ;
gl.glTranslate(0,y,0);
mcube[i].draw(gl);
gl.glPopMatrix();
with y= - Barheight/2 in your case or change vertex coordinates of your bars
I have done creating the 3d bar graph though I have some other issues right now
but I believe some one might be helpful with this code.
Myrenderer class:
public class BarRenderer implements GLSurfaceView.Renderer {
int[] vals;
private float translatex, scaly;
// For controlling cube's z-position, x and y angles and speeds (NEW)
float angleX = 0; // (NEW)
float angleY = 0; // (NEW)
float speedX = 0; // (NEW)
float speedY = 0; // (NEW)
float translatez;
public BarRenderer(Context context, boolean useTranslucentBackground,
int[] vals) {
mTranslucentBackground = useTranslucentBackground;
mfloor = new Cube(0);
mwall = new Cube(0);
this.vals = vals;
for (int i = 0; i < this.vals.length; i++) {
mcube[i] = new Cube(i);
}
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
translatex = 2.0f;
scaly = 0.8f;
for (int i = 0; i < vals.length; i++) {
gl.glPushMatrix();
mTransY = -5 + (scaly * vals[i]);
gl.glTranslatef((-3.0f + (translatex * i)), mTransY, translatez);
gl.glRotatef(mAngleX, 1.0f, 0.0f, 0.0f);
gl.glRotatef(mAngleY, 0.0f, 1.0f, 0.0f);
gl.glScalef(0.4f, scaly * vals[i], 0.4f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
mcube[i].draw(gl);
gl.glPopMatrix();
}
mTransY = .075f;
// mAngleX = -90.0f;
// mAngleY = -0.01f;
mAngleX += speedX;
mAngleY += speedY;
translatez = -6.0f;
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float aspectRatio;
float zNear = .1f;
float zFar = 1000;
float fieldOfView = 80.0f / 57.3f;
float size;
gl.glEnable(GL10.GL_NORMALIZE);
aspectRatio = (float) width / (float) height;
gl.glMatrixMode(GL10.GL_PROJECTION);
size = zNear * (float) (Math.tan((double) (fieldOfView / 2.0f)));
gl.glFrustumf(-size, size, -size / aspectRatio, size / aspectRatio,
zNear, zFar);
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
if (mTranslucentBackground) {
gl.glClearColor(0, 0, 0, 0);
} else {
gl.glClearColor(1, 1, 1, 1);
}
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_POINT_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
private boolean mTranslucentBackground;
private Cube[] mcube = new Cube[4];
private Cube mfloor, mwall;
private float mTransY;
private float mAngleX,mAngleY;
}
and myGLsurfaceview class:
public class MyBarGLSurfaceView extends GLSurfaceView {
BarRenderer renderer; // Custom GL Renderer
// For touch event
private final float TOUCH_SCALE_FACTOR = 180.0f / 320.0f;
private float previousX;
private float previousY;
public MyBarGLSurfaceView(Context context,
boolean useTranslucentBackground, int[] vals) {
super(context);
renderer = new BarRenderer(context, useTranslucentBackground, vals);
this.setRenderer(renderer);
// Request focus, otherwise key/button won't react
this.requestFocus();
this.setFocusableInTouchMode(true);
}
// Handler for key event
#Override
public boolean onKeyUp(int keyCode, KeyEvent evt) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT: // Decrease Y-rotational speed
renderer.speedY -= 01.1f;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT: // Increase Y-rotational speed
renderer.speedY += 01.1f;
break;
case KeyEvent.KEYCODE_DPAD_UP: // Decrease X-rotational speed
renderer.speedX -= 01.1f;
break;
case KeyEvent.KEYCODE_DPAD_DOWN: // Increase X-rotational speed
renderer.speedX += 01.1f;
break;
case KeyEvent.KEYCODE_A: // Zoom out (decrease z)
renderer.translatez -= 0.2f;
break;
case KeyEvent.KEYCODE_Z: // Zoom in (increase z)
renderer.translatez += 0.2f;
break;
}
return true; // Event handled
}
// Handler for touch event
#Override
public boolean onTouchEvent(final MotionEvent evt) {
float currentX = evt.getX();
float currentY = evt.getY();
float deltaX, deltaY;
switch (evt.getAction()) {
case MotionEvent.ACTION_MOVE:
// Modify rotational angles according to movement
deltaX = currentX - previousX;
deltaY = currentY - previousY;
renderer.angleX += deltaY * TOUCH_SCALE_FACTOR;
renderer.angleY += deltaX * TOUCH_SCALE_FACTOR;
}
// Save current x, y
previousX = currentX;
previousY = currentY;
return true; // Event handled
}
}
though it is done still I would like if anyone can please answer me
1> how to make this bar graphs to grow animatedly, right now when this bar graph activity starts the bar graph appears but i want the bar graph to grow animatedly not just show it
2> how to create a base and a background wall to the bar graph like this link http://www.fusioncharts.com/demos/gallery/#column-and-bar
3> and I was trying to rotate or move the bar graph as per user touch movements i tried the code like another example, you will see that code too and that is why i created this glsurfaceview class of my own, but for some reason it is not working, don't know where and what i have missed, if anyone have any idea please point it out to me
thanks
Hello my rendering is very glitchy on Tegra 3 devices.. any Idea how this is possible?
Here are two screens. the first one is the glitchy one from an Asus A510 tablet and the second is the correct rendering on my galaxy nexus
And here is my code of the render class:
public class ShowCaseRenderer {
GLGraphics glGraphics;
LookAtCamera camera;
AmbientLight ambientLight;
DirectionalLight directionalLight;
public float rotation = 0;
public static float rotationCamera = 0;
private Vector2 camDirection;
public static final int SWIPE_LEFT = -1;
public static final int SWIPE_RIGHT = 1;
public ShowCaseRenderer(GLGraphics glGraphics) {
camDirection = new Vector2();
this.glGraphics = glGraphics;
camera = new LookAtCamera(67, glGraphics.getWidth()
/ (float) glGraphics.getHeight(), 0.1f, 100);
ambientLight = new AmbientLight();
ambientLight.setColor(0.2f, 0.2f, 0.2f, 1.0f);
directionalLight = new DirectionalLight();
directionalLight.setDirection(-1, -0.5f, 0);
GL10 gl = glGraphics.getGL();
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 67,
glGraphics.getWidth() / (float) glGraphics.getHeight(), 0.1f,
10.0f);
}
public void render(int swipe, float deltaTime) {
GL10 gl = glGraphics.getGL();
camDirection.set((float) Math.cos(rotationCamera * Vector2.TO_RADIANS),
(float) Math.sin(rotationCamera * Vector2.TO_RADIANS)).nor();
camera.getPosition().set(0, 0, 0)
.sub(camDirection.x * 2.55f, -0.4f, camDirection.y * 2.5f);
camera.getLookAt().set(0, 0, 0);
camera.setMatrices(gl);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_COLOR_MATERIAL);
gl.glEnable(GL10.GL_BLEND);
ambientLight.enable(gl);
directionalLight.enable(gl, GL10.GL_LIGHT0);
if (swipe == SWIPE_LEFT && rotationCamera % 90 != 0) {
rotationCamera = rotationCamera -= 1.5f;
}
if (swipe == SWIPE_RIGHT && rotationCamera % 90 != 0) {
rotationCamera = rotationCamera += 1.5f;
}
if (rotationCamera == 360)
rotationCamera = 0;
if (rotationCamera < 0)
rotationCamera = rotationCamera + 360;
//eigenRotation der Autos
rotation = rotation += deltaTime * 25;
if (rotation > 360) {
rotation = rotation - 360;
}
renderShowCase(gl);
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glDisable(GL10.GL_COLOR_MATERIAL);
gl.glDisable(GL10.GL_LIGHTING);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glDisable(GL10.GL_BLEND);
}
private void renderShowCase(GL10 gl) {
Assets.ectoMobileTexture.bind();
Assets.ectoMobileModel.bind();
gl.glPushMatrix();
gl.glTranslatef(0, 0.01f, -1.5f);
gl.glRotatef(-rotation, 0, 1, 0);
Assets.ectoMobileModel.draw(GL10.GL_TRIANGLES, 0,
Assets.ectoMobileModel.getNumVertices());
gl.glPopMatrix();
Assets.ectoMobileModel.unbind();
Assets.batMobileTexture.bind();
Assets.batMobileModel.bind();
gl.glPushMatrix();
gl.glTranslatef(0, 0.01f, 1.5f);
gl.glRotatef(-rotation, 0, 1, 0);
Assets.batMobileModel.draw(GL10.GL_TRIANGLES, 0,
Assets.batMobileModel.getNumVertices());
gl.glPopMatrix();
Assets.batMobileModel.unbind();
Assets.mysteryMachineTexture.bind();
Assets.mysteryMachineModel.bind();
gl.glPushMatrix();
gl.glTranslatef(-1.5f, 0.01f, 0);
gl.glRotatef(-rotation, 0, 1, 0);
Assets.mysteryMachineModel.draw(GL10.GL_TRIANGLES, 0,
Assets.mysteryMachineModel.getNumVertices());
gl.glPopMatrix();
Assets.mysteryMachineModel.unbind();
Assets.podRacerTexture.bind();
Assets.podRacerModel.bind();
gl.glPushMatrix();
gl.glTranslatef(1.5f, 0.01f, 0);
gl.glRotatef(-rotation, 0, 1, 0);
Assets.podRacerModel.draw(GL10.GL_TRIANGLES, 0,
Assets.podRacerModel.getNumVertices());
gl.glPopMatrix();
Assets.podRacerModel.unbind();
}
}
thank you...
ok solved the Problem. The glitch caused a guiElement, that had a strange blend config...
I want to overlay an icon on top of 3D models in my game. I'm using gluProject to get the screen coordinates of the centre point of the models, and then using that data to draw icons on a custom view:
In my renderer class:
private void projectScreenCoords(GLSurfaceView view, GraphicsEntity ge, GL10 gl){
MatrixGrabber matrixGrabber = new MatrixGrabber();
matrixGrabber.getCurrentModelView(gl);
float[] modelMat = matrixGrabber.mModelView;
matrixGrabber.getCurrentProjection(gl);
float[] projMat = matrixGrabber.mProjection;
gl.glMatrixMode(GL10.GL_MODELVIEW);
// view port
int[] viewport = new int[]{view.getTop(),view.getLeft(),view.getWidth(),view.getHeight()};
float[] vector = new float[4];
GLU.gluProject(ge.getTransform().tx, ge.getTransform().ty, ge.getTransform().tz, modelMat, 0, projMat, 0, viewport, 0, vector, 0);
ge.setScreenCoords(vector[0], viewport[3] - vector[1]);
}
and my custom view:
protected void onDraw (Canvas canvas){
Vector<GraphicsEntity> scene = renderer.getForegroundScene();
for(int i = 0;i<scene.size();i++){
GraphicsEntity ge = scene.get(i);
float[] xy = ge.getScreenCoords();
if(xy[0]>-1 && xy[1]>-1){
canvas.drawBitmap(bitmap, xy[0]-bitmapWidth/2, xy[1]-bitmapHeight/2, null);
}
invalidate();
}
}
and where I set my projection matrix:
protected void setProjectionMatrix(GL10 gl){
float ratio = (float) viewportWidth / viewportHeight;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio*zoom, ratio*zoom, -1*zoom, 1*zoom, nearPlane, farPlane);
}
However, the further from the centre of the screen, the further off the centre of the model the icon is drawn:
Clearly, if it was just the viewport size that was incorrect (due to the bars at the top/bottom) then the icons would be off just in the y-axis, but they are off in the x-axis as well. What am I missing / doing wrong? Also I haven't used the 3rd value returned by gluProject, but this always has a value of 0.7 so I'm not quite sure how it would be used if at all?
Using SDK version 7, I've tested this on multiple devices (ZTE Blade running android 2.1 and Kindle Fire running 2.3.4) with the same results. viewportWidth/Height and view.getWidth/Height() are the same, and view.getTop/Left() returns 0. The MatrixGrabber code works for gluUnProject, so I'm reasonably confident that isn't the problem
EDIT: here is the rest of my GL-related drawing code:
In renderer:
// camera variables
protected float FOV = 60.0f;
protected float nearPlane = 3;
protected float farPlane = 7;
protected float eyeX = 0;
protected float eyeY = 0;
protected float eyeZ = 0;
protected float centreX = 0;
protected float centreY = 0;
protected float centreZ = ((farPlane - nearPlane) / 2) + nearPlane;
protected float upX = 0;
protected float upY = 1;
protected float upZ = 0;
protected float viewportWidth;
protected float viewportHeight;
// user camera control variables
protected float zoom = 1;
protected float rotatedX = 0;
protected float rotatedY = 0;
protected boolean zoomed = true;
protected TrackingTransform tracking;
protected void setWorldTransform(GL10 gl){
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
// transforms the centre position to 0,0,0
gl.glTranslatef(-centreX, -centreY, -centreZ);
}
protected void setModelRotation(GL10 gl, GraphicsEntity ge){
if(ge.isTrackerball()){
gl.glRotatef(rotatedX, 1.0f, 0, 0);
gl.glRotatef(rotatedY, 0, -1.0f, 0);
if(tracking!=null){
synchronized(tracking){
tracking.transform(gl);
}
}
} else if(ge.isBackground()){
gl.glTranslatef(centreX, centreY, centreZ);
gl.glRotatef(rotatedX, 1.0f, 0, 0);
gl.glRotatef(rotatedY, 0, -1.0f, 0);
if(ge.isSkybox()==true){
ge.getTransform().sx = nearPlane + 1.0f;
ge.getTransform().sy = nearPlane + 1.0f;
ge.getTransform().sz = nearPlane + 1.0f;
ge.getTransform().tx = 0;
ge.getTransform().ty = 0;
ge.getTransform().tz = 0;
}
}
}
protected void setModelViewMatrix(GL10 gl){
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
// not used:
//GLU.gluLookAt(gl, eyeX, eyeY, eyeZ, centreX, centreY, centreZ, upX, upY, upZ);
}
#Override
public void onDrawFrame(GL10 gl) {
// Set up various things before drawing
gl.glFrontFace(GL10.GL_CW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_FRONT);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL10.GL_DEPTH_TEST);
// change projection matrix
float oldzoom = zoom;
zoom = 1.0f;
setProjectionMatrix(gl);
zoom = oldzoom;
// set global world transform (also changes to modelview)
setWorldTransform(gl);
// loop through and draw background models
for(int i = 0;i<backgroundGraphicsEntities.size();i++){
GraphicsEntity ge = backgroundGraphicsEntities.get(i);
SimpleTransform t = ge.getTransform();
int modelIndex = ge.getModelIndex();
if(modelIndex>=0){
gl.glPushMatrix();
setModelRotation(gl, ge);
t.transform(gl);
if(ge.isSprite() && gl11flag){
Sprite s = sprites.get(modelIndex);
s.draw((GL11) gl, ge);
} else {
Model m = models.get(modelIndex);
if(m!=null){
m.draw(gl);
}
}
gl.glPopMatrix();
}
if(i==0 && ge.isSkybox()){
// if skybox, reset depth bit
gl.glClear(GL10.GL_DEPTH_BUFFER_BIT);
}
}
gl.glClear(GL10.GL_DEPTH_BUFFER_BIT);
// change projection matrix (if zoomed)
setProjectionMatrix(gl);
// change back to modelview
gl.glMatrixMode(GL10.GL_MODELVIEW);
// loop through and draw models
for(int i = 0;i<graphicsEntities.size();i++){
GraphicsEntity ge = graphicsEntities.get(i);
SimpleTransform t = ge.getTransform();
int modelIndex = ge.getModelIndex();
if(modelIndex>=0){
gl.glPushMatrix();
setModelRotation(gl, ge);
t.transform(gl);
if(ge.isSprite() && gl11flag){
Sprite s = sprites.get(modelIndex);
s.draw((GL11) gl, ge);
} else {
Model m = models.get(modelIndex);
if(m!=null){
m.draw(gl);
}
if(projectScreenCoords){
projectScreenCoords(glSurfaceView, ge, gl);
}
}
gl.glPopMatrix();
}
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
viewportWidth = width;
viewportHeight = height;
gl.glViewport(0, 0, width, height);
setProjectionMatrix(gl);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
if(gl instanceof GL11){
gl11flag = true;
}
gl.glClearColor(0, 0, 0, 1);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthMask(true);
//gl.glClearDepthf(1.0f);
gl.glDepthFunc(GL10.GL_LEQUAL);
//gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
setProjectionMatrix(gl);
}
and then in SimpleTransform (i.e. what gets called when ge.getTransform().transform(gl) is called):
public void transform(GL10 gl) {
gl.glTranslatef(tx, ty, tz);
gl.glRotatef(rz, 0, 0, 1);
gl.glRotatef(ry, 0, 1, 0);
gl.glRotatef(rx, 1, 0, 0);
gl.glScalef(sx, sy, sz);
}
and for TrackingTransform:
#Override
public void transform(GL10 gl) {
gl.glTranslatef(-tx, -ty, -tz);
}
and finally in model.draw():
public void draw(GL10 gl){
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// Pass the vertex buffer in
gl.glVertexPointer(3, GL10.GL_FLOAT, 0,
vertices);
int textureID = material.getTexture().getTextureID();
if(textureID>=0){
// Enable Textures
gl.glEnable(GL10.GL_TEXTURE_2D);
// Get specific texture.
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID);
// Use UV coordinates.
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Pass in texture coordinates
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureCoordinates);
}
// Pass in vertex normals
gl.glNormalPointer(GL10.GL_FLOAT, 0, normals);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glDrawElements(GL10.GL_TRIANGLES, numindices,GL10.GL_UNSIGNED_SHORT, indices);
gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
if(textureID>=0){
// Disable buffers
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
The problem wasn't in the gluProject code at all. In fact, it was to do with the translations done before calling gluProject:
In onDrawFrame:
Model m = models.get(modelIndex);
if(m!=null){
m.draw(gl);
gl.glTranslatef(-tracking.getTransform().tx, -tracking.getTransform().ty, -tracking.getTransform().tz);
if(tickcount % projectFrequency == 0 ){
projectScreenCoords(glSurfaceView, ge, gl);
}
}
I am building a small Android application which will add a cube on user's touch. The z depth is always -10, screen is always in landscape mode. This question seems to be asked many times, but I cannot get it running, forgive me as I am newbie to opengl.
I am quite confused about the screen coordinate/window coordinate/object coordinate to use with the gluUnproject. As I understand, screen is when user touch and we get x and y from motion even. Window coordinate is when we load the identity, and object coordinate is when we transform the identity matrix to get object's coordinate. Is that right?
And here is my code, the matrix stacking is from android api sample. Please point out what I am doing wrong.
The drawing part:
public void draw(GL10 gl) {
for (GLObject glObject : list) {
if (glObject != null) {
gl.glLoadIdentity();
glObject.draw(gl);
}
}
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
if (isTouching) {
boolean addObject = false;
for (GLObject glObject : list) {
addObject = glObject.checkTouch(gl, x, y);
}
if (!addObject) {
i++;
Log.d("i", i + "");
addGLObject(gl);
}
isTouching = false;
}
}
Here is the code for adding object
private void getMatrix(GL10 gl, int mode, float[] mat) {
GLView.matrixTrackingGL.glMatrixMode(mode);
GLView.matrixTrackingGL.getMatrix(mat, 0);
}
public void addGLObject(GL10 gl) {
float[] XY = getWorldCoordinate(gl, x, y);
if (XY != null) {
// XY[0] = (float) (x - Main.SCREEN_WIDTH / 2) / 10;
//
// XY[1] = (float) (Main.SCREEN_HEIGHT / 2 - y) / 10;
GLObject glObject = new GLObject(colors, XY[0], XY[1]);
Log.d("Object position", "X: " + XY[0] + " Y: " + XY[1]);
list.add(glObject);
}
}
private float[] getWorldCoordinate(GL10 gl, int x, int y) {
float[] modelMatrix = new float[16];
float[] projMatrix = new float[16];
int[] viewport = {0, 0, Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT};
getMatrix(gl, GL10.GL_MODELVIEW, modelMatrix);
getMatrix(gl, GL10.GL_PROJECTION, projMatrix);
float[] output = new float[4];
GLU.gluUnProject(x, viewport[1] + viewport[3] - y, -10, modelMatrix, 0, projMatrix, 0, viewport, 0, output, 0);
return new float[] {output[0]/output[3], output[1]/output[3]};
}
public class GLObject {
private float[] vertices = { 1.000000f, 1.000000f, -1.000000f, 1.000000f,
-1.000000f, -1.000000f, -1.000000f, -1.000000f, -1.000000f,
-1.000000f, 1.000000f, -1.000000f, 1.000000f, 1.000000f, 1.000000f,
1.000000f, -1.000001f, 1.000000f, -1.000000f, -1.000000f,
1.000000f, -1.000000f, 1.000000f, 1.000000f, };
private short[] faces = { 0, 1, 2, 0, 2, 3, 4, 7, 6, 4, 6, 5, 0, 4, 5, 0,
5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2, 7, 3, 4, 0, 3, 4, 3, 7 };
private float[] colors;
private float[] rot = { 0.0f, 0.0f, 0.0f };
private Float positionX, positionY;
private int alpha = 0;
// Our vertex buffer.
private FloatBuffer vertexBuffer;
// Our index buffer.
private ShortBuffer faceBuffer;
public GLObject() {
init();
}
public GLObject(float[] colors, float x, float y) {
this.colors = colors.clone();
this.positionX = x;
this.positionY = y;
if (positionX.intValue() % 2 == 0) {
rot[0] = 1.0f;
} else {
if (positionY.intValue() % 2 == 0) {
rot[1] = 1.0f;
} else {
rot[2] = 1.0f;
}
}
init();
}
private void init() {
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer ibb = ByteBuffer.allocateDirect(faces.length * 2);
ibb.order(ByteOrder.nativeOrder());
faceBuffer = ibb.asShortBuffer();
faceBuffer.put(faces);
faceBuffer.position(0);
}
public boolean checkTouch(GL10 gl, int x, int y) {
boolean isTouched = false;
ByteBuffer pixels = ByteBuffer.allocate(4);
gl.glReadPixels(x, Main.SCREEN_HEIGHT - y, 1, 1, GL10.GL_RGBA,
GL10.GL_UNSIGNED_BYTE, pixels);
Log.d("COLOR",
pixels.get(0) + " " + pixels.get(1) + " " + pixels.get(2) + " "
+ pixels.get(3));
// isTouched always false to test always add object to screen
return isTouched;
}
public void draw(GL10 gl) {
// Counter-clockwise winding.
gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
// Enable face culling.
gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs
// What faces to remove with the face culling.
gl.glCullFace(GL10.GL_BACK); // OpenGL docs
// Enabled the vertices buffer for writing and to be used during
// rendering.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// OpenGL docs.
// Enable color
gl.glColor4f(colors[0], colors[1], colors[2], 1.0f);
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glPushMatrix();
gl.glTranslatef(positionX, positionY, -10);
// rotate
alpha += 1;
gl.glRotatef(alpha, rot[0], rot[1], rot[2]);
// draw
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, // OpenGL docs
vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, faces.length,// OpenGL docs
GL10.GL_UNSIGNED_SHORT, faceBuffer);
gl.glPopMatrix();
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // OpenGL docs
// Disable face culling.
gl.glDisable(GL10.GL_CULL_FACE); // OpenGL docs
}
}
onDraw:
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
// Clears the screen and depth buffer.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs.
GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
// Reset the projection matrix
gl.glLoadIdentity();
GLU.gluPerspective(gl, 90.0f, Main.SCREEN_WIDTH/Main.SCREEN_HEIGHT, 3.0f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 4.0f, 2.0f, 1.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
// draw object
glObjectManager.draw(gl);
}
It has nothing to do with landscape mode, the problem is that you are translating your cube by -10 on the z axis. Try putting values of -100 and all the cubes will be at the center. Then try putting 0 for the z value (x, y, 0). The cubes will be much further out from the center of the screen and closer to where you expect them to be.
Look at http://tle.tafevc.com.au/toolbox/file/9495cce8-17b5-a8a5-d9b3-47c0c142d88d/1/sketches_and_drawings_lo.zip/3204a_20_reading_drawings/images/brick_perspective.jpg for an example of perspective projection. When the cube is at z=0, that is what is in the image. But when z=-10, it is closer to the center of the screen (the X) and smaller, because it follows the blue lines out to the horizon.
I've been trying to create my own sphere using OpenGL ES and I followed the math described here http://www.math.montana.edu/frankw/ccp/multiworld/multipleIVP/spherical/learn.htm
However when I draw the sphere (just the vertices), just half of the sphere is been drawn. Can you please point to me what is the exact problem in the code that I am giving below:
public class Sphere {
static private FloatBuffer sphereVertex;
static private FloatBuffer sphereNormal;
static float sphere_parms[]=new float[3];
double mRaduis;
double mStep;
float mVertices[];
private static double DEG = Math.PI/180;
int mPoints;
/**
* The value of step will define the size of each facet as well as the number of facets
*
* #param radius
* #param step
*/
public Sphere( float radius, double step) {
this.mRaduis = radius;
this.mStep = step;
sphereVertex = FloatBuffer.allocate(40000);
mPoints = build();
Log.d("ALIS CHECK!!!!!!", " COUNT:" + mPoints);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, sphereVertex);
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_POINTS, 0, mPoints);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
private int build() {
/**
* x = p * sin(phi) * cos(theta)
* y = p * sin(phi) * sin(theta)
* z = p * cos(phi)
*/
double dTheta = mStep * DEG;
double dPhi = dTheta;
int points = 0;
for(double phi = -(Math.PI/2); phi <= Math.PI/2; phi+=dPhi) {
//for each stage calculating the slices
for(double theta = 0.0; theta <= (Math.PI * 2); theta+=dTheta) {
sphereVertex.put((float) (mRaduis * Math.sin(phi) * Math.cos(theta)) );
sphereVertex.put((float) (mRaduis * Math.sin(phi) * Math.sin(theta)) );
sphereVertex.put((float) (mRaduis * Math.cos(phi)) );
points++;
}
}
sphereVertex.position(0);
return points;
}
}
Also here is my renderer class:
/**
* These values are used to rotate the image by a certain value
*/
private float xRot;
private float yRot;
public void setxRot(float xRot) {
this.xRot += xRot;
}
public void setyRot(float yRot) {
this.yRot += yRot;
}
public GLViewRenderer(Context ctx) {
//initialize our 3D triangle here
mSphere = new Sphere(1, 25);
//Initializing the rate counter object
//This will help us in calculating the frames per second
fpsCalculator = new RateCounter(TAG);
fpsCalculator.start();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//initialize all the things required for openGL configurations
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
public void onDrawFrame(GL10 gl) {
if(fpsCalculator != null)
fpsCalculator.countOnce();
//write the drawing code needed
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
/**
* Change this value in z if you want to see the image zoomed in
*/
gl.glTranslatef(0.0f, 0.0f, -5.0f);
gl.glRotatef(xRot, 0.0f, 1.0f, 0.0f);
gl.glRotatef(yRot, 1.0f, 0.0f, 0.0f);
mSphere.draw(gl);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) {
height = 1;
}
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
//Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
gl.glLoadIdentity(); //Reset The Modelview Matrix
}
/**
* Used to stop the FPS counter
*/
public void pause() {
if(fpsCalculator != null)
fpsCalculator.stop();
}
}
I'd really appreciate some help on this.
P.S: I tried changing CW to CCW still no luck
Thanks
Change this
for(double phi = -(Math.PI/2); phi <= Math.PI/2; phi+=dPhi)
to this
for(double phi = -(Math.PI); phi <= Math.PI; phi+=dPhi)
Actually, changing
for(double phi = -(Math.PI/2); phi <= Math.PI/2; phi+=dPhi)
to
for(double phi = -(Math.PI); phi <= 0; phi+=dPhi)
is enough. With phi from -(Math.PI) to +(Math.PI) you are making 360 degrees turn and counting each point twice. You can also do:
for(double phi = -(Math.PI); phi <= Math.PI; phi+=dPhi) {
for(double theta = 0.0; theta <= (Math.PI); theta+=dTheta) {
...
}
}
to avoid counting twice.