JPCT-AE-Basic Structure

Source: Internet
Author: User
Tags gety

This piece of code I believe that people have downloaded JPCT-AE should have read, this article is reprinted, because I am also learning, the original author of several articles are very useful to me, so we recommend it to you.

Http://blog.csdn.net/simdanfeg/article/details/6393303

This example shows the specific implementation process of a cube, compared with the previous pure OpenGL ES implementation, it uses JPCT-AE to achieve, because I personally think this framework is very convenient, so today I will introduce the implementation of JPCT-AE through the Wiki on my website. This example gives you a quick start to the JPCT-AE help documentation.

 

(1) What is jpct: a 3D Game Engine encapsulated with OpenGL ES, which has j2se and Android versions.

(2) how to obtain its jar package and help documentation: http://download.csdn.net/user/simdanfeg download


Example 1: Different implementations of the same cube

 

Package com. threed. jpct. example;

Import java. Lang. Reflect. field;

Import javax. microedition. khronos. EGL. egl10;
Import javax. microedition. khronos. EGL. eglconfig;
Import javax. microedition. khronos. EGL. egldisplay;
Import javax. microedition. khronos. opengles. gl10;

Import Android. App. activity;
Import Android. OpenGL. glsurfaceview;
Import Android. OS. Bundle;
Import Android. View. motionevent;

Import com. threed. jpct. camera;
Import com. threed. jpct. framebuffer;
Import com. threed. jpct. light;
Import com. threed. jpct. Logger;
Import com. threed. jpct. object3d;
Import com. threed. jpct. primitives;
Import com. threed. jpct. rgbcolor;
Import com. threed. jpct. simplevector;
Import com. threed. jpct. texture;
Import com. threed. jpct. texturemanager;
Import com. threed. jpct. World;
Import com. threed. jpct. util. bitmaphelper;
Import com. threed. jpct. util. memoryhelper;

/**
* A simple example. It focuses more on displaying how to use the JPCT-AE 3D game framework than displaying how to write a correct Android app.
* It contains methods such as the activity class to process pause and resume.
*
* @ Author egonolsen
*
*/
Public class helloworld extends activity {

// The onpause and onresume methods used by the helloworld object to process the activity
Private Static helloworld master = NULL;

// Glsurfaceview object
Private glsurfaceview mglview;

// Class myrenderer object
Private myrenderer Renderer = NULL;

// When jpct renders the background, the framebuffer class provides a buffer. The result is essentially an image that can be displayed, modified, or even processed.
Private framebuffer Fb = NULL;

// The world class is the most important class in jpct, and it "sticks" things like glue. It contains objects and light that define jpct scenarios
Private world = NULL;

// Similar to the color class in Java. AWT. *
Private rgbcolor back = new rgbcolor (50, 50,100 );

Private float touchturn = 0;
Private float touchturnup = 0;

Private float xpos =-1;
Private float ypos =-1;

// The object3d class is a three-dimensional object. Do not think it is similar to Java. Lang. object.
// An object3d object is added to the rendered world object as an instance. Object3d in the world
// Once an instance is added, it may be associated as a child/parent to establish a system for them.
// The human body model can also be applied to the above rules. They are often not added to a world instance,
// Bind it to another object (human model or non-human model ). Some methods need an instance in this class.
// Add it to a world instance (which can be implemented using the world. addobject () method ).
Private object3d cube = NULL;

// Number of frames per second
Private int FPS = 0;

// Illumination
Private Light sun = NULL;

Protected void oncreate (bundle savedinstancestate ){
// A common log class in jpct In the logger class used to print and store messages, errors, and warnings.
// Messages generated by each jpct will be added to the queue of this class.
Logger. Log ("oncreate ");
// If this class object is not null, the class will be loaded from all the attributes of the object.
If (master! = NULL ){
Copy (master );
}

Super. oncreate (savedinstancestate );

// Instantiate glsurfaceview
Mglview = new glsurfaceview (this );
// Use the self-implemented eglconfigchooser, which must be before setrenderer (Renderer)
// If no seteglconfigchooser method is called, by default, the view selects a depth buffer of at least 16 bits compatible with the current Android. View. surface.
Mglview. seteglconfigchooser (New glsurfaceview. eglconfigchooser (){
Public eglconfig chooseconfig (egl10 EGL, egldisplay display ){
// Ensure that we get a 16bit framebuffer. Otherwise, we'll fall
// Back to pixelflinger on some device (read: Samsung i7500)
Int [] attributes = new int [] {egl10.egl _ depth_size, 16,
Egl10.egl _ None };
Eglconfig [] configs = new eglconfig [1];
Int [] result = new int [1];
EGL. eglchooseconfig (display, attributes, configs, 1, result );
Return configs [0];
}
});
// Instantiate myrenderer
Renderer = new myrenderer ();
// Set the view Renderer, start the thread to call rendering, and even start rendering.
Mglview. setrenderer (Renderer );
// Set a clear view
Setcontentview (mglview );
}

// Rewrite onpause ()
@ Override
Protected void onpause (){
Super. onpause ();
Mglview. onpause ();
}

// Rewrite onresume ()
@ Override
Protected void onresume (){
Super. onresume ();
Mglview. onresume ();
}

// Rewrite onstop ()
@ Override
Protected void onstop (){
Super. onstop ();
}

Private void copy (Object SRC ){
Try {
// Print logs
Logger. Log ("copying data from Master activity! ");
// Returns an array containing the filed object of all fields of the current class.
Field [] FS = SRC. getclass (). getdeclaredfields ();
// Traverse the FS Array
For (field F: FS ){
// Try to set the accessibility flag value. If the flag is set to false, access checks are performed. If the flag is set to true, access checks are disabled.
F. setaccessible (true );
// Load all obtained values into the current class
F. Set (this, F. Get (SRC ));
}
} Catch (exception e ){
// Throw a running exception
Throw new runtimeexception (E );
}
}

Public Boolean ontouchevent (motionevent me ){

// Start with the button
If (Me. getaction () = motionevent. action_down ){
// Save the initial X and Y position in xpos and ypos.
Xpos = me. getx ();
Ypos = me. Gety ();
Return true;
}
// Button ends
If (Me. getaction () = motionevent. action_up ){
// Set X, Y, and rotation angle to the initial value.
Xpos =-1;
Ypos =-1;
Touchturn = 0;
Touchturnup = 0;
Return true;
}

If (Me. getaction () = motionevent. action_move ){
// Calculate the offset position of X and Y and the rotation angle on the X and Y axes.
Float XD = me. getx ()-xpos;
Float YD = me. Gety ()-ypos;
// Logger. Log ("me. getx ()-xpos ----------->"
// + (Me. getx ()-xpos ));
Xpos = me. getx ();
Ypos = me. Gety ();
Logger. Log ("xpos ------------>" + xpos );
// Logger. Log ("ypos ------------>" + ypos );
// Take the X axis as an example. If the cursor is pulled from left to right, the cursor is pulled from right to left.
Touchturn = XD/-100f;
Touchturnup = YD/-100f;
Logger. Log ("touchturn ------------>" + touchturn );
// Logger. Log ("touchturnup ------------>" + touchturnup );
Return true;
}

// Sleep for milliseconds after each move
Try {
Thread. Sleep (15 );
} Catch (exception e ){
// No need for this...
}

Return super. ontouchevent (me );
}

// The myrenderer class implements the glsurfaceview. Renderer Interface
Class myrenderer implements glsurfaceview. Renderer {
// The number of milliseconds in the current system
Private long time = system. currenttimemillis ();
// Whether to stop
Private Boolean stop = false;

// Stop
Public void stop (){
Stop = true;
}

// When the screen changes
Public void onsurfacechanged (gl10 GL, int W, int h ){
// If framebuffer is not null, release resources occupied by FB
If (FB! = NULL ){
FB. Dispose ();
}
// Create a framebuffer whose width is W and height is H
Fb = new framebuffer (GL, W, H );
Logger. Log (Master + "");
// If the master is empty
If (Master = NULL ){

// Instantiate the world object
World = New World ();

// Set the intensity of the ambient light source. If this value is set to negative, the entire scenario is dimmed, and if it is positive, everything is illuminated.
World. setambientlight (20, 20, 20 );

// Create a new light source in world
Sun = new light (World );

// Set the illumination intensity
Sun. setintensity (250,250,250 );

// Create a texture
// Constructor texture (bitmap image)
// Static bitmap rescale (Bitmap bitmap, int width, int height)
// Static bitmap convert (drawable)
Texture texture = new texture (bitmaphelper. rescale (
Bitmaphelper. Convert (getresources (). getdrawable (
R. drawable. Glass), 64, 64 ));

// Texturemanager. getinstance () gets a texturemanager object
// Addtexture ("texture", texture) adds a texture
Texturemanager. getinstance (). addtexture ("texture", texture );

// The object3d object starts :-)

// Primitives provides some basic 3D objects. If you generate some objects for testing or
// It is wise to use these classes for other purposes because they are fast and simple and do not need to be loaded or edited.
// Call public static object3d getcube (float scale) scale: Angle
// Return a cube
Cube = primitives. getcube (10 );

// Texture the texture on all the faces of the object.
Cube. calctexturewrapspherical ();

// Set the texture for the object
Cube. settexture ("texture ");

// Unless you want to use polygonmanager later, the memory that no longer needs data will be released.
Cube. Strip ();

// Initialization of some basic objects is almost all the processes required for further processing.
// If the object is "ready for rendering" (loading, texture allocation, placement, rendering Mode settings,
// Animation and vertex controller allocation), so build () must be called,
Cube. Build ();

// Add the object3d object to the world set
World. addobject (cube );

// This camera represents the position and direction of camera/Viewer in the current scenario. It also contains information about the current field of view.
// You should remember that the rotation matrix of camera is actually a rotation matrix of objects applied in the world.
// This is very important. When the Rotation Angle of camera is selected, a camera (virtual) rotates around W and rotates around w,
// It will have the same effect. Therefore, considering the rotation angle, when world is centered around camera, the camera angle of view is static. If you do not like
// You can use the rotatecamera () method.
Camera cam = World. getcamera ();

// Move camera backward at a speed of 50 (relative to the current direction)
Cam. movecamera (camera. camera_moveout, 50 );

// Cub. gettransformedcenter () returns the center of the object
// Cam. lookat (simplevector lookat ))
// Rotate the camera so that it looks at the given location of the world-space.
Cam. lookat (cube. gettransformedcenter ());

// Simplevector is a basic class that represents three-dimensional vectors.
// It is composed of simplevector or at least one simplevector variant (sometimes due
// For some reasons, such as performance may be used (float X, float y, float z ).
Simplevector SV = new simplevector ();

// Set the X, Y, and Z values of the current simplevector to the value of the given simplevector (cube. gettransformedcenter ().
Sv. Set (cube. gettransformedcenter ());

// Subtract 100 from the y Direction
Sv. Y-= 100;

// Subtract 100 from the z direction
Sv. Z-= 100;

// Set the Light Source Position
Sun. setposition (SV );

// Force GC and finalization to try to release some memory and write the memory into logs at the same time,
// This can avoid animation inconsistencies. However, it only reduces the chance of this situation.
Memoryhelper. Compact ();

// If the master is empty, use logging and set the master to helloworld.
If (Master = NULL ){
Logger. Log ("saving Master activity! ");
Master = helloworld. This;
}
}
}

// Onsurfacecreated (gl10 GL, eglconfig config) to be implemented)
Public void onsurfacecreated (gl10 GL, eglconfig config ){
}

// Draw to the current screen:-d
Public void ondrawframe (gl10 GL ){

Try {
// If stop is true
If (! Stop ){
// If the touchturn value is not 0, rotate the touchture angle to the Y axis.
If (touchturn! = 0 ){
// The rotation of an object around Y is applied to the next rendering of the object from the given matrix W-axis angle (positive in the clockwise direction of radians.
Cube. rotatey (touchturn );
// Set touchturn to 0
Touchturn = 0;
}

If (touchturnup! = 0 ){
// Rotate the axis matrix of an object around X, which is applied to the next rendering of the object from the given angle width (radians, counterclockwise positive values.
Cube. rotatex (touchturnup );
// Set touchtureup to 0
Touchturnup = 0;
}

// Clear framebuffer with the given color (back)
FB. Clear (back );
// Transform and light all Polygon
World. renderscene (FB );
// Draw
World. Draw (FB );
// Render Image Display
FB. Display ();

// Record FPS
If (system. currenttimemillis ()-time> = 1000 ){
// Logger. Log (FPS + "FPS ");
FPS = 0;
Time = system. currenttimemillis ();
}
FPS ++;

// Release framebuffer if stop is false
} Else {
If (FB! = NULL ){
FB. Dispose ();
Fb = NULL;
}
}
// Print the exception information when an exception occurs.
} Catch (exception e ){
Logger. Log (E, logger. Message );
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.