smartGL for Android


Source link: https://github.com/smart-fun/smartGL

SmartGL for Android

SmartGL is a Graphic Engine for creating Android Games and Apps. It is based on OpenGL and handles 2D Sprites and 3D Textured Objects.

SmartGL is used by several games and apps.

How to use

The scene is displayed inside a SmartGLView.

A SmartGLViewController interface is used to handle the scene (like adding objects to the scene).

A SmartGLRenderer is used to render the scene in the view.

 <fr.arnaudguyon.smartgl.opengl.SmartGLView

android:id="@+id/smartGLView"

android:layout_width="match_parent"

android:layout_height="match_parent" />

You can create an instance of SmartGLViewController or just implement it in your Activity or Fragment.

public class MainActivity extends AppCompatActivity implements SmartGLViewController {

private SmartGLView mSmartGLView;

@Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 setContentView(R.layout.activity_main);

 mSmartGLView = (SmartGLView) findViewById(R.id.smartGLView);

mSmartGLView.setDefaultRenderer(this);

mSmartGLView.setController(this);

  
}
 
}

When the SmartGLView is ready, onPrepareView(...) is called on the SmartGLViewController. This method is the best place to prepare the scene (load textures, and add objects to the scene).

When the SmartGLView is dismissed, onReleaseView(...) is called. This is the best place to release content from memory (textures must be released).

If the View is resized for any reason (rotation of the device for example), onResizeView(...) is called.

At every OpenGL frame, onTick(...) is called.

Finally, every touch interaction calls onTouchEvent(...)

public class MainActivity extends AppCompatActivity implements SmartGLViewController {

@Override
  public void onPrepareView(SmartGLView smartGLView) {

  
}

@Override
  public void onReleaseView(SmartGLView smartGLView) {

  
}

@Override
  public void onResizeView(SmartGLView smartGLView) {

  
}

@Override
  public void onTick(SmartGLView smartGLView) {

  
}

@Override
  public void onTouchEvent(SmartGLView smartGLView, TouchHelperEvent event) {

  
}
 
}

LifeCycle

It is really important to inform the SmartGLView when the Activity or Fragment is paused or resumed. If you miss this step the scene will not be initialized or restored correctly.

public class MainActivity extends AppCompatActivity implements SmartGLViewController {

 @Override
  protected void onPause() {

if (mSmartGLView != null) {

 mSmartGLView.onPause();

}

super.onPause();

  
}

@Override
  protected void onResume() {

super.onResume();

if (mSmartGLView != null) {

 mSmartGLView.onResume();

}

  
}
 
}

Playing with the Sprites

The draw pipeline includes Render passes. For example you can have several 3D objects in the background pass, and several Sprites in the forground pass.

Let's start with a simple Sprite using a Texture (picture) loaded from the Android drawables. It is highly recommended to share the same texture between several Sprites or Objects 3D when possible: the textures are loaded into the GPU memory and it is often not that big.

public class MainActivity extends AppCompatActivity implements SmartGLViewController {

private Texture mSpriteTexture;

 @Override
  public void onPrepareView(SmartGLView smartGLView) {

 SmartGLRenderer renderer = smartGLView.getSmartGLRenderer();

RenderPassSprite renderPassSprite = new RenderPassSprite();

renderer.addRenderPass(renderPassSprite);
  // add it only once for all Sprites

 mSpriteTexture = new Texture(context, R.drawable.planet);

 mSprite = new Sprite(120, 120);
 // 120 x 120 pixels

mSprite.setPivot(0.5f, 0.5f);
  // set position / rotation axis to the middle of the sprite

mSprite.setPos(60, 60);

mSprite.setTexture(mSpriteTexture);

renderPassSprite.addSprite(mSprite);

  
}

@Override
  public void onReleaseView(SmartGLView smartGLView) {

if (mSpriteTexture != null) {

 mSpriteTexture.release();

}

  
}
  
}

Congratulations! The Sprite is displayed on your screen!

Add movement

Let's add some basic moves.

Framerate vary on devices. This is why you should always take the frame duration into account when moving elements on screen.

