Renderer – 渲染器
Triangle – 三角形(需要度娘了解一下为什么3D模型是用很多三角形拼接成的)
Shader – 着色器
mesh – 网格?
Matrix – 矩阵
vertex – (三角形或锥形的)角顶
Texture – 纹理
OCTREE – 八叉树,用于3D空间分割,3D空间冲突检测
cube – 立方体
Renderer – 渲染器
Triangle – 三角形(需要度娘了解一下为什么3D模型是用很多三角形拼接成的)
Shader – 着色器
mesh – 网格?
Matrix – 矩阵
vertex – (三角形或锥形的)角顶
Texture – 纹理
OCTREE – 八叉树,用于3D空间分割,3D空间冲突检测
cube – 立方体
1 2 3 4 |
public Object3D(float[] coordinates, float[] uvs, int[] indices, int textureId) |
coordinates
– the coordinates [x1,y1,z1,x2,y2,z2,…]uvs
– the texture coordinates [u1,v1,u2,v2,…]indices
– the indices indexing the tuples/triples in the coordinate arraystextureId
– the texture id. If not known, just use TextureManager.TEXTURE_NOTFOUND
1 2 3 4 5 |
public Object3D(float[] coordinates, float[] normals, float[] uvs, int[] indices, int textureId) |
coordinates
– the coordinates [x1,y1,z1,x2,y2,z2,…]normals
– the vertex normals [nx1,ny1,nz1,nx2,ny2,nz2,…]uvs
– the texture coordinates [u1,v1,u2,v2,…]indices
– the indices indexing the tuples/triples in the coordinate arraystextureId
– the texture id. If not known, just use TextureManager.TEXTURE_NOTFOUND
1 |
public Object3D(int maxTriangles) |
1 |
public Object3D(Object3D obj) |
obj
– the Object3D to construct this Object3D from
1 2 |
public Object3D(Object3D obj, boolean reuseMesh) |
obj
– the Object3D to construct this Object3D fromreuseMesh
– if true, the new object will use the same mesh
1 |
public static Object3D createDummyObj() |
1 |
public void setVirtualizer(Virtualizer virtualizer) |
virtualizer
– the virtualizer
1 |
public Virtualizer getVirtualizer() |
1 |
public void setUserObject(java.lang.Object obj) |
obj
– Object whatever you like…
1 |
public void setSortOffset(float offset) |
offset
– the offset
1 2 |
public static Object3D mergeObjects(Object3D first, Object3D second) |
first
– the first object to mergesecond
– the second object to merge
1 |
public static Object3D mergeAll(Object3D... objs) |
objs
– the objects
1 |
public static void resetNextID() |
1 |
public static int getNextID() |
1 |
public static void setNextID(int next) |
next
– the next ID
1 |
public void compile() |
1 |
public void compile(boolean dynamic) |
dynamic
– if the mesh dynamic?
1 2 |
public void compile(boolean dynamic, boolean staticUV) |
dynamic
– if the mesh dynamic?staticUV
– does it use static uv-coordinates
1 |
public void shareCompiledData(<a title="class in com.threed.jpct" href="https://www.jpct.net/jpct-ae/doc/com/threed/jpct/Object3D.html">Object3D</a> withObj) |
withObj
– the object to share data with
1 |
public void touch() |
1 |
public void strip() |
1 |
public void forceGeometryIndices(boolean force) |
force
– should we?
1 |
public void setAnimationSequence(Animation anim) |
anim
– the animation sequenceAnimation
, getMesh()
1 |
public void clearAnimation() |
1 |
public Animation getAnimationSequence() |
1 2 |
public void animate(float index, int seq) |
index
– the “time-index” into the sub-sequence of the animationseq
– the number of the sub.sequence (sub-sequence start at 1, 0 stands for the animation as a whole)
1 |
public void animate(float index) |
index
– the “time-index” into the animation
1 |
public void setCollisionMode(int mode) |
mode
– The desired mode (COLLISION_CHECK_NONE, COLLISION_CHECK_OTHERS, COLLISION_CHECK_SELF or combinations)COLLISION_CHECK_NONE
, COLLISION_CHECK_OTHERS
, COLLISION_CHECK_SELF
1 |
public void setCollisionOptimization(boolean optimized) |
optimized
– should the collision detection be optimized or notCOLLISION_DETECTION_OPTIMIZED
, COLLISION_DETECTION_NOT_OPTIMIZED
1 |
public void setVisibility(boolean mode) |
mode
– visible or notOBJ_VISIBLE
, OBJ_INVISIBLE
1 |
public void calcBoundingBox() |
build()
1 |
public void build() |
World.buildAllObjects()
1 |
public void build(boolean staticUV) |
build()
1 |
public void disableVertexSharing() |
1 |
public boolean hasChild(Object3D obj) |
1 |
public boolean hasParent(Object3D obj) |
1 |
public void addChild(Object3D obj) |
1 |
public void removeChild(Object3D obj) |
1 |
public void removeParent(Object3D obj) |
obj
– the Object3D to remove
1 |
public void addParent(Object3D obj) |
obj
– the object that should become a parent of this.
1 |
public Object3D[] getParents() |
1 |
public int getID() |
resetNextID()
, World.addObject(com.threed.jpct.Object3D)
1 |
public void setName(java.lang.String n) |
1 |
public boolean wasVisible() |
1 |
public void setCulling(boolean mode) |
1 |
public void setShadingMode(int mode) |
mode
– the shading modeSHADING_GOURAUD
, SHADING_FAKED_FLAT
1 |
public void setLighting(int mode) |
mode
– the lighting modeLIGHTING_ALL_ENABLED
, LIGHTING_NO_LIGHTS
1 |
public int getLighting() |
LIGHTING_ALL_ENABLED
, LIGHTING_NO_LIGHTS
1 |
public void setMaxLights(int lightCount) |
lightCount
– a value between 0 and 8
1 |
public int getMaxLights() |
1 |
public void setSpecularLighting(boolean mode) |
mode
– the specular modeSPECULAR_ENABLED
, SPECULAR_DISABLED
1 |
public boolean getSpecularLighting() |
SPECULAR_ENABLED
, SPECULAR_DISABLED
1 |
public void setTransparency(int trans) |
trans
– the tranparency level. 0 is the highest possible transparency, a value below 0 will disable tranparency for this object
1 |
public void setShader(GLSLShader shader) |
shader
– the shader
1 |
public GLSLShader getShader() |
1 |
public void calcTangentVectors() |
1 |
public void clearShader() |
1 |
public int getTransparency() |
1 |
public boolean isTransparent() |
1 |
public void setTransparencyMode(int mode) |
mode
– the modeTRANSPARENCY_MODE_DEFAULT
, TRANSPARENCY_MODE_ADD
1 |
public int getTransparencyMode() |
TRANSPARENCY_MODE_DEFAULT
, TRANSPARENCY_MODE_ADD
1 |
public boolean hasVertexAlpha() |
1 |
public void setAlphaWrites(boolean alphaWrites) |
alphaWrites
– do alpha writes or not
1 |
public boolean isAlphaWrites() |
1 |
public void setAdditionalColor(RGBColor col) |
col
– the color
1 2 3 |
public void setAdditionalColor(int red, int green, int blue) |
red
– the red component (0..255)green
– the green component (0..255)blue
– the blue component (0..255)
1 |
public RGBColor getAdditionalColor() |
1 |
public void clearAdditionalColor() |
1 |
public void clearObject() |
cloneObject()
, decoupleMesh()
1 |
public void decoupleMesh() |
cloneObject()
, clearObject()
1 |
public void setBillboarding(boolean mode) |
1 |
public boolean isEnvmapped() |
ENVMAP_ENABLED
, ENVMAP_DISABLED
1 |
public void setEnvmapped(boolean mode) |
mode
– the environment mapping modeENVMAP_ENABLED
, ENVMAP_DISABLED
1 |
public void setFixedPointMode(boolean useFixedPoint) |
useFixedPoint
– use it?
1 |
public void rotateX(float w) |
w
– 旋转角度,弧度值,逆时针为正,顺时针为负。一周的弧度数为2πr/r=2π,360°角=2π弧度,因此,1弧度约为57.3°,即57°17’44.806”
1 |
public void rotateY(float w) |
w
– 旋转角度,弧度值,逆时针为正,顺时针为负。一周的弧度数为2πr/r=2π,360°角=2π弧度,因此,1弧度约为57.3°,即57°17’44.806”
1 |
public void rotateZ(float w) |
w
– 旋转角度,弧度值,逆时针为正,顺时针为负。一周的弧度数为2πr/r=2π,360°角=2π弧度,因此,1弧度约为57.3°,即57°17’44.806”
1 2 |
public void rotateAxis(SimpleVector axis, float angle) |
axis
– a direction-vector pointing into the axis direction with the object’s rotation pivot as position vectorangle
– the angle of the rotation
1 |
public void translateMesh() |
1 |
public void translate(SimpleVector trans) |
trans
– the translation vector
1 2 3 |
public void translate(float x, float y, float z) |
x
– the number of units the object should be translated parallel to the x axisy
– the number of units the object should be translated parallel to the y axisz
– the number of units the object should be translated parallel to the z axis
1 |
public void align(Camera camera) |
camera
– the Camera the Object3D should be aligned with
1 |
public void align(Object3D object) |
object
– the object this object should be aligned with
1 2 |
public void setOrientation(SimpleVector dir, SimpleVector up) |
dir
– the directionup
– the up-vector
1 |
public void enableLazyTransformations() |
disableLazyTransformations()
1 |
public void disableLazyTransformations() |
enableLazyTransformations()
1 |
public void scale(float scale) |
scale
– the new scalesetScale(float)
1 |
public void setScale(float absScale) |
absScale
– the new (absolute) scalescale(float)
1 |
public float getScale() |
1 |
public SimpleVector getTranslation() |
1 |
public SimpleVector getTranslation(SimpleVector trns) |
trns
– the SimpleVector to fill and return
1 |
public SimpleVector getOrigin() |
1 |
public SimpleVector getXAxis() |
1 |
public SimpleVector getYAxis() |
1 |
public SimpleVector getZAxis() |
1 |
public SimpleVector getXAxis(SimpleVector toFill) |
toFill
– the vector to fill
1 |
public SimpleVector getYAxis(SimpleVector toFill) |
toFill
– the vector to fill
1 |
public SimpleVector getZAxis(SimpleVector toFill) |
toFill
– the vector to fill
1 |
public Matrix getRotationMatrix() |
1 |
public Matrix getTranslationMatrix() |
1 |
public Matrix getOriginMatrix() |
translate(float,float,float)
, setOrigin(com.threed.jpct.SimpleVector)
1 |
public int getLightCount() |
1 |
public void setRotationMatrix(Matrix mat) |
mat
– the new rotation matrixrotateX(float)
, rotateY(float)
, rotateZ(float)
1 |
public void clearRotation() |
1 |
public void clearTranslation() |
1 |
public void setTextureMatrix(Matrix mat) |
mat
– the matrix
1 |
public Matrix getTextureMatrix() |
1 |
public void rotateMesh() |
1 |
public void setTranslationMatrix(Matrix mat) |
1 |
public void setMesh(<a title="class in com.threed.jpct" href="https://www.jpct.net/jpct-ae/doc/com/threed/jpct/Mesh.html">Mesh</a> mesh) |
mesh
– the new mesh of the objectgetMesh()
, calcBoundingBox()
1 |
public Mesh getMesh() |
Animation
, Mesh.cloneMesh(boolean)
, setMesh(com.threed.jpct.Mesh)
1 |
public PolygonManager getPolygonManager() |
strip()
1 2 3 4 5 6 |
public void setBoundingBox(float minx, float maxx, float miny, float maxy, float minz, float maxz) |
minx
– the lowest x-value of the AABBmaxx
– the highest x-value of the AABBminy
– the lowest y-value of the AABBmaxy
– the highest y-value of the AABBminz
– the lowest z-value of the AABBmaxz
– the highest z-value of the AABBbuild()
, calcBoundingBox()
1 |
public Object3D cloneObject() |
1 |
public Matrix getWorldTransformation() |
1 |
public void addCollisionListener(CollisionListener listener) |
1 |
public void removeCollisionListener(CollisionListener listener) |
1 |
public void disableCollisionListeners() |
1 |
public void enableCollisionListeners() |
1 |
public java.util.Iterator<CollisionListener> getCollisionListeners() |
1 |
public void setRenderHook(IRenderHook hook) |
hook
– the hook into the rendering pipelineIRenderHook
1 |
public IRenderHook getRenderHook() |
1 |
public void addSpecificLight(Light light) |
1 |
public void removeSpecificLight(Light light) |
1 |
public void clearSpecificLights() |
1 2 |
public int checkForCollision(SimpleVector dirVec, float step) |
dirVec
– the direction vector (a unit vector)step
– the length of the casted ray (a collision will only be detected of it takes place within this range)setCenter(com.threed.jpct.SimpleVector)
, setCollisionMode(int)
, getID()
, NO_OBJECT
1 2 |
public SimpleVector checkForCollisionSpherical(SimpleVector translation, float radius) |
translation
– the translation the object should performradius
– the radius of the sphere (a collision will only be detected of it takes place within this radius)setCenter(com.threed.jpct.SimpleVector)
, setCollisionMode(int)
1 2 3 |
public SimpleVector checkForCollisionEllipsoid(SimpleVector translation, SimpleVector ellipsoid, int recursionDepth) |
translation
– the translation the object should performellipsoid
– the radius of the epplisoid in x,y and z directionrecursionDepth
– the max. recursion depth of the collision detection. A higher value will improve the accuracy of the collision detection but reduce performance. Reasonable values lie between 1 and 5.setCenter(com.threed.jpct.SimpleVector)
, setCollisionMode(int)
1 |
public void setEllipsoidMode(int mode) |
mode
– int the modeELLIPSOID_ALIGNED
, ELLIPSOID_TRANSFORMED
1 |
public int getEllipsoidMode() |
ELLIPSOID_ALIGNED
, ELLIPSOID_TRANSFORMED
1 |
public boolean wasTargetOfLastCollision() |
resetCollisionStatus()
1 |
public void resetCollisionStatus() |
wasTargetOfLastCollision()
1 2 |
public float calcMinDistance(SimpleVector org, SimpleVector dr) |
org
– a SimpleVector containing the position vectordr
– a SimpleVector containing the direction vectorCOLLISION_NONE
1 2 3 |
public float calcMinDistance(SimpleVector org, SimpleVector dr, float ignoreIfLarger) |
org
– a SimpleVector containing the position vectordr
– a SimpleVector containing the direction vectorignoreIfLarger
– only polygons within this range will be taken into accountCOLLISION_NONE
1 |
public void setCenter(SimpleVector center) |
center
– the centerbuild()
1 |
public SimpleVector getCenter() |
1 |
public SimpleVector getTransformedCenter() |
1 |
public SimpleVector getTransformedCenter(SimpleVector toFill) |
1 |
public void setRotationPivot(SimpleVector pivot) |
pivot
– the rotation pivot
1 |
public SimpleVector getRotationPivot() |
1 |
public void calcCenter() |
setCenter(com.threed.jpct.SimpleVector)
, setRotationPivot(com.threed.jpct.SimpleVector)
, build()
1 |
public void setOcTree(OcTree ocTree) |
ocTree
– the octree that subdivides this object
1 |
public OcTree getOcTree() |
1 |
public void setOrigin(SimpleVector origin) |
origin
– the origintranslate(float, float, float)
1 |
public void invert() |
1 |
public void invertCulling(boolean inv) |
inv
– invert the culling?
1 |
public boolean cullingIsInverted() |
1 |
public void calcNormals() |
build()
1 |
public void calcTextureWrap() |
calcTextureWrapSpherical()
1 |
public void calcTextureWrapSpherical() |
calcTextureWrap()
, Primitives
1 |
public void setTexture(java.lang.String texname) |
texname
– 通过TextureManager.addTexture()方法添加的纹理名称
1 |
public void setTexture(TextureInfo tInf) |
tInf
– the TextureInfoTextureInfo
1 |
public void shareTextureData(Object3D source) |
source
– the source of the texturing information
1 2 3 |
public float rayIntersectsAABB(SimpleVector org, SimpleVector dr, boolean isNormalized) |
org
– the position vector of the raydr
– the direction vector of the rayisNormalized
– indicates, that dr is already normalized. So the method can spare another normalization.RAY_MISSES_BOX
1 2 |
public float rayIntersectsAABB(SimpleVector org, SimpleVector dr) |
org
– the position vector of the raydr
– the direction vector of the rayRAY_MISSES_BOX
1 2 |
public boolean ellipsoidIntersectsAABB(SimpleVector org, SimpleVector ellipsoid) |
org
– the position vector of the ellipsoid (the ellipsoid’s center)ellipsoid
– the radius of the ellipsoid in x,y and z-direction as a SimpleVector
1 2 3 4 |
public boolean ellipsoidIntersectsAABB(float orgx, float orgy, float orgz, SimpleVector ellipsoid) |
1 2 |
public boolean sphereIntersectsAABB(SimpleVector org, float radius) |
org
– the position vector of the sphere (the sphere’s center)radius
– the radius of the sphere
1 2 3 |
public int addTriangle(SimpleVector vert1, SimpleVector vert2, SimpleVector vert3) |
vert1
– the first vertexvert2
– the second vertexvert3
– the third vertex
1 2 3 4 5 6 7 8 9 |
public int addTriangle(SimpleVector vert1, float u, float v, SimpleVector vert2, float u2, float v2, SimpleVector vert3, float u3, float v3) |
vert1
– the first vertexu
– the u component of the texture position at the first vertexv
– the v component of the texture position at the first vertexvert2
– the second vertexu2
– the u component of the texture position at the second vertexv2
– the v component of the texture position at the second vertexvert3
– the third vertexu3
– the u component of the texture position at the third vertexv3
– the v component of the texture position at the third vertexinvert()
1 2 3 4 5 6 7 8 9 10 |
public int addTriangle(SimpleVector vert1, float u, float v, SimpleVector vert2, float u2, float v2, SimpleVector vert3, float u3, float v3, int textureID) |
vert1
– the first vertexu
– the u component of the texture position at the first vertexv
– the v component of the texture position at the first vertexvert2
– the second vertexu2
– the u component of the texture position at the second vertexv2
– the v component of the texture position at the second vertexvert3
– the third vertexu3
– the u component of the texture position at the third vertexv3
– the v component of the texture position at the third vertextextureID
– the ID of the texture as returned by TextureManager.getTextureID()TextureManager.getTextureID(java.lang.String)
, invert()
1 2 3 4 5 6 7 8 9 10 11 |
public int addTriangle(SimpleVector vert1, float u, float v, SimpleVector vert2, float u2, float v2, SimpleVector vert3, float u3, float v3, int textureID, int sec) |
vert1
– the first vertexu
– the u component of the texture position at the first vertexv
– the v component of the texture position at the first vertexvert2
– the second vertexu2
– the u component of the texture position at the second vertexv2
– the v component of the texture position at the second vertexvert3
– the third vertexu3
– the u component of the texture position at the third vertexv3
– the v component of the texture position at the third vertextextureID
– the ID of the texture as returned by TextureManager.getTextureID()TextureManager.getTextureID(java.lang.String)
, invert()
1 2 3 4 |
public int addTriangle(SimpleVector vert1, SimpleVector vert2, SimpleVector vert3, TextureInfo tInf) |
vert1
– the first vertexvert2
– the second vertexvert3
– the third vertextInf
– the TextureInfoTextureManager.getTextureID(java.lang.String)
, TextureInfo
, invert()
1 |
public Matrix getInverseWorldTransformation() |
1 |
public Matrix getInverseWorldTransformation(Matrix mat) |
将obj和mtl文件放在Android项目中的src/main/assets目录下,在java代码中使用如下代码加载3D模型文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
private Object3D demoObj; private void loadObject() { InputStream mtlStream = null; InputStream objStream = null; try { objStream = getResources().getAssets().open("jeep.obj"); mtlStream = getResources().getAssets().open("jeep.mtl"); } catch (IOException e) { e.printStackTrace(); } Object3D[] demoobjs; demoobjs = Loader.loadOBJ(objStream, mtlStream, 0.7f); demoObj = new Object3D(0); for(Object3D obj3d: demoobjs){ demoObj = Object3D.mergeObjects(demoObj, obj3d); } // demoObj.rotateX(3f); // demoObj.rotateY(-1.5f); // demoObj.rotateZ(0.12f); // demoObj.rotateX(-0.1f); // demoObj.translate(0.8f, 0, -8); } |
需要在Android 1.5以上运行,如果需要使用OpenGL ES 2.0,需要在Android 2.2或以上。
下载jPCT-AE库,将jpct_ae.jar添加到项目中。
jPCT-AE不是线程安全,尽量不要跨线程操作jPCT-AE的对象。
GLSurfaceView的Render方法是在Android的主UI线程中运行的吗?
答:onDrawFrame是用于进行实际渲染的方法,运行在Render子线程中。
用于初始化创建或者更新World和FrameBuffer等对象。
实际的渲染过程所在的方法。
可以进行实际的绘制操作,也可以进行绘制帧率的计算或者控制(比如控制帧率为30fps以节省设备电量)。
通常,一些图像库的坐标系是这样的,
向右为x轴正向,向上为y轴正向,指向屏幕外为z轴正向,
但是jPCT的坐标系不太一样,jPCT和上述坐标系比较的话,围绕x轴旋转了180度,如下:
向右为x轴正向,向下为y轴正向,指向屏幕内为z轴正向。
如果你同时使用了其他不同坐标系的代码,需要对坐标进行一些转换。
https://www.jpct.net/wiki/index.php?title=Reducing_memory_usage
博文背景:imx 8qxp主板,android8.1系统,系统编译模式userDebug模式
一、开启成功的步骤
1 2 3 4 5 6 7 8 9 |
Fastboot: Got bootloader commands! 1 setufp mode 0 1 cdns3_uboot_initmode 0 flashing unlock. It is able to unlock device. 1 Start /data wipe process.... Wipe /data completed. Start stored_rollback_index wipe process.... Wipe stored_rollback_index completed. |
饿汉模式在声明静态内部对象时就创建该对象,就是不管在程序中是否用到这个静态对象都会创建它,对于不一定会使用该对象的程序是一种资源的浪费,代码如下:
1 2 3 4 5 6 7 8 9 |
public class SingletonTest { private static SingletonTest _singleton = new SingletonTest(); private SingletonTest() { } public static SingletonTest instance(){ return _singleton; } } |
因为饿汉模式在声明的时候就创建了,所以不会有线程安全问题
对于构造函数需要传入参数的单例也没办法这样创建
懒汉模式,即在使用的时候才去创建它,相对于饿汉他有点懒,但其实他不懒
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class SingletonTest { private static SingletonTest _singleton ; private SingletonTest() { } public static SingletonTest instance(){ if(_singleton == null) { _singleton = new SingletonTest(); } return _singleton; } } |
假如在多个线程中都会用到这个单例对象,那么就有可能造成实例出多个对象的问题了,就是这种方式是线程不安全的
我们在instance()方法前加上synchronized修饰,这样多个线程就不可能同时进入instance方法了,也不会造成new多个实例的问题了
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class SingletonTest { private static SingletonTest _singleton = null; private SingletonTest() { } public synchronized static SingletonTest instance(){ if(_singleton == null) { _singleton = new SingletonTest(); } return _singleton; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class SingletonTest { private static SingletonTest _singleton = null; private SingletonTest() { } public static SingletonTest instance(){ if(_singleton == null) { synchronized (SingletonTest.class) { _singleton = new SingletonTest(); } } return _singleton; } } |
这种方式看似加了一个锁,但是多个线程可能同时进入到if的判断语句中,同样不能保证_singleton的唯一性
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class SingletonTest { private volatile static SingletonTest _singleton = null; private SingletonTest() { } public static SingletonTest instance(){ if(_singleton == null) { synchronized (SingletonTest.class) { if(_singleton == null) { _singleton = new SingletonTest(); } } } return _singleton; } } |
举例:short类型的值范围为-32768 ~ 32767,看下面这些对short值的操作
1 2 3 4 5 6 7 8 9 10 11 |
short s32768 = (short) 32768; System.out.println("32768强制转换后的结果为:" + s32768); short s32769 = (short) 32769; System.out.println("32769强制转换后的结果为:" +s32769); short s65535 = (short) 65535; System.out.println("65535强制转换后的结果为:" +s65535); short s65536 = (short) 65536; System.out.println("65536强制转换后的结果为:" +s65536); |
最后的输出结果为:
1 2 3 4 |
32768强制转换后的结果为:-32768 32769强制转换后的结果为:-32767 65535强制转换后的结果为:-1 65536强制转换后的结果为:0 |
对于长度小于或者等于int的数据类型,如short, byte在进行如下运算后,其他运算结果为int型
<<,>>,>>>,&,|,^,+,-,*,/
如下代码,就会提示你将运算强制转换成short类型
1 2 |
short b222 = 10; short sdddd = b222 - 8; |
可以通过&运算将有符号号转为无符号数,
对于byte类型& 0xFF,
对于short类型& 0xFFFF,
对于int类型& 0xFFFFFFFF
注意&运算完的结果是一个int型的值
下面看一个示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public static void main(String[] args){ byte negb = -8; int negbint = negb & 0xFF; int castint = (int)negb; System.out.println("-8: " + byte2binary(negb)); System.out.println("-8 & 0xFF: " + negbint); System.out.println("negbint: " + int2binary(negbint)); System.out.println("-8 强制转换成int: " + castint); System.out.println("castint: " + int2binary(castint)); } public static String int2binary(int value){ String ibstr = Integer.toBinaryString(value); // 显示全部二进制字符,32位 while (ibstr.length() < 32){ ibstr = "0" + ibstr; } String ibstr1 = ibstr.substring(0, 8) + " " + ibstr.substring(8, 16) + " " + ibstr.substring(16, 24) + " " + ibstr.substring(24, 32); return ibstr1; } |
运行结果:
1 2 3 4 5 |
-8: 11111000 -8 & 0xFF: 248 negbint: 00000000 00000000 00000000 11111000 -8 强制转换成int: -8 castint: 11111111 11111111 11111111 11111000 |
强制类型转换并没有改变符号。
负数的左移位操作会使数据保持为负数
个人比较喜欢c语言,所以后面的编程都会使用c语言。
使用c语言的话可以使用用wiringPi类库。
1、查看是否安装wiringPi类库,输入下面的命令,如果返回版本信息则表示已安装wiringPi库
1 |
$ gpio -v |
2、如果没有安装,输入下面的命令安装
1 |
sudo apt-get install wiringpi |
3、wiringPi源码
github上有一个wiringPi的代码库,但那个并不是wiringPi的源码,其源码在下面这个地址中
将Led灯的两个针脚分别连到GND和GPIO0两个接口上,网上有的教程还说要连上一个电阻,但是俺没有,就直接连上了。
新建一个ledblink.c文件,然后输入下面的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> #include <wiringPi.h> #define LED 0 #define BLINK_INTERVAL 500 int main(void){ printf("LED blink!\n"); wiringPiSetup(); pinMode(LED, OUTPUT); for(;;){ digitalWrite(LED, 1); delay(BLINK_INTERVAL); digitalWrite(LED, 0); delay(BLINK_INTERVAL); } return 0; } |
输入下面的命令编译
1 |
gcc -o ledblink ledblink.c -lwiringPi |
编译完成后,会生成一个ledblink的可执行程序,输入./ledblink运行,看led灯是不是开始闪烁了!
LOCAL_MODULE_TAGS :=user debug eng tests optional
user: 指该模块只在user版本下才编译
debug: 批该模块只在userdebug版本下才编译
eng: 指该模块只在eng版本下才编译
tests: 指该模块只在tests版本下才编译
optional:指该模块在所有版本下都编译(但只有在PRODUCT_PACKAGES中定义的模块才会被安装?)
1. gRPC – 远程RPC框架
适用平台:c++、Java、Python、Php、Js等多平台
2. Netty -基于Java NIO client-server的网络应用框架
适用平台:Java
3. thrift – 用于可扩展的跨语言服务开发,简单来说就是RPC远程调用,它是一个完整的 RPC 框架体系。
适用平台:Actionscript 3.0、c_glib、C++、CSharp、D、Dart、Delphi、Go、Graphviz、Haxe Framework、Haskell、Java、Javascript、Node.js、OCaml、Perl、PHP、Python、Ruby
4. SOME/IP(vsomeip) – 用于车载的远程通讯框架,特点:服务发现\带宽预留,符合autosar(汽车开放系统架构)标准.SOME/IP是指协议栈,vsomeip是用c++的实现
适用平台:C/C++