+ All Categories
Home > Documents > CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity...

CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity...

Date post: 24-Sep-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
18
CS482 Lab Session 2 HelloOpenGL3D 2019. 9. 19
Transcript
Page 1: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

CS482 Lab Session 2HelloOpenGL3D

2019. 9. 19

Page 2: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

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

Page 3: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

Structure

3

Page 4: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

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

Page 5: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

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

Page 6: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

MainActivity

6

OnCreate()OnStart()OnResume()

OnPause() OnResume() OnPause()OnStop()

Page 7: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

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);

Page 8: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

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) {

Page 9: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

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");

Page 10: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

Implementation

10

Page 11: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

MainActivity

11

Page 12: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

MyGLSurfaceView

12

Page 13: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

MyGLRenderer

13

Page 14: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

MyGLRenderer

14

Page 15: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

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)

Page 16: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

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

Page 17: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

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);

Page 18: CS482 Lab Session 2 - KAISTvclab.kaist.ac.kr/cs482/cs482_lab_02_hellowopengl3d.pdf · MainActivity • In Android, an activity represents a single screen with a user interface. •

No More Hints

18


Recommended