Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Camera rotate problem

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    10

    Camera rotate problem

    First, sorry for my bad English.

    I am developing my final project do college, a volumetric viewer for Android, and i have de a problem.
    The first visualization is fine, but when i rotate the camera, the old visualization is drawed, and the new, with the new angle of camera, too.

    Here is the link os the pictures demonstrating it:
    https://dl.dropboxusercontent.com/u/...%3A29%3A18.png
    https://dl.dropboxusercontent.com/u/...%3A29%3A27.png

    and here is the code:

    Code :
     
    public class ViewerRenderer implements Renderer {
     
    	private Square square;
    	private Camera camera;
     
    	private float vertices[] = {
    			-1.0f, -1.0f,  0.0f,        // V1 - bottom left
    			-1.0f,  1.0f,  0.0f,        // V2 - top left
    			1.0f, -1.0f,  0.0f,        // V3 - bottom right
    			1.0f,  1.0f,  0.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)
    	};
     
    	private FloatBuffer vertexBuffer;
     
    	private boolean cameraChanged = true;
     
    	public ViewerRenderer(Square square, Camera camera) {
    		this.square = square;
     
    		this.camera = camera;
     
    		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);
    	}
     
    	public void onDrawFrame(GL10 gl) {
    		//gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
     
    		// Reset the Modelview Matrix
    		gl.glLoadIdentity();
     
    		if(cameraChanged) {
    			//square.drawBackground(gl);
     
    			GLU.gluLookAt(gl, camera.getEyeX(), camera.getEyeY(), camera.getEyeZ(), 
    					camera.getCenterX(), camera.getCenterY(), camera.getCenterZ(),
    					camera.getUpX(), camera.getUpY(), camera.getUpZ());
     
    			//cameraChanged = false;
    		}
     
    		square.draw(gl);
    	}
     
    	public void onSurfaceChanged(GL10 gl, int width, int height) {
    		if (height == 0) {
    			height = 1;
    		}
     
    		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
     
    		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
    				100.0f);
     
    		gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
    		gl.glLoadIdentity();
    	}
     
    	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    		square.loadGLTextures(gl);
     
    		gl.glEnable(GL10.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
    		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
    		gl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testing
    		gl.glDepthFunc(GL10.GL_LEQUAL); // The Type Of Depth Testing To Do
     
    		// Really Nice Perspective Calculations
    		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    	}
     
    	public Camera getCamera() {
    		return camera;
    	}
     
    	public void setCamera(Camera camera) {
    		this.camera = camera;
    		cameraChanged = true;
    	}
    }
     
    public class Square {
     
    	private FloatBuffer vertexBuffer;
    	//private int[] textures = new int[1];
    	private int[] textures;
     
    	private List<Bitmap> bitmaps;
     
    	private float vertices[] = {
    			-1.0f, -1.0f,  0.0f,        // V1 - bottom left
    			-1.0f,  1.0f,  0.0f,        // V2 - top left
    			1.0f, -1.0f,  0.0f,        // V3 - bottom right
    			1.0f,  1.0f,  0.0f         // V4 - top right
    		};
     
    	private byte indices[] = {
    			//Faces definition
        		0,1,3, 0,3,2, 			//Face front
        		4,5,7, 4,7,6, 			//Face right
        		8,9,11, 8,11,10, 		//... 
        		12,13,15, 12,15,14, 	
        		16,17,19, 16,19,18, 	
        		20,21,23, 20,23,22, 	
    								};
     
    	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)
    	};
    	private ByteBuffer indexBuffer;
     
    	public Square(List<Bitmap> bitmaps) {		
    		textures = new int[bitmaps.size()];
    		this.bitmaps = new ArrayList<Bitmap>();
    		//usado para pintar da ultima imagem para a primeira
    		for (int i = 0; i < bitmaps.size(); i++) {
    			this.bitmaps.add(bitmaps.get(bitmaps.size()-(i+1)));
    		}
     
    		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);
     
    		indexBuffer = ByteBuffer.allocateDirect(indices.length);
    		indexBuffer.put(indices);
    		indexBuffer.position(0);
    	}
     
    	public void draw(GL10 gl) {
    		gl.glMatrixMode(GL10.GL_MODELVIEW);
     
    		/*gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    		gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
    		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
    		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);*/
     
    		float z = 0.0078125f;
     
    		gl.glEnable(GL10.GL_TEXTURE_2D);
     
    		gl.glDepthMask(false);
    		gl.glEnable(GL10.GL_BLEND);
    		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
     
    		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
     
    		for (int i = 0; i < textures.length; i++) {
    			gl.glTranslatef(0.0f, 0.0f, z);
     
    			// bind the previously generated texture
    			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);
     
    			// 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);
     
    			//y += inc;
    		}
     
    		gl.glDisable(GL10.GL_BLEND);
    		gl.glDepthMask(true);
     
    		gl.glDisable(GL10.GL_TEXTURE_2D);
     
    		// Disable the client state before leaving
    		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    	}
     
    	public void loadGLTextures(GL10 gl) {
    		Bitmap bitmap = null;
    		gl.glGenTextures(textures.length, textures, 0);
     
    		for (int i = 0; i < textures.length; i++) {
    			// generate one texture pointer
     
    			// ...and bind it to our array
    			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);
     
    			// 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);
     
    			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
     
    			// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
    			bitmap = bitmaps.get(i);
    			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
    			gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
    		}
     
    		// Clean up
    		//bitmap.recycle();
    	}
     
    }

    What i doing wrong?

  2. #2
    Intern Contributor
    Join Date
    Jul 2013
    Posts
    65
    maybe something like
    glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    in the start of your draw function?

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    10
    Only here:

    Code :
    public void onDrawFrame(GL10 gl) {
    		//gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

  4. #4
    Intern Contributor
    Join Date
    Jul 2013
    Posts
    65
    ups,didn't see that.
    Your still not using glClearColor(..), you commemnted it out.
    It may be needed to overwrite the previous screen.
    But ofcourse .. you must have tried it by now..

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    10
    I've tested it with this snippet of code uncommented, and continued working there.

  6. #6
    Intern Contributor
    Join Date
    Jul 2013
    Posts
    65
    Judged by the photos you link to, the frameDraw event will be called at the time you release the mouse-pointer from the ruler. That is .. the screen has been drawn twice only? I'm thinking on performance from this perspective: Is the type 'GL10' a rendercontext or eqvivalent mayor composit? Wouldn't it be copied if you don't involve pointers & address-of operators, that is: 'sending' the argument by reference.
    It's a silly question that I'm far too inexperienced in addressing, but if the whold context has to be copied you probably should expect some sort of unexpected thing to happen.
    ... am I way off?

  7. #7
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    10
    I moved the code of square#draw method to viewer_renderer#onDrawFrame, and the problem persists.

  8. #8
    Junior Member Newbie
    Join Date
    Sep 2008
    Posts
    19
    Re: "What i doing wrong?"
    Don't know yet:

    But: how many textures are you loading? i.e is textures.length > 1? could be the problem?
    You have the rendering of the geometry (unnecessarily) dependent on the number of textures you have loaded. It could be a problem later(if not already)Either way, good idea to change it:
    Suppose you have two textures: you will have glTranslatef(0.0f, 0.0f, z) called after glLoadIdentity(); on the first glDrawArrays.
    Then glTranslatef(0.0f, 0.0f, z) after glTranslatef(0.0f, 0.0f, z) on the subsequent glDrawArrays calls in that loop you have.
    Last edited by Steven Katic; 11-12-2013 at 07:23 PM.

  9. #9
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    10
    I'm loading, in my test, 5 textures, but it's dynamic.
    It's not the best way to do this with many textures?

  10. #10
    Intern Contributor
    Join Date
    Jul 2013
    Posts
    65
    hi jonatasdaniel,
    I noticed that you are writing java-code .. it has another 'by-ref' than I used to. The way you do it is probably ok.
    I never (yet, but comming soon) did any drawing with textures. Running through your code it surprises me that you've got two calls in
    public void draw:
    .. gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
    and again further down iterating through your textures:
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

    would that be the right procedure?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •