vistaでandroid studio その14

# 概要
vistaでandroid studio 1.0.1やってみた。
openglやってみた。
練習問題やってみた。

# 練習問題
三角形を描画せよ。

# 写真

![device-2022-09-03-193142.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/7a63092a-61b1-1de6-ff03-e68389543d5a.png)


# サンプルコード

```
package com.ohisamallc.ohiapp154.ohiapp154;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.net.Uri;
import android.opengl.EGL14;
import android.opengl.GLES10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends ActionBarActivity {
	GLSurfaceView gLView;
	GLRenderer mRenderer;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		gLView = new GLSurfaceView(this);
		gLView.setEGLContextClientVersion(2);
		mRenderer = new GLRenderer();
		gLView.setRenderer(mRenderer);
		gLView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
		setContentView(gLView);

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	class GLRenderer implements GLSurfaceView.Renderer {
		Triangle triangle;
		public GLRenderer() {
		}
		@Override
		public void onSurfaceCreated(GL10 gl, EGLConfig config) {
			GLES20.glClearColor(0.0f, 0.0f, 0.7f, 1);
			GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
			triangle = new Triangle();
			triangle.Init();
		}
		@Override
		public void onSurfaceChanged(GL10 gl, int width, int height) {
		}
		@Override
		public void onDrawFrame(GL10 unused) {
			GLES20.glClearColor(0.0f, 0.0f, 0.7f, 1);
			GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
			triangle.draw();
		}
		public void onPause() {
		}
		public void onResume() {
		}
	}

	class Triangle {
		public final String vertexShaderCode = "attribute vec4 vPosition;" +
						"attribute vec4 vColor;" +
						"varying mediump vec4 varColor;" +
						"void main() {" +
						"  varColor = vColor;" +
						"  gl_Position = vPosition;" +
						"}";
		public final String fragmentShaderCode = "precision mediump float;" +
						"varying mediump vec4 varColor;" +
						"void main() {" +
						"  gl_FragColor = varColor;" +
						"}";
		private int loadShader(int type, String shaderCode) {
			int err = 0;
			int shader = GLES20.glCreateShader(type);
			//err = EGL14.eglGetError();
			//if (err != EGL14.EGL_SUCCESS)
			//	return 0;
			GLES20.glShaderSource(shader, shaderCode);
			//if (EGL14.eglGetError() != EGL14.EGL_SUCCESS)
			//	return 0;
			GLES20.glCompileShader(shader);
			return shader;
		}
		private int shaderProgram;
		public Triangle() {
		}
		public int Init() {
			int err = 0;
			int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
			int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
			shaderProgram = GLES20.glCreateProgram();
			err = GLES20.glGetError();
			if (err != GLES20.GL_NO_ERROR)
				return err;
			GLES20.glAttachShader(shaderProgram, vertexShader);
			GLES20.glAttachShader(shaderProgram, fragmentShader);
			GLES20.glLinkProgram(shaderProgram);
			err = GLES20.glGetError();
			if (err != GLES20.GL_NO_ERROR)
				return err;
			int[] status = new int[1];
			GLES20.glGetProgramiv(shaderProgram, GLES20.GL_LINK_STATUS, status, 0);
			if (status[0] == GLES20.GL_FALSE)
			{
				String msz = GLES20.glGetProgramInfoLog(shaderProgram);
				Log.e("sample", GLES20.glGetProgramInfoLog(shaderProgram));
				GLES20.glDeleteProgram(shaderProgram);
			}
			return GLES20.GL_NO_ERROR;
		}
		public void draw() {
			int err = 0;
			GLES20.glUseProgram(shaderProgram);
			err = GLES20.glGetError();
			if (err != GLES20.GL_NO_ERROR) return;
			int positionAttrib = GLES20.glGetAttribLocation(shaderProgram, "vPosition");
			GLES20.glEnableVertexAttribArray(positionAttrib);
			if (true)
			{
				float vertices[] = {
						0.0f, 0.4f, 0.0f, 1.0f,
						-0.3f, -0.3f, 0.0f, 1.0f,
						0.5f, -0.3f, 0.0f, 1.0f,
				};
				ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
				bb.order(ByteOrder.nativeOrder());
				FloatBuffer vertexBuffer = bb.asFloatBuffer();
				vertexBuffer.put(vertices);
				vertexBuffer.position(0);
				int stride = 0;
				GLES20.glVertexAttribPointer(positionAttrib, 4, GLES20.GL_FLOAT, false, stride, vertexBuffer);
			}
			boolean enableColor = true;
			int colorAttrib = 0;
			if (enableColor)
			{
				float colors[] = {
						1.0f, 0.0f, 0.0f, 1.0f,
						0.0f, 1.0f, 0.0f, 1.0f,
						0.0f, 0.0f, 1.0f, 1.0f,
				};
				colorAttrib = GLES20.glGetAttribLocation(shaderProgram, "vColor");
				GLES20.glEnableVertexAttribArray(colorAttrib);
				ByteBuffer bb2 = ByteBuffer.allocateDirect(colors.length * 4);
				bb2.order(ByteOrder.nativeOrder());
				FloatBuffer colorBuffer = bb2.asFloatBuffer();
				colorBuffer.put(colors);
				colorBuffer.position(0);
				int stride = 0;
				GLES20.glVertexAttribPointer(colorAttrib, 4, GLES20.GL_FLOAT, false, stride, colorBuffer);
			}
			GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
			GLES20.glDisableVertexAttribArray(positionAttrib);
			if (enableColor)
			{
				GLES20.glDisableVertexAttribArray(colorAttrib);
			}
		}
	}

}


```


以上。