Open GL의 기본적인 코드는
https://developer.android.com/develop/ui/views/graphics/opengl
위 사이트에 공개된 코드를 사용하였다.
MainActivity.java
package com.example.final_term_practice;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private GLSurfaceView gLView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity.
gLView = new MyGLSurfaceView(this);
setContentView(gLView);
}
}
MyGLSurfaceView.java
package com.example.final_term_practice;
import android.content.Context;
import android.opengl.GLSurfaceView;
class MyGLSurfaceView extends GLSurfaceView {
private final MyGLRenderer renderer;
public MyGLSurfaceView(Context context){
super(context);
// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);
renderer = new MyGLRenderer();
// Set the Renderer for drawing on the GLSurfaceView
setRenderer(renderer);
}
}
MyGLRenderer.java
package com.example.final_term_practice;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MyGLRenderer implements GLSurfaceView.Renderer {
private Final_practice mClones;
// vPMatrix is an abbreviation for "Model View Projection Matrix"
private final float[] vPMatrix = new float[16];
private final float[] projectionMatrix = new float[16];
private final float[] viewMatrix = new float[16];
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
// initialize a triangle
mClones = new Final_practice();
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
// Set the camera position (View matrix)
Matrix.setLookAtM(viewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(vPMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
// Draw shape
mClones.draw(vPMatrix);
// mTriangle.draw();
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM(projectionMatrix, 0, ratio, -ratio, -1, 1, 3, 7);
}
public static int loadShader(int type, String shaderCode){
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}
Final_practice.java
package com.example.final_term_practice;
import android.opengl.GLES20;
import android.opengl.Matrix;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
public class Final_practice {
///// Projection matrixes ////
private final float[] mTranslateMatrix = new float[16];
private final float[] mRotationMatrix = new float[16];
private final float[] scratch = new float[16];
private final float[] scratch_2 = new float[16];
private final float[] scratch_3 = new float[16];
private final float[] mRotationMatrix_new = new float[16];
// Use to access and set the view transformation
private int vPMatrixHandle;
private FloatBuffer circleBuffer_1;
private FloatBuffer circleBuffer_2;
private FloatBuffer circleBuffer_3;
private FloatBuffer bodyBuffer;
private final int mProgram;
private int positionHandle;
private int colorHandle;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
// Set color with red, green, blue and alpha (opacity) values
float color_red[] = {1.0f, 0.0f, 0.0f, 1.0f};
float color_black[] = {0.0f, 0.0f, 0.0f, 1.0f};
float color_yellow[] = {1.0f, 1.0f, 0.0f, 1.0f};
float color_gray[] = {0.8f, 0.8f, 0.8f, 1.0f};
float color_white[] = {1.0f, 1.0f, 1.0f, 1.0f};
static float circleCoords_1[] = new float[102 * COORDS_PER_VERTEX];
//노란 사다리꼴의 좌표 (큰원의 반지름이 0.3이여서 지름은 0.6)
float bodyCoords[] = { // in counterclockwise order:
0.2f, 0.7f, 0.0f,
0.2f, 0.1f, 0.0f,
-0.2f, 0.6f, 0.0f,
-0.2f, 0.2f, 0.0f
};
public Final_practice() {
int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
// create empty OpenGL ES Program
mProgram = GLES20.glCreateProgram();
// add the vertex shader to program
GLES20.glAttachShader(mProgram, vertexShader);
// add the fragment shader to program
GLES20.glAttachShader(mProgram, fragmentShader);
// creates OpenGL ES program executables
GLES20.glLinkProgram(mProgram);
//큰 원 (오른쪽 위치)
float R1 = 0.3f;
float R2 = 0.3f;
float x_center = 0.0f;
float y_center = 0.0f;
float dAngle = 2 * (float) Math.PI / (100);
//// v0 /////
circleCoords_1[0] = x_center;
circleCoords_1[1] = y_center;
circleCoords_1[2] = 0.0f;
int index = 3;
float angle;
///// 1st part
for (angle = 0.0f; angle <= 2 * Math.PI + dAngle; angle = angle + dAngle) {
circleCoords_1[index] = R1 * (float) Math.cos(angle);
circleCoords_1[index + 1] = R2 * (float) Math.sin(angle);
circleCoords_1[index + 2] = 0.0f;
index = index + 3;
}
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
circleCoords_1.length * 4);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
circleBuffer_1 = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
circleBuffer_1.put(circleCoords_1);
// set the buffer to read the first coordinate
circleBuffer_1.position(0);
//작은 원(왼쪽 위치), 하단에 위치한 회색 작은 원
R1 = 0.2f;
R2 = 0.2f;
x_center = 0.0f;
y_center = 0.0f;
dAngle = 2 * (float) Math.PI / (100);
//// v0 /////
circleCoords_1[0] = x_center;
circleCoords_1[1] = y_center;
circleCoords_1[2] = 0.0f;
index = 3;
///// 1st part
for (angle = 0.0f; angle <= 2 * Math.PI + dAngle; angle = angle + dAngle) {
circleCoords_1[index] = R1 * (float) Math.cos(angle);
circleCoords_1[index + 1] = R2 * (float) Math.sin(angle);
circleCoords_1[index + 2] = 0.0f;
index = index + 3;
}
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb1 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
circleCoords_1.length * 4);
// use the device hardware's native byte order
bb1.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
circleBuffer_2 = bb1.asFloatBuffer();
// add the coordinates to the FloatBuffer
circleBuffer_2.put(circleCoords_1);
// set the buffer to read the first coordinate
circleBuffer_2.position(0);
//하단에 위치하게 될 작은 원 (하얀색)
R1 = 0.1f;
R2 = 0.1f;
x_center = 0.0f;
y_center = 0.0f;
dAngle = 2 * (float) Math.PI / (100);
//// v0 /////
circleCoords_1[0] = x_center;
circleCoords_1[1] = y_center;
circleCoords_1[2] = 0.0f;
index = 3;
///// 1st part
for (angle = 0.0f; angle <= 2 * Math.PI + dAngle; angle = angle + dAngle) {
circleCoords_1[index] = R1 * (float) Math.cos(angle);
circleCoords_1[index + 1] = R2 * (float) Math.sin(angle);
circleCoords_1[index + 2] = 0.0f;
index = index + 3;
}
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb2 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
circleCoords_1.length * 4);
// use the device hardware's native byte order
bb2.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
circleBuffer_3 = bb2.asFloatBuffer();
// add the coordinates to the FloatBuffer
circleBuffer_3.put(circleCoords_1);
// set the buffer to read the first coordinate
circleBuffer_3.position(0);
ByteBuffer bb3 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
bodyCoords.length * 4);
// use the device hardware's native byte order
bb3.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
bodyBuffer = bb3.asFloatBuffer();
// add the coordinates to the FloatBuffer
bodyBuffer.put(bodyCoords);
// set the buffer to read the first coordinate
bodyBuffer.position(0);
}
public void draw(float[] mvpMatrix) {
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
positionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(positionHandle);
// get handle to fragment shader's vColor member
colorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
// Set color for drawing the triangle
// get handle to shape's transformation matrix
vPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
/// circle 1 (빨강 큰 원) //
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, circleBuffer_1);
Matrix.setIdentityM(mTranslateMatrix, 0);
Matrix.setIdentityM(mRotationMatrix, 0);
Matrix.translateM(mTranslateMatrix, 0, 0.2f, 0.4f, 0.0f); //빨강원이 위치할 위치 지정
Matrix.setRotateM(mRotationMatrix, 0, 0.0f, 0, 0, -1.0f);
//얼마나 회전할 건지 지정하는데 지금은 회전하지 않도록 설정
//scratch, scratch_2가 어디에 들어가있는지 잘 확인 (최종 glUniformMatrix4fv여기에는 scratch_2가 들어가있음)
Matrix.multiplyMM(scratch, 0, mvpMatrix, 0, mTranslateMatrix, 0);
Matrix.multiplyMM(scratch_2, 0, scratch, 0, mRotationMatrix, 0);
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, scratch_2, 0);
//색 지정
GLES20.glUniform4fv(colorHandle, 1, color_red, 0);
// Draw the circle (102번 회전해서 원 만들 수 있게 해줌)
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 102);
/// Circle 2 (빨강 작은 원)
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, circleBuffer_2);
Matrix.setIdentityM(mTranslateMatrix, 0);
Matrix.setIdentityM(mRotationMatrix, 0);
Matrix.translateM(mTranslateMatrix, 0, -0.2f, 0.4f, 0.0f); //작은원의 좌표를 지정
Matrix.setRotateM(mRotationMatrix, 0, 0.0f, 0, 0, -1.0f);
Matrix.multiplyMM(scratch, 0, mvpMatrix, 0, mTranslateMatrix, 0);
Matrix.multiplyMM(scratch_2, 0, scratch, 0, mRotationMatrix, 0);
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, scratch_2, 0);
GLES20.glUniform4fv(colorHandle, 1, color_red, 0);
// Draw the circle
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 102);
/// Draw body (노랑 사다리꼴)///
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, mvpMatrix, 0);
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, bodyBuffer);
GLES20.glUniform4fv(colorHandle, 1, color_yellow, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); //FAN이 아닌 STRIP 사용
///// Figure 2 ///
//// Central body - 하단 중심 회색 원///
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, circleBuffer_2);
Matrix.setIdentityM(mTranslateMatrix, 0);
Matrix.setIdentityM(mRotationMatrix, 0);
Matrix.translateM(mTranslateMatrix, 0, 0.0f, -0.5f, 0.0f); //좌표 설정
Matrix.setRotateM(mRotationMatrix, 0, 0.0f, 0, 0, -1.0f);
Matrix.multiplyMM(scratch, 0, mvpMatrix, 0, mTranslateMatrix, 0);
Matrix.multiplyMM(scratch_2, 0, scratch, 0, mRotationMatrix, 0);
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, scratch_2, 0);
GLES20.glUniform4fv(colorHandle, 1, color_gray, 0);
// Draw the circle
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 102);
//// Cut part 1 북쪽 흰색 원///
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, circleBuffer_3);
Matrix.setIdentityM(mTranslateMatrix, 0);
Matrix.setIdentityM(mRotationMatrix, 0);
Matrix.translateM(mTranslateMatrix, 0, 0.0f, -0.3f, 0.0f); //좌표 설정
Matrix.setRotateM(mRotationMatrix, 0, 0.0f, 0, 0, -1.0f);
Matrix.multiplyMM(scratch, 0, mvpMatrix, 0, mTranslateMatrix, 0);
Matrix.multiplyMM(scratch_2, 0, scratch, 0, mRotationMatrix, 0);
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, scratch_2, 0);
GLES20.glUniform4fv(colorHandle, 1, color_white, 0);
// Draw the circle
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 102);
//// Cut part 2 서쪽 흰색 원 ///
Matrix.setIdentityM(mTranslateMatrix, 0);
Matrix.setIdentityM(mRotationMatrix, 0);
//회색 원을 중심으로 좌 우에 위치 하는거여서 y축은 동일하게 x축만 변경해줌
Matrix.translateM(mTranslateMatrix, 0, -0.2f, -0.5f, 0.0f);
Matrix.setRotateM(mRotationMatrix, 0, 0.0f, 0, 0, -1.0f);
Matrix.multiplyMM(scratch, 0, mvpMatrix, 0, mTranslateMatrix, 0);
Matrix.multiplyMM(scratch_2, 0, scratch, 0, mRotationMatrix, 0);
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, scratch_2, 0);
// Draw the circle
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 102);
//// Cut part 3 남쪽 흰색 원///
Matrix.setIdentityM(mTranslateMatrix, 0);
Matrix.setIdentityM(mRotationMatrix, 0);
Matrix.translateM(mTranslateMatrix, 0, 0.0f, -0.7f, 0.0f);
Matrix.setRotateM(mRotationMatrix, 0, 0.0f, 0, 0, -1.0f);
Matrix.multiplyMM(scratch, 0, mvpMatrix, 0, mTranslateMatrix, 0);
Matrix.multiplyMM(scratch_2, 0, scratch, 0, mRotationMatrix, 0);
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, scratch_2, 0);
// Draw the circle
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 102);
//// Cut part 4 동쪽 흰색 원///
Matrix.setIdentityM(mTranslateMatrix, 0);
Matrix.setIdentityM(mRotationMatrix, 0);
Matrix.translateM(mTranslateMatrix, 0, 0.2f, -0.5f, 0.0f);
Matrix.setRotateM(mRotationMatrix, 0, 0.0f, 0, 0, -1.0f);
Matrix.multiplyMM(scratch, 0, mvpMatrix, 0, mTranslateMatrix, 0);
Matrix.multiplyMM(scratch_2, 0, scratch, 0, mRotationMatrix, 0);
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, scratch_2, 0);
// Draw the circle
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 102);
// Disable vertex array
GLES20.glDisableVertexAttribArray(positionHandle);
}
private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
// the matrix must be included as a modifier of gl_Position
// Note that the uMVPMatrix factor *must be first* in order
// for the matrix multiplication product to be correct.
" gl_Position = uMVPMatrix * vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
}
'실시간 운영체제' 카테고리의 다른 글
[실시간 운영체제] Open GL Draw house pt.2 (with animation) (0) | 2023.06.17 |
---|---|
[실시간 운영체제] Open GL Draw house pt.2 (0) | 2023.06.17 |
[실시간 운영체제] Open GL clones animation (0) | 2023.06.17 |
[실시간 운영체제] Open GL animation (0) | 2023.06.15 |
[실시간 운영체제] Open GL translation and rotaion (0) | 2023.06.15 |