CS482 Lab Session 2 HelloOpenGL3D 2019. 9. 19
CS482 Lab Session 2HelloOpenGL3D
2019. 9. 19
HelloOpengl3D• Fork or download ‘HelloOpenGL3D’
https://github.com/KAIST-VCLAB/cs482-2019-master
• Run the application• HelloOpenGL3D
– 3 buttons (world/cube1/cube2)– a cube– a square
2
Structure
3
HelloOpengl3D
• MainActivity class– configures layout and view
• MyGLSurfaceView class– renderer manager– handles touch event
• MyGLRenderer class– responsible for making OpenGL calls to
render a frame
• Cube class– stores vertex position and normal
• Square class– stores vertex position and normal
MainActivity
MyGLSurfaceView
MyGLRenderer
Cube
Square
MainActivity• In Android, an activity represents a single screen with a user interface.
• onCreate()– When an activity begins, onCreate() is called once.– Put all initialization codes in onCreate().
• onResume()– onResume() is called right before the activity shows up.– Put codes for getting ready here. (e.g., re-allocating)
• onPause()– onPause() is called when another activity comes into the foreground.– Put codes for relaxing here. (e.g., de-allocating)
• For more information:– https://developer.android.com/training/basics/activity-lifecycle/starting.html– https://developer.android.com/reference/android/app/Activity.html– http://www.tutorialspoint.com/android/android_acitivities.htm
MainActivity
6
OnCreate()OnStart()OnResume()
OnPause() OnResume() OnPause()OnStop()
MyGLSurfaceView• A subclass of ‘GLSurfaceView’
• GLSurfaceView– For displaying OpenGL rendering.– A GLSurfaceView provides the following features:
• Manages a surface, which is a special piece of memory that can be composited into the Android view system.
• Manages an EGL display, which enables OpenGL to render into a surface.• Accepts a user-provided Renderer object that does the actual rendering.• Renders on a dedicated thread to decouple rendering performance from
the UI thread.• Supports both on-demand and continuous rendering.• Optionally wraps, traces, and/or error-checks the renderer's OpenGL calls.
• onTouchEvent() handles touch events from user
7
mRenderer = new MyGLRenderer();setRenderer(mRenderer);
MyGLRenderer• A subclass of ‘GLRenderer’
• GLRenderer– A generic renderer interface.– The renderer is responsible for making OpenGL
calls to render a frame.– GLSurfaceView clients typically create their own
classes that implement this interface, and then call setRenderer() to register the renderer with the GLSurfaceView.
8
@Overridepublic void onDrawFrame(GL10 unused) {
Cube, Square• Custom classes for 3D objects.
• Store 3D positions and normal vectors of vertices.• Attach shaders– add the vertex shader and face shader from files
(.vshader, .fshader)
• Do routines for OpenGL rendering
9
Cube
Square
int vertexShader = MyGLRenderer.loadShaderFromFile(GLES20.GL_VERTEX_SHADER, "basic-gl2.vshader");
int fragmentShader = MyGLRenderer.loadShaderFromFile(GLES20.GL_FRAGMENT_SHADER, "diffuse-gl2.fshader");
Implementation
10
MainActivity
11
MyGLSurfaceView
12
MyGLRenderer
13
MyGLRenderer
14
Task• Add a new Cube next to the old one• Implement rotation/translation w.r.t.
– World– Cube1– Cube2
• Rotation by touching with a finger• Translation by touching with two fingers
15
Add a cube Rotate (world) Rotate (cube1) Translation (cube1)
Hints• You only need to modify..– onTouchEvent(..) in MyGLSurfaceView.java– onDrawFrame(..) in MyGLRenderer.java– onSurfaceCreated(..) in MyGLRenderer.java
• Try to make use of the matrix library.– Matrix.setIdentityM(..)– Matrix.translateM(..)– Matrix.rotateM(..)– Matrix.multiplyMM(..)
16
More Hints• You have to modify model-view matrices that are used when yo
u draw a square and cubes.• Modify them using dx and dy from onTouchEvent(..) function.
17
// DrawmSquare.draw(mProjMatrix, mSquareModelViewMatrix, mSquareNormalMatrix, mLight, mLight2);mCube.draw(mProjMatrix, mCubeModelViewMatrix, mCubeNormalMatrix, mLight, mLight2);
No More Hints
18