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.
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.
publicclassMainActivityextendsAppCompatActivityimplementsSmartGLViewController {
privateSmartGLViewmSmartGLView;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
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(...). Note that you have to enable the touch in the view, by code or in the XML:
<fr.arnaudguyon.smartgl.opengl.SmartGLView
...
android:clickable="true"/>Here are the callbacks:
publicclassMainActivityextendsAppCompatActivityimplementsSmartGLViewController {
@OverridepublicvoidonPrepareView(SmartGLViewsmartGLView) {
}
@OverridepublicvoidonReleaseView(SmartGLViewsmartGLView) {
}
@OverridepublicvoidonResizeView(SmartGLViewsmartGLView) {
}
@OverridepublicvoidonTick(SmartGLViewsmartGLView) {
}
@OverridepublicvoidonTouchEvent(SmartGLViewsmartGLView, TouchHelperEventevent) {
}
}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.
publicclassMainActivityextendsAppCompatActivityimplementsSmartGLViewController {
@OverrideprotectedvoidonPause() {
if (mSmartGLView != null) {
mSmartGLView.onPause();
}
super.onPause();
}
@OverrideprotectedvoidonResume() {
super.onResume();
if (mSmartGLView != null) {
mSmartGLView.onResume();
}
}
}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 which size is limited.
publicclassMainActivityextendsAppCompatActivityimplementsSmartGLViewController {
privateTexturemSpriteTexture;
@OverridepublicvoidonPrepareView(SmartGLViewsmartGLView) {
SmartGLRendererrenderer = smartGLView.getSmartGLRenderer();
RenderPassSpriterenderPassSprite = newRenderPassSprite();
renderer.addRenderPass(renderPassSprite); // add it only once for all Sprites// the drawable must be a picture (jpg or png), not a vector (xml)mSpriteTexture = newTexture(context, R.drawable.planet);
mSprite = newSprite(120, 120); // 120 x 120 pixelsmSprite.setPivot(0.5f, 0.5f); // set position / rotation axis to the middle of the spritemSprite.setPos(60, 60);
mSprite.setTexture(mSpriteTexture);
renderPassSprite.addSprite(mSprite);
}
@OverridepublicvoidonReleaseView(SmartGLViewsmartGLView) {
if (mSpriteTexture != null) {
mSpriteTexture.release();
mSpriteTexture = null;
}
}
}Congratulations! The Sprite is displayed on your screen!
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.
publicclassMainActivityextendsAppCompatActivityimplementsSmartGLViewController {
@OverridepublicvoidonTick(SmartGLViewsmartGLView) {
SmartGLRendererrenderer = smartGLView.getSmartGLRenderer();
floatframeDuration = renderer.getFrameDuration();
if (mSprite != null) {
floatnewX = mSprite.getPosX() + (frameDuration * 100);
floatnewY = mSprite.getPosY();
if (newX > 600) {
newX = 0;
}
mSprite.setPos(newX, newY);
floatnewRot = mSprite.getRotation() + (frameDuration * 100);
mSprite.setRotation(newRot);
}
}Your Sprite is now moving and rotating on the screen!
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.
publicclassMainActivityextendsAppCompatActivityimplementsSmartGLViewController {
privateObject3DmSpaceship;
privateTexturemShipTexture;
@OverridepublicvoidonPrepareView(SmartGLViewsmartGLView) {
// ...RenderPassObject3DrenderPassObject3D = newRenderPassObject3D();
renderer.addRenderPass(renderPassObject3D); // add it only once for all 3D Objects// the drawable must be a picture (jpg or png), not a vector (xml)mShipTexture = newTexture(context, R.drawable.ship_picture);
WavefrontModelmodel = newWavefrontModel.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 smallmSpaceship.setPos(0, 0, -5); // move the object in front of the camerarenderPassObject3D.addObject(mSpaceship);
}
@OverridepublicvoidonTick(SmartGLViewsmartGLView) {
// ...// add some rotation movementif (mSpaceship != null) {
floatrx = mSpaceship.getRotX() + 100*frameDuration;
floatry = mSpaceship.getRotY() + 77*frameDuration;
floatrz = mSpaceship.getRotZ() + 56*frameDuration;
mSpaceship.setRotation(rx, ry, rz);
}
@OverridepublicvoidonReleaseView(SmartGLViewsmartGLView) {
if (mShipTexture != null) {
mShipTexture.release();
mShipTexture = null;
}
}
}
}Congratulations! You know how to display and move Sprites and 3D Objects!
Note that if your Model does not have a texture but colors on each vertex, you'll have to use a specific "color" shader instead of the default one:
newRenderPassObject3D(RenderPassObject3D.ShaderType.SHADER_COLOR, true, true);
Also if there are no textures and no colors on vertex, you can specify a default color in the WavefrontModel.Builder using:
builder.setColor(0.5f, 0.3, 0.8f); // RGBRead the ADVANCED DOCUMENTATION ON WIKI
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:
- ZenDay, a 3D Calendar and Task manager
- Slippy Slope, a funny game which uses a lot 2D Sprites
- Bulldozer Rampage, a race game which uses 3D objects
- Time Shift a demo that combines 2D and 3D effects
Add the following maven{} line to your PROJECT build.gradle file
allprojects {
repositories {
mavenCentral()
maven { url "https://jitpack.io" } // add this line
}
}
Add the libary dependency to your APP module build.gradle file
dependencies {
implementation 'com.github.smart-fun:smartGL:1.2.9' // add this line
}
Copyright 2016-2025 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.