public class MainActivity extends AppCompatActivity implements SmartGLViewController {

@Override
  public void onTick(SmartGLView smartGLView) {

 SmartGLRenderer renderer = smartGLView.getSmartGLRenderer();

float frameDuration = renderer.getFrameDuration();

if (mSprite != null) {

 float newX = mSprite.getPosX() + (frameDuration * 100);

 float newY = mSprite.getPosY();

 if (newX > 600) {

  newX = 0;

 
}

 mSprite.setPos(newX, newY);

  float newRot = mSprite.getRotation() + (frameDuration * 100);

 mSprite.setRotation(newRot);

  
}
  
}

Your Sprite is now moving and rotating on the screen!

Playing with 3D Objects

Now let's add a moving 3D Object! There is a WavefrontModel class that can load a Wavefront model from an .obj file.

See eventually Wave Front format on Wikipedia for the file format definition.

A WavefrontModel.Builder is used to load the model and convert it into a Object3D. For each material used in the model, you will have to assign a corresponding Texture (see AddTexture() method).

There is no standard unity for the size of the models, so when you load different models you'll probably have to scale them so that they look the correct size compared to each other. You can use setScale() for that. The Camera is set at position (0,0,0) by default, and looking to -Z direction. If you want your objects visible for the camera, you need to put them in front of it, using something like setPos(0,0,-10). You can also move and rotate the camera if you want.

public class MainActivity extends AppCompatActivity implements SmartGLViewController {

private Object3D mSpaceship;
  private Texture mShipTexture;

@Override
  public void onPrepareView(SmartGLView smartGLView) {

 // ...

 RenderPassObject3D renderPassObject3D = new RenderPassObject3D();

renderer.addRenderPass(renderPassObject3D);
  // add it only once for all 3D Objects

mShipTexture = new Texture(context, R.drawable.ship_picture);

 WavefrontModel model = new WavefrontModel.Builder(context, R.raw.spaceship_obj)

  .addTexture("Material001", mShipTexture) // "Material001" is defined in the spaceship_obj file

  .create();

mSpaceship = model.toObject3D();

mSpaceship.setScale(0.1f, 0.1f, 0.1f);
  // Adjust the scale if object is too big / too small

mSpaceship.setPos(0, 0, -5);

// move the object in front of the camera

renderPassObject3D.addObject(mSpaceship);

  
}

@Override
  public void onTick(SmartGLView smartGLView) {

 // ...

 // add some rotation movement

if (mSpaceship != null) {

 float rx = mSpaceship.getRotX() + 100*frameDuration;

 float ry = mSpaceship.getRotY() + 77*frameDuration;

 float rz = mSpaceship.getRotZ() + 56*frameDuration;

 mSpaceship.setRotation(rx, ry, rz);

}

  @Override

public void onReleaseView(SmartGLView smartGLView) {

 if (mShipTexture != null) {

  mShipTexture.release();

 
}

}

  
}
  
}

Congratulations! You know how to display and move Sprites and 3D Objects!

Advanced use of SmartGL

Read the ADVANCED DOCUMENTATION ON WIKI

Samples

See the sample app for a complete implementation (download or clone the repository)

You can also have a look to these apps that use smartGL:

Installation with gradle

Add the following maven{ } line to your PROJECT build.gradle file

allprojects {

  repositories {

jcenter()

maven {
 url "https://jitpack.io" 
}

 // add this line
  
}
 
}
 

Add the libary dependency to your APP build.gradle file

dependencies {

  compile 'com.github.smart-fun:smartGL:1.1.4'
 // add this line 
}
 

License

Copyright 2016 Arnaud Guyon

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Resources

Bash script to simplify adb command usage.

This is a helpful bash utility for working with multiple devices at the same time.

Wrapper for Android Preferences which provides a fluid syntax.

Example:

Prefs.with(context).save(key, value);

Library that allows you to customize the font of a PagerTitleStrip.

This library provides a circular button that looks like the new button shown in the Google+ app for Android.

Drawable animation inspired by Tinder.

Interpolation methods:

  • LinearInterpolator
  • BounceInterpolator
  • CycleInterpolator

Android Signature Pad is an Android library for drawing smooth signatures. It uses variable width Bézier curve interpolation.

Features:

  • Bézier implementation for a smoother line
  • Variable point size based on velocity
  • Customizable pen color and size

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes