Open GL의 기본적인 코드는
https://developer.android.com/develop/ui/views/graphics/opengl
위 사이트에 공개된 코드를 사용하였다.
MainActivity.java
package com.example.draw_house_hp5;
import androidx.appcompat.app.AppCompatActivity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private GLSurfaceView gLView;
@Override
protected 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.draw_house_hp5;
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.draw_house_hp5;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
public class MyGLRenderer implements GLSurfaceView.Renderer {
// 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];
private house mhouse;
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
//GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//backgroound / 배경색을 하얀색을 바꿔줌
GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
mhouse = new house();
}
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);
mhouse.draw(vPMatrix);
}
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;
}
}
house.java
package com.example.draw_house_hp5;
import android.opengl.GLES20;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
public class house {
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;" +
"}";
// Use to access and set the view transformation
private int vPMatrixHandle;
private FloatBuffer vertexBuffer, vertexBuffer_circle1,vertexBuffer_circle2,vertexBuffer_circle3,vertexBuffer_circle4,vertexBuffer_circle5
,vertexBuffer_door_lock,vertexBuffer_cloud1,vertexBuffer_cloud2,vertexBuffer_cloud3,vertexBuffer_cloud4;
private final int mProgram;
private int positionHandle;
private int colorHandle;
//private final int vertexCount = Vertec_coor.length / COORDS_PER_VERTEX;
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;
static float Vertec_coor[] = { // in counterclockwise order:
0.6f, 0.2f, 0.0f, // 지붕 v0
-0.6f, 0.2f, 0.0f, // v1
0.4f, 0.4f, 0.0f, // v2
-0.4f, 0.4f, 0.0f, // v3
0.45f, 0.2f, 0.0f, // 벽면 v4
0.45f, -0.3f, 0.0f, // v5
-0.45f, 0.2f, 0.0f, // v6
-0.45f, -0.3f, 0.0f, // v7
0.0f, 0.15f, 0.0f, // 창틀 v8
0.0f, -0.1f, 0.0f, // v9
-0.35f, 0.15f, 0.0f, // v10
-0.35f, -0.1f, 0.0f, // v11
-0.02f, 0.13f, 0.0f, // 창문 v12
-0.02f, -0.08f, 0.0f, // v13
-0.33f, 0.13f, 0.0f, // v14
-0.33f, -0.08f, 0.0f, // v15
-0.165f, 0.13f, 0.0f, // 창문 중간선 v16
-0.165f, -0.08f, 0.0f, //v17
-0.185f, 0.13f, 0.0f, //v18
-0.185f, -0.08f, 0.0f, //v19
0.35f, 0.15f, 0.0f, // 문 v20
0.35f, -0.3f, 0.0f, //v21
0.15f, 0.15f, 0.0f, //v22
0.15f, -0.3f, 0.0f, //v23
-0.15f, 0.55f, 0.0f, // 굴뚝 v24
-0.15f, 0.4f, 0.0f, //v25
-0.3f, 0.55f, 0.0f, //v26
-0.3f, 0.4f, 0.0f, //v27
}; //집 만들기
// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };
//지붕 색 정하기 (rgb값/255.0f)
float roof_color[] = {0/255.0f, 0/255.0f, 153/255.0f, 1.0f };
float wall_color[] = {224/255.0f, 224/255.0f, 224/255.0f, 1.0f };
float wood_color[] = {105/255.0f, 51/255.0f, 0/255.0f, 1.0f };
float window_color[] = {204/255.0f, 229/255.0f, 255/255.0f, 1.0f };
float chimney_color[] = {96/255.0f, 96/255.0f, 96/255.0f, 1.0f };
float door_lock_color[] = {255/255.0f, 255/255.0f, 0/255.0f, 1.0f };
float cloud_color[] = {192/255.0f, 192/255.0f, 192/255.0f, 1.0f };
float roof_circle_color1[] = {255/255.0f, 153/255.0f, 153/255.0f, 1.0f };
float roof_circle_color2[] = {255/255.0f, 255/255.0f, 153/255.0f, 1.0f };
float roof_circle_color3[] = {204/255.0f, 255/255.0f, 153/255.0f, 1.0f };
float roof_circle_color4[] = {153/255.0f, 204/255.0f, 255/255.0f, 1.0f };
float roof_circle_color5[] = {204/255.0f, 204/255.0f, 255/255.0f, 1.0f };
private static final int vertexCount=100;
static float circle_coordinate[] = new float[vertexCount*COORDS_PER_VERTEX]; //원을 그리기 위해 이 부분을 바꿔준다
static float circle_coordinate2[] = new float[vertexCount*COORDS_PER_VERTEX]; //원을 그리기 위해 이 부분을 바꿔준다
static float circle_coordinate3[] = new float[vertexCount*COORDS_PER_VERTEX]; //원을 그리기 위해 이 부분을 바꿔준다
static float circle_coordinate4[] = new float[vertexCount*COORDS_PER_VERTEX]; //원을 그리기 위해 이 부분을 바꿔준다
static float circle_coordinate5[] = new float[vertexCount*COORDS_PER_VERTEX]; //원을 그리기 위해 이 부분을 바꿔준다
static float door_lock_coordinate[] = new float[vertexCount*COORDS_PER_VERTEX]; //원을 그리기 위해 이 부분을 바꿔준다
static float cloud_coordinate1[] = new float[vertexCount*COORDS_PER_VERTEX]; //원을 그리기 위해 이 부분을 바꿔준다
static float cloud_coordinate2[] = new float[vertexCount*COORDS_PER_VERTEX]; //원을 그리기 위해 이 부분을 바꿔준다
static float cloud_coordinate3[] = new float[vertexCount*COORDS_PER_VERTEX]; //원을 그리기 위해 이 부분을 바꿔준다
static float cloud_coordinate4[] = new float[vertexCount*COORDS_PER_VERTEX]; //원을 그리기 위해 이 부분을 바꿔준다
public house() {
//set vertexes for circle 1
float R = 0.05f; //반지름
float r=0.05f; // 타원으로 하고싶을때 x축의 반지름 값 바꿔주면 됨
float circle_center_x = 0.35f; //원의 중심점 바꿀 수 있게 해줌
float circle_center_y = 0.3f;
circle_coordinate[0] = circle_center_x; //v0 : x=0 원의 중심점(0,0,0)
circle_coordinate[1] = circle_center_y; //v0 : y=0
circle_coordinate[2] = 0.0f; //v0 : z=0
//make a loop to compute circle vertexes
float d_angle = (float)Math.PI/24;
int index = 3;
//돌아가면서 삼각형의 좌표를 계속 생성해줌
//for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle) //축 안바꾸고 그냥 일직선
//for (float angle = (float)Math.PI/4;angle<=5*Math.PI/4+d_angle;angle=angle+d_angle){ //원의 축을 바꿔주는거
for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle){ //축 안바꾸고 그냥 일직선
circle_coordinate[index] = circle_center_x+r*(float)Math.cos(angle); //x=R*cos(angle) 타원일떄
//circle_coordinate[index] = circle_center_x+R*(float)Math.cos(angle);
circle_coordinate[index+1] = circle_center_y+R*(float)Math.sin(angle); //x=R*cos(angle)
circle_coordinate[index+2] = 0.0f; //x=R*cos(angle)
index = index+3;
}
ByteBuffer bb2 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
circle_coordinate.length * 4);
// use the device hardware's native byte order
bb2.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer_circle1 = bb2.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer_circle1.put(circle_coordinate);
// set the buffer to read the first coordinate
vertexBuffer_circle1.position(0);
//set vertexes for circle 2
R = 0.05f; //반지름
r=0.05f; // 타원으로 하고싶을때 x축의 반지름 값 바꿔주면 됨
circle_center_x = 0.178f; //원의 중심점 바꿀 수 있게 해줌
circle_center_y = 0.3f;
circle_coordinate2[0] = circle_center_x; //v0 : x=0 원의 중심점(0,0,0)
circle_coordinate2[1] = circle_center_y; //v0 : y=0
circle_coordinate2[2] = 0.0f; //v0 : z=0
//make a loop to compute circle vertexes
d_angle = (float)Math.PI/24;
index = 3;
//돌아가면서 삼각형의 좌표를 계속 생성해줌
//for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle) //축 안바꾸고 그냥 일직선
//for (float angle = (float)Math.PI/4;angle<=5*Math.PI/4+d_angle;angle=angle+d_angle){ //원의 축을 바꿔주는거
for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle){ //축 안바꾸고 그냥 일직선
circle_coordinate2[index] = circle_center_x+r*(float)Math.cos(angle); //x=R*cos(angle) 타원일떄
//circle_coordinate[index] = circle_center_x+R*(float)Math.cos(angle);
circle_coordinate2[index+1] = circle_center_y+R*(float)Math.sin(angle); //x=R*cos(angle)
circle_coordinate2[index+2] = 0.0f; //x=R*cos(angle)
index = index+3;
}
ByteBuffer bb3 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
circle_coordinate2.length * 4);
// use the device hardware's native byte order
bb3.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer_circle2 = bb3.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer_circle2.put(circle_coordinate2);
// set the buffer to read the first coordinate
vertexBuffer_circle2.position(0);
//set vertexes for circle 3
R = 0.05f; //반지름
r=0.05f; // 타원으로 하고싶을때 x축의 반지름 값 바꿔주면 됨
circle_center_x = 0.0f; //원의 중심점 바꿀 수 있게 해줌
circle_center_y = 0.3f;
circle_coordinate3[0] = circle_center_x; //v0 : x=0 원의 중심점(0,0,0)
circle_coordinate3[1] = circle_center_y; //v0 : y=0
circle_coordinate3[2] = 0.0f; //v0 : z=0
//make a loop to compute circle vertexes
d_angle = (float)Math.PI/24;
index = 3;
//돌아가면서 삼각형의 좌표를 계속 생성해줌
//for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle) //축 안바꾸고 그냥 일직선
//for (float angle = (float)Math.PI/4;angle<=5*Math.PI/4+d_angle;angle=angle+d_angle){ //원의 축을 바꿔주는거
for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle){ //축 안바꾸고 그냥 일직선
circle_coordinate3[index] = circle_center_x+r*(float)Math.cos(angle); //x=R*cos(angle) 타원일떄
//circle_coordinate[index] = circle_center_x+R*(float)Math.cos(angle);
circle_coordinate3[index+1] = circle_center_y+R*(float)Math.sin(angle); //x=R*cos(angle)
circle_coordinate3[index+2] = 0.0f; //x=R*cos(angle)
index = index+3;
}
ByteBuffer bb4 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
circle_coordinate3.length * 4);
// use the device hardware's native byte order
bb4.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer_circle3 = bb4.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer_circle3.put(circle_coordinate3);
// set the buffer to read the first coordinate
vertexBuffer_circle3.position(0);
//set vertexes for circle 4
R = 0.05f; //반지름
r=0.05f; // 타원으로 하고싶을때 x축의 반지름 값 바꿔주면 됨
circle_center_x = -0.175f; //원의 중심점 바꿀 수 있게 해줌
circle_center_y = 0.3f;
circle_coordinate4[0] = circle_center_x; //v0 : x=0 원의 중심점(0,0,0)
circle_coordinate4[1] = circle_center_y; //v0 : y=0
circle_coordinate4[2] = 0.0f; //v0 : z=0
//make a loop to compute circle vertexes
d_angle = (float)Math.PI/24;
index = 3;
//돌아가면서 삼각형의 좌표를 계속 생성해줌
//for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle) //축 안바꾸고 그냥 일직선
//for (float angle = (float)Math.PI/4;angle<=5*Math.PI/4+d_angle;angle=angle+d_angle){ //원의 축을 바꿔주는거
for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle){ //축 안바꾸고 그냥 일직선
circle_coordinate4[index] = circle_center_x+r*(float)Math.cos(angle); //x=R*cos(angle) 타원일떄
//circle_coordinate[index] = circle_center_x+R*(float)Math.cos(angle);
circle_coordinate4[index+1] = circle_center_y+R*(float)Math.sin(angle); //x=R*cos(angle)
circle_coordinate4[index+2] = 0.0f; //x=R*cos(angle)
index = index+3;
}
ByteBuffer bb5 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
circle_coordinate4.length * 4);
// use the device hardware's native byte order
bb5.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer_circle4 = bb5.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer_circle4.put(circle_coordinate4);
// set the buffer to read the first coordinate
vertexBuffer_circle4.position(0);
//set vertexes for circle 5
R = 0.05f; //반지름
r=0.05f; // 타원으로 하고싶을때 x축의 반지름 값 바꿔주면 됨
circle_center_x = -0.35f; //원의 중심점 바꿀 수 있게 해줌
circle_center_y = 0.3f;
circle_coordinate5[0] = circle_center_x; //v0 : x=0 원의 중심점(0,0,0)
circle_coordinate5[1] = circle_center_y; //v0 : y=0
circle_coordinate5[2] = 0.0f; //v0 : z=0
//make a loop to compute circle vertexes
d_angle = (float)Math.PI/24;
index = 3;
//돌아가면서 삼각형의 좌표를 계속 생성해줌
//for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle) //축 안바꾸고 그냥 일직선
//for (float angle = (float)Math.PI/4;angle<=5*Math.PI/4+d_angle;angle=angle+d_angle){ //원의 축을 바꿔주는거
for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle){ //축 안바꾸고 그냥 일직선
circle_coordinate5[index] = circle_center_x+r*(float)Math.cos(angle); //x=R*cos(angle) 타원일떄
//circle_coordinate[index] = circle_center_x+R*(float)Math.cos(angle);
circle_coordinate5[index+1] = circle_center_y+R*(float)Math.sin(angle); //x=R*cos(angle)
circle_coordinate5[index+2] = 0.0f; //x=R*cos(angle)
index = index+3;
}
ByteBuffer bb6 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
circle_coordinate5.length * 4);
// use the device hardware's native byte order
bb6.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer_circle5 = bb6.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer_circle5.put(circle_coordinate5);
// set the buffer to read the first coordinate
vertexBuffer_circle5.position(0);
//set vertexes for door lock
R = 0.011f; //반지름
r=0.017f; // 타원으로 하고싶을때 x축의 반지름 값 바꿔주면 됨
circle_center_x = 0.185f; //원의 중심점 바꿀 수 있게 해줌
circle_center_y = -0.07f;
door_lock_coordinate[0] = circle_center_x; //v0 : x=0 원의 중심점(0,0,0)
door_lock_coordinate[1] = circle_center_y; //v0 : y=0
door_lock_coordinate[2] = 0.0f; //v0 : z=0
//make a loop to compute circle vertexes
d_angle = (float)Math.PI/24;
index = 3;
//돌아가면서 삼각형의 좌표를 계속 생성해줌
//for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle) //축 안바꾸고 그냥 일직선
//for (float angle = (float)Math.PI/4;angle<=5*Math.PI/4+d_angle;angle=angle+d_angle){ //원의 축을 바꿔주는거
for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle){ //축 안바꾸고 그냥 일직선
door_lock_coordinate[index] = circle_center_x+r*(float)Math.cos(angle); //x=R*cos(angle) 타원일떄
//circle_coordinate[index] = circle_center_x+R*(float)Math.cos(angle);
door_lock_coordinate[index+1] = circle_center_y+R*(float)Math.sin(angle); //x=R*cos(angle)
door_lock_coordinate[index+2] = 0.0f; //x=R*cos(angle)
index = index+3;
}
ByteBuffer bb7 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
door_lock_coordinate.length * 4);
// use the device hardware's native byte order
bb7.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer_door_lock = bb7.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer_door_lock.put(door_lock_coordinate);
// set the buffer to read the first coordinate
vertexBuffer_door_lock.position(0);
//set vertexes for cloud 1
R = 0.055f; //반지름
r=0.12f; // 타원으로 하고싶을때 x축의 반지름 값 바꿔주면 됨
circle_center_x = -0.235f; //원의 중심점 바꿀 수 있게 해줌
circle_center_y = 0.64f;
cloud_coordinate1[0] = circle_center_x; //v0 : x=0 원의 중심점(0,0,0)
cloud_coordinate1[1] = circle_center_y; //v0 : y=0
cloud_coordinate1[2] = 0.0f; //v0 : z=0
//make a loop to compute circle vertexes
d_angle = (float)Math.PI/24;
index = 3;
//돌아가면서 삼각형의 좌표를 계속 생성해줌
//for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle) //축 안바꾸고 그냥 일직선
//for (float angle = (float)Math.PI/4;angle<=5*Math.PI/4+d_angle;angle=angle+d_angle){ //원의 축을 바꿔주는거
for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle){ //축 안바꾸고 그냥 일직선
cloud_coordinate1[index] = circle_center_x+r*(float)Math.cos(angle); //x=R*cos(angle) 타원일떄
//circle_coordinate[index] = circle_center_x+R*(float)Math.cos(angle);
cloud_coordinate1[index+1] = circle_center_y+R*(float)Math.sin(angle); //x=R*cos(angle)
cloud_coordinate1[index+2] = 0.0f; //x=R*cos(angle)
index = index+3;
}
ByteBuffer bb8 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
cloud_coordinate1.length * 4);
// use the device hardware's native byte order
bb8.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer_cloud1 = bb8.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer_cloud1.put(cloud_coordinate1);
// set the buffer to read the first coordinate
vertexBuffer_cloud1.position(0);
//set vertexes for cloud 2
R = 0.04f; //반지름
r=0.10f; // 타원으로 하고싶을때 x축의 반지름 값 바꿔주면 됨
circle_center_x = -0.22f; //원의 중심점 바꿀 수 있게 해줌
circle_center_y = 0.76f;
cloud_coordinate2[0] = circle_center_x; //v0 : x=0 원의 중심점(0,0,0)
cloud_coordinate2[1] = circle_center_y; //v0 : y=0
cloud_coordinate2[2] = 0.0f; //v0 : z=0
//make a loop to compute circle vertexes
d_angle = (float)Math.PI/24;
index = 3;
//돌아가면서 삼각형의 좌표를 계속 생성해줌
//for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle) //축 안바꾸고 그냥 일직선
//for (float angle = (float)Math.PI/4;angle<=5*Math.PI/4+d_angle;angle=angle+d_angle){ //원의 축을 바꿔주는거
for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle){ //축 안바꾸고 그냥 일직선
cloud_coordinate2[index] = circle_center_x+r*(float)Math.cos(angle); //x=R*cos(angle) 타원일떄
//circle_coordinate[index] = circle_center_x+R*(float)Math.cos(angle);
cloud_coordinate2[index+1] = circle_center_y+R*(float)Math.sin(angle); //x=R*cos(angle)
cloud_coordinate2[index+2] = 0.0f; //x=R*cos(angle)
index = index+3;
}
ByteBuffer bb9 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
cloud_coordinate2.length * 4);
// use the device hardware's native byte order
bb9.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer_cloud2 = bb9.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer_cloud2.put(cloud_coordinate2);
// set the buffer to read the first coordinate
vertexBuffer_cloud2.position(0);
//set vertexes for cloud 3
R = 0.03f; //반지름
r=0.08f; // 타원으로 하고싶을때 x축의 반지름 값 바꿔주면 됨
circle_center_x = -0.19f; //원의 중심점 바꿀 수 있게 해줌
circle_center_y = 0.85f;
cloud_coordinate3[0] = circle_center_x; //v0 : x=0 원의 중심점(0,0,0)
cloud_coordinate3[1] = circle_center_y; //v0 : y=0
cloud_coordinate3[2] = 0.0f; //v0 : z=0
//make a loop to compute circle vertexes
d_angle = (float)Math.PI/24;
index = 3;
//돌아가면서 삼각형의 좌표를 계속 생성해줌
//for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle) //축 안바꾸고 그냥 일직선
//for (float angle = (float)Math.PI/4;angle<=5*Math.PI/4+d_angle;angle=angle+d_angle){ //원의 축을 바꿔주는거
for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle){ //축 안바꾸고 그냥 일직선
cloud_coordinate3[index] = circle_center_x+r*(float)Math.cos(angle); //x=R*cos(angle) 타원일떄
//circle_coordinate[index] = circle_center_x+R*(float)Math.cos(angle);
cloud_coordinate3[index+1] = circle_center_y+R*(float)Math.sin(angle); //x=R*cos(angle)
cloud_coordinate3[index+2] = 0.0f; //x=R*cos(angle)
index = index+3;
}
ByteBuffer bb10 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
cloud_coordinate3.length * 4);
// use the device hardware's native byte order
bb10.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer_cloud3 = bb10.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer_cloud3.put(cloud_coordinate3);
// set the buffer to read the first coordinate
vertexBuffer_cloud3.position(0);
//set vertexes for cloud 4
R = 0.025f; //반지름
r=0.07f; // 타원으로 하고싶을때 x축의 반지름 값 바꿔주면 됨
circle_center_x = -0.17f; //원의 중심점 바꿀 수 있게 해줌
circle_center_y = 0.925f;
cloud_coordinate4[0] = circle_center_x; //v0 : x=0 원의 중심점(0,0,0)
cloud_coordinate4[1] = circle_center_y; //v0 : y=0
cloud_coordinate4[2] = 0.0f; //v0 : z=0
//make a loop to compute circle vertexes
d_angle = (float)Math.PI/24;
index = 3;
//돌아가면서 삼각형의 좌표를 계속 생성해줌
//for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle) //축 안바꾸고 그냥 일직선
//for (float angle = (float)Math.PI/4;angle<=5*Math.PI/4+d_angle;angle=angle+d_angle){ //원의 축을 바꿔주는거
for (float angle = 0.0f;angle<=2*Math.PI+d_angle;angle=angle+d_angle){ //축 안바꾸고 그냥 일직선
cloud_coordinate4[index] = circle_center_x+r*(float)Math.cos(angle); //x=R*cos(angle) 타원일떄
//circle_coordinate[index] = circle_center_x+R*(float)Math.cos(angle);
cloud_coordinate4[index+1] = circle_center_y+R*(float)Math.sin(angle); //x=R*cos(angle)
cloud_coordinate4[index+2] = 0.0f; //x=R*cos(angle)
index = index+3;
}
ByteBuffer bb11 = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
cloud_coordinate4.length * 4);
// use the device hardware's native byte order
bb11.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer_cloud4 = bb11.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer_cloud4.put(cloud_coordinate4);
// set the buffer to read the first coordinate
vertexBuffer_cloud4.position(0);
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
Vertec_coor.length * 4);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer.put(Vertec_coor);
// set the buffer to read the first coordinate
vertexBuffer.position(0);
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);
}
public void draw(float[] mvpMatrix) {
// get handle to shape's transformation matrix
vPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
// Pass the projection and view transformation to the shader
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, mvpMatrix, 0);
// 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);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
// get handle to fragment shader's vColor member
colorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
// Set color for drawing the triangle (지붕 만드는 삼각형, 바꾼 색 넣어줌)
GLES20.glUniform4fv(colorHandle, 1, roof_color, 0);
// Draw the roof (지붕 그리기)
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
// Draw the wall (벽면 그리기)
GLES20.glUniform4fv(colorHandle, 1, wall_color, 0); //여기서 색 안바꿔주면 앞에서 지정한 색으로 만들어짐
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 4, 4); //삼각형 두개 합쳐줌
//3 4 5 하나, 3 5 6 하나 만들어짐 -> GL_TRIANGLE_FAN
// Draw the wood (창틀 그리기)
GLES20.glUniform4fv(colorHandle, 1, wood_color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 8, 4); //삼각형 두개 합쳐줌
// Draw the windom (창문 그리기)
GLES20.glUniform4fv(colorHandle, 1, window_color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 12, 4); //삼각형 두개 합쳐줌
// Draw the wood (중간 창틀 그리기)
GLES20.glUniform4fv(colorHandle, 1, wood_color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 16, 4); //삼각형 두개 합쳐줌
// Draw the door (문 그리기)
GLES20.glUniform4fv(colorHandle, 1, wood_color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 20, 4); //삼각형 두개 합쳐줌
// Draw the chimney (문 그리기)
GLES20.glUniform4fv(colorHandle, 1, chimney_color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 24, 4); //삼각형 두개 합쳐줌
//
// // Draw the window for door (문에 있는 창문 그리기)
// GLES20.glUniform4fv(colorHandle, 1, window_color, 0);
// GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 23, 4); //삼각형 두개 합쳐줌
///draw a circle 1
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer_circle1);
GLES20.glUniform4fv(colorHandle, 1, roof_circle_color5, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 50);
///draw a circle 2
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer_circle2);
GLES20.glUniform4fv(colorHandle, 1, roof_circle_color4, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 50);
///draw a circle 3
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer_circle3);
GLES20.glUniform4fv(colorHandle, 1, roof_circle_color3, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 50);
///draw a circle 4
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer_circle4);
GLES20.glUniform4fv(colorHandle, 1, roof_circle_color2, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 50);
///draw a circle 5
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer_circle5);
GLES20.glUniform4fv(colorHandle, 1, roof_circle_color1, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 50);
///draw a door lock
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer_door_lock);
GLES20.glUniform4fv(colorHandle, 1, door_lock_color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 50);
///draw a cloud1
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer_cloud1);
GLES20.glUniform4fv(colorHandle, 1, cloud_color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 50);
///draw a cloud2
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer_cloud2);
GLES20.glUniform4fv(colorHandle, 1, cloud_color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 50);
///draw a cloud3
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer_cloud3);
GLES20.glUniform4fv(colorHandle, 1, cloud_color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 50);
///draw a cloud4
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer_cloud4);
GLES20.glUniform4fv(colorHandle, 1, cloud_color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 50);
// Disable vertex array
GLES20.glDisableVertexAttribArray(positionHandle);
}
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 different shapes (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 |