这是RoofShape类
package com.view9.stoddart.opengl;
import android.opengl.GLU;
import android.util.Log;
import com.view9.stoddart.model.OpenGlPointObject;
import com.view9.stoddart.model.OpenGlRoofObject;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.List;
import javax.microedition.khronos.opengles.GL10;
/**
* Created by view9 on 10/25/15.
*/
public class RoofShape {
ArrayList<CoOrdinatesModel> cordinateList;
List<Short> indicesList;
private FloatBuffer mVertexBuffer = null;
private ShortBuffer mShapeBorderIndicesBuffer = null;
private int mNumOfShapeBorderIndices = 0;
OpenGlRoofObject roofObject;
public RoofShape(OpenGlRoofObject roofObject) {
this.roofObject = roofObject;
setAllBuffers();
}
public void drawShape(GL10 gl) {
// Log.d("#####", "draw called");
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
// Draw all lines
gl.glDrawElements(GL10.GL_LINES, mNumOfShapeBorderIndices,
GL10.GL_UNSIGNED_SHORT, mShapeBorderIndicesBuffer);
GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
private void setAllBuffers() {
cordinateList = new ArrayList<>();
cordinateList.add(new CoOrdinatesModel(Float.parseFloat("0.0"), Float.parseFloat("0.0"), Float.parseFloat("0.0")));
for (int pointCount = 0; pointCount < roofObject.getPoinstList().size(); pointCount++) {
OpenGlPointObject pointObject = roofObject.getPoinstList().get(pointCount);
cordinateList.add(new CoOrdinatesModel(pointObject.getxAsis(), pointObject.getyAxis(), pointObject.getzAxis()));
}
/**====================================
* working with the indices for line join
* adding cordinates to indicesList
======================================*/
indicesList = new ArrayList<>();
for (int indicesCount = 0; indicesCount < roofObject.getLinesList().size(); indicesCount++) {
String p = roofObject.getLinesList().get(indicesCount).getPath();
for (String strPoint : p.split(",")) {
//spliting the string and eleminating the prefix string
String prefixStringEleminating = strPoint.substring(1);
Short intPoint = Short.parseShort(prefixStringEleminating);
indicesList.add(intPoint);
}
}
updateVertexListToArray(cordinateList);
updateVerticesListToArray(indicesList);
}
private void updateVerticesListToArray(List<Short> list) {
/**
* For converting indices list to indices array
*/
ArrayList<Short> indicesList = new ArrayList<>();
indicesList.addAll(list);
short[] pointsToJoinList = new short[indicesList.size()];
int indicesCount = 0;
for (short shortIndices : indicesList) {
pointsToJoinList[indicesCount++] = shortIndices;
}
mNumOfShapeBorderIndices = pointsToJoinList.length;
ByteBuffer tbibb = ByteBuffer.allocateDirect(pointsToJoinList.length * 2);
tbibb.order(ByteOrder.nativeOrder());
mShapeBorderIndicesBuffer = tbibb.asShortBuffer();
mShapeBorderIndicesBuffer.put(pointsToJoinList);
mShapeBorderIndicesBuffer.position(0);
}
private void updateVertexListToArray(ArrayList<CoOrdinatesModel> cordinateList) {
/**============================
* working with the vertexes
* adding cordinates to VertexList
===============================*/
List<Float> vList = new ArrayList<>();
int loopSize = cordinateList.size();
for (int i = 0; i < loopSize; i++) {
vList.add(cordinateList.get(i).getxAxis());
vList.add(cordinateList.get(i).getyAxis());
vList.add(cordinateList.get(i).getzAxis());
}
/**
* converting vertex list to array
*/
float[] vertexlist = new float[vList.size()];
int count = 0;
for (float f : vList) {
vertexlist[count++] = f;
}
ByteBuffer vbb = ByteBuffer.allocateDirect(vertexlist.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertexlist);
mVertexBuffer.position(0);
}
public void setFlashingPointTOCordinatelist(GL10 gl, ArrayList<CoOrdinatesModel> flashingPoint) {
cordinateList.addAll(flashingPoint);
mVertexBuffer.clear();
mShapeBorderIndicesBuffer.clear();
updateVertexListToArray(cordinateList);
Short initialPoint = Short.parseShort("0");
Short previousUpdated = Short.parseShort("0");
for (int i = 1; i <= 5; i++) {
String totalNo = (cordinateList.size() + 1) + i + "";
Short indice = Short.parseShort(totalNo);
String str = indice + "";
if (i == 1) {
indicesList.add(Short.parseShort(str));
initialPoint = Short.parseShort(str);
} else if (i == 2) {
indicesList.add(Short.parseShort(str));
previousUpdated = Short.parseShort(str);
} else if (i == 3 || i == 4) {
indicesList.add(previousUpdated);
indicesList.add(Short.parseShort(str));
previousUpdated = Short.parseShort(str);
} else if (i == 5) {
indicesList.add(previousUpdated);
indicesList.add(initialPoint);
}
}
updateVerticesListToArray(indicesList);
drawShape(gl);
}
}
这是我的渲染器类
package com.view9.stoddart.opengl;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.Log;
import com.view9.stoddart.model.OpenGlRoofObject;
import java.util.ArrayList;
import javax.microedition.khronos.opengles.GL10;
public class StoddartNewRenderer implements GLSurfaceView.Renderer {
private Context mContext;
public float mAngleX = 0.0f;
public float mAngleY = 0.0f;
public float mAngleZ = 0.0f;
private float mPreviousX;
private float mPreviousY;
private final float TOUCH_SCALE_FACTOR = 0.6f;
private OpenGlRoofObject roofObject;
GL10 glForShape;
RoofShape roofShape;
public StoddartNewRenderer(Context context, OpenGlRoofObject roofObject) {
mContext = context;
this.roofObject = roofObject;
roofShape = new RoofShape(roofObject);
}
public void onDrawFrame(GL10 gl) {
// Log.d("####","draw frame called");
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -3.0f);
gl.glRotatef(mAngleX, 1, 0, 0);
gl.glRotatef(mAngleY, 0, 1, 0);
gl.glRotatef(mAngleZ, 0, 0, 1);
// Set line color to black
gl.glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
// gl.glColorMask(true, false, false, true);
roofShape.drawShape(gl);
//testing
glForShape = gl;
}
@Override
public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) {
Log.d("####", "surface created");
gl.glClearColor(1f, 1f, 1f, 1);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glEnable(GL10.GL_DEPTH_TEST);
// Enabled the vertex buffer for writing and to be used during rendering.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (height == 0) height = 1;
gl.glViewport(0, 0, width, height);
float aspect = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-aspect, aspect, -1.0f, 1.0f, 1.0f, 0.0f);
Log.d("####", "surface is changed");
}
public void drawSelectedShape(float x, float y) {
ArrayList<CoOrdinatesModel> flashingPoint = new ArrayList<>();
flashingPoint.add(new CoOrdinatesModel(x - 0.1f, y, 0f));
flashingPoint.add(new CoOrdinatesModel(x, y + 0.1f, 0f));
flashingPoint.add(new CoOrdinatesModel(x + 0.1f, y, 0f));
flashingPoint.add(new CoOrdinatesModel(x, y - 0.1f, 0f));
roofShape.setFlashingPointTOCordinatelist(glForShape, flashingPoint);
}
}
这是我的GLSurfaceview类
package com.view9.stoddart.opengl;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
/**
* Created by view9 on 10/12/15.
*/
public class StoddartOpenGlSurfaceView extends GLSurfaceView {
StoddartNewRenderer renderer;
public StoddartOpenGlSurfaceView(Context context) {
super(context);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
public StoddartOpenGlSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void showRenderer(StoddartNewRenderer renderer) {
this.renderer = renderer;
}
@Override
public boolean onTouchEvent(MotionEvent e) {
// return renderer.onTouchEvent(event);
float x = e.getX() / 100;
float y = e.getY() / 100;
Log.d("position", "x-axis: " + x + " y-axis: " + y);
renderer.drawSelectedShape(x, y);
requestRender();
return true;
}
}
这是我设置glsurfaceview 的活动 数据是从xi格式的api中获取的,我已经很好地解析了数据并将其存储为屋顶对象。
package com.view9.stoddart.activities;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.view9.stoddart.ApiCallConfiguration.ServerConfig;
import com.view9.stoddart.ApiCallConfiguration.WebService;
import com.view9.stoddart.R;
import com.view9.stoddart.fragment.ItemsSelectFragment;
import com.view9.stoddart.model.OpenGlRoofObject;
import com.view9.stoddart.model.SelectItemsDto;
import com.view9.stoddart.opengl.StoddartNewRenderer;
import com.view9.stoddart.opengl.StoddartOpenGlSurfaceView;
import com.view9.stoddart.utills.AppController;
import com.view9.stoddart.utills.Cache;
import com.view9.stoddart.utills.CustomDialogs;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
import java.util.ArrayList;
/**
* Created by view9 on 6/8/15.
*/
public class DrawingFlashingActivity extends BaseActivity {
DrawingFlashingActivity instance = null;
public DrawingFlashingActivity getInstance() {
return instance;
}
OpenGlRoofObject roofObject;
private LinearLayout gridLayout, flashingDiagramLayout;
// StoddartRenderer renderer;
StoddartNewRenderer renderer;
StoddartOpenGlSurfaceView stoddartOpenGlSurfaceView;
Response.Listener<String> response;
Response.ErrorListener errorListener;
Dialog pDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = DrawingFlashingActivity.this;
roofObject = Cache.cachedRoofObject;
iniitilizeUiComponent();
stoddartOpenGlSurfaceView = (StoddartOpenGlSurfaceView) findViewById(R.id.opengl_surface_view);
renderer = new StoddartNewRenderer(getInstance(), roofObject);
stoddartOpenGlSurfaceView.setRenderer(renderer);
stoddartOpenGlSurfaceView.showRenderer(renderer);
gettingServerResponse();
pDialog = CustomDialogs.progressDialog(getInstance(), AppController.LOADING);
}
private void gettingServerResponse() {
response = new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.d("response", s);
pDialog.dismiss();
try {
JSONObject nodeObject = new JSONObject(s);
Boolean status = nodeObject.getBoolean("success");
int code = nodeObject.getInt("code");
if (code == 1 && status) {
Cache.FLASHING_TYPE = s;
parseFlashingType(s);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
volleyError.printStackTrace();
}
};
}
private void parseFlashingType(String s) {
try {
ArrayList<SelectItemsDto> itemslist = new ArrayList<>();
JSONObject nodeObject = new JSONObject(s);
JSONArray dataArray = nodeObject.getJSONArray("data");
for (int i = 0; i < dataArray.length(); i++) {
String type = dataArray.get(i).toString();
itemslist.add(new SelectItemsDto("i", type, false, false));
}
ItemsSelectFragment alertdFragment = new ItemsSelectFragment();
Bundle bundle = new Bundle();
bundle.putString("title", "Choose Flashing Type");
bundle.putBoolean("isSingleSelect", true);
bundle.putSerializable("itemsList", (Serializable) itemslist);
alertdFragment.setArguments(bundle);
alertdFragment.show(getSupportFragmentManager(), "Alert Dialog Fragment");
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public String getToolbarTitle() {
return "Flashing";
}
@Override
public int getResourceLayout() {
return R.layout.layout_flashing_measure_activity;
}
@Override
public int getActivityId() {
return 8;
}
@Override
protected void onPause() {
super.onPause();
stoddartOpenGlSurfaceView.onPause();
}
@Override
protected void onResume() {
super.onResume();
stoddartOpenGlSurfaceView.onResume();
}
private void iniitilizeUiComponent() {
gridLayout = (LinearLayout) findViewById(R.id.l_gridLayout);
gridLayout.setVisibility(View.GONE);
flashingDiagramLayout = (LinearLayout) findViewById(R.id.flashing_diagram_layout);
flashingDiagramLayout.setVisibility(View.VISIBLE);
setListeners();
}
private void setListeners() {
ivTick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Cache.FLASHING_TYPE != "") {
parseFlashingType(Cache.FLASHING_TYPE);
} else {
pDialog.show();
WebService.getFlashingType(response, errorListener, ServerConfig.GET_FLASHING_TYPE_URL);
}
}
});
}
}
编写这些代码我可以成功地绘制一个如图所示的形状。显示的图是屋顶。我的问题是,我想在触摸形状时添加一个圆圈;可能有多个圈子,例如如果我触摸说(1,2,1)坐标,则应该再次将圆圈添加到该点。如果我触摸另一个点,则应将下一个圆圈添加到该点,依此类推......这也会显示在第二个图像中。
我浪费了3天这样做,但没有找到任何解决方案;到目前为止,我在这段代码中尝试做的是:每当我触摸视图时,我都会计算x和y的值,并传递给setFlashingPointTOCordinatelist()
类中定义的方法RoofShape
。 / p>
任何形式的帮助将不胜感激。请记住,我对这个opengl很新。我可以画出这张图片here。
this是我想要触摸形状的图像。