आकार परिभाषित करें
संग्रह की मदद से व्यवस्थित रहें
अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.
OpenGL ES व्यू के हिसाब से बनाए जाने वाले आकारों को परिभाषित करने की क्षमता होने का पहला कदम है
बनाने में मदद मिलती है. OpenGL ES के साथ ड्रॉइंग करना थोड़ा मुश्किल हो सकता है
हम इस बारे में कुछ बुनियादी बातें जानते हैं कि OpenGL ES से आपको ग्राफ़िक ऑब्जेक्ट तय करने की कैसे उम्मीद है.
यह लेसन एक Android डिवाइस स्क्रीन के संबंध में OpenGL ES निर्देशांक सिस्टम के बारे में बताता है,
आकृति परिभाषित करने, आकृतियों के फ़लक रहने, और साथ ही त्रिभुज और वर्ग को परिभाषित करने के बारे में मूलभूत बातें.
त्रिभुज को परिभाषित करें
OpenGL ES आपको त्रि-आयामी स्पेस में निर्देशांकों का इस्तेमाल करके बनाए गए ऑब्जेक्ट को तय करने देता है. इसलिए,
इससे पहले कि आप कोई त्रिभुज बनाएं, आपको इसके निर्देशांक तय करने होंगे. OpenGL में, यह करने का सामान्य तरीका
यह निर्देशांकों के लिए फ़्लोटिंग पॉइंट नंबर की वर्टेक्स रेंज को तय करने के लिए है. ज़्यादा से ज़्यादा के लिए
दक्षता के लिए, आप इन निर्देशांकों को एक ByteBuffer
में लिखते हैं, जिसे
प्रोसेसिंग के लिए OpenGL ES ग्राफ़िक्स पाइपलाइन.
Kotlin
// number of coordinates per vertex in this array
const val COORDS_PER_VERTEX = 3
var triangleCoords = floatArrayOf( // in counterclockwise order:
0.0f, 0.622008459f, 0.0f, // top
-0.5f, -0.311004243f, 0.0f, // bottom left
0.5f, -0.311004243f, 0.0f // bottom right
)
class Triangle {
// Set color with red, green, blue and alpha (opacity) values
val color = floatArrayOf(0.63671875f, 0.76953125f, 0.22265625f, 1.0f)
private var vertexBuffer: FloatBuffer =
// (number of coordinate values * 4 bytes per float)
ByteBuffer.allocateDirect(triangleCoords.size * 4).run {
// use the device hardware's native byte order
order(ByteOrder.nativeOrder())
// create a floating point buffer from the ByteBuffer
asFloatBuffer().apply {
// add the coordinates to the FloatBuffer
put(triangleCoords)
// set the buffer to read the first coordinate
position(0)
}
}
}
Java
public class Triangle {
private FloatBuffer vertexBuffer;
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float triangleCoords[] = { // in counterclockwise order:
0.0f, 0.622008459f, 0.0f, // top
-0.5f, -0.311004243f, 0.0f, // bottom left
0.5f, -0.311004243f, 0.0f // bottom right
};
// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };
public Triangle() {
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
triangleCoords.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(triangleCoords);
// set the buffer to read the first coordinate
vertexBuffer.position(0);
}
}
डिफ़ॉल्ट रूप से, OpenGL ES एक निर्देशांक सिस्टम मानता है, जहां [0,0,0] (X,Y,Z)
GLSurfaceView
फ़्रेम,
[1,1,0] फ़्रेम का सबसे ऊपरी दायां कोना है और
[-1,-1,0] फ़्रेम के सबसे नीचे बाईं ओर होता है. इस निर्देशांक प्रणाली का उदाहरण देखने के लिए, देखें
OpenGL ES डेवलपर
गाइड देखें.
ध्यान दें कि इस आकार के निर्देशांकों को घड़ी की उलटी दिशा में तय किया जाता है. ड्रॉइंग
क्रम महत्वपूर्ण होता है क्योंकि यह परिभाषित करता है कि आकृति का अगला हिस्सा कौनसा है, जिसे आम तौर पर आप
ड्रॉ करना हो और इसके पीछे का चेहरा हो, जिसे बनाने के लिए OpenGL ES कल का इस्तेमाल करके ड्रॉ न किया जा सकता है
चेहरे की पहचान करने की सुविधा. चेहरे और अलग-अलग लाइन बनाने के बारे में ज़्यादा जानकारी के लिए, यह देखें
OpenGL ES की डेवलपर गाइड.
वर्ग परिभाषित करें
OpenGL में त्रिभुजों को परिभाषित करना बहुत आसान है, लेकिन अगर आप थोड़ा और ज़्यादा आज़माना चाहें, तो
जटिल? बोलें, एक स्क्वेयर? ऐसा करने के कई तरीके हैं, लेकिन ड्रॉइंग करने का एक आम तरीका
OpenGL ES में आकार एक साथ बनाए गए दो त्रिभुजों का इस्तेमाल करने के लिए है:
पहला डायग्राम. दो त्रिभुजों का इस्तेमाल करके एक वर्ग बनाना.
फिर से, आपको दोनों त्रिभुजों के लिए शीर्षों को घड़ी की उलटी दिशा में तय करना चाहिए
इस आकार को निरूपित करें और मानों को ByteBuffer
में रखें. इससे बचने के लिए
प्रत्येक त्रिभुज द्वारा साझा किए गए दो निर्देशांक दो बार परिभाषित करते हैं, तो
OpenGL ES ग्राफ़िक्स पाइपलाइन में इन वर्टेक्स को आरेखित करने का तरीका बताया गया है. इस आकार का कोड यहां दिया गया है:
Kotlin
// number of coordinates per vertex in this array
const val COORDS_PER_VERTEX = 3
var squareCoords = floatArrayOf(
-0.5f, 0.5f, 0.0f, // top left
-0.5f, -0.5f, 0.0f, // bottom left
0.5f, -0.5f, 0.0f, // bottom right
0.5f, 0.5f, 0.0f // top right
)
class Square2 {
private val drawOrder = shortArrayOf(0, 1, 2, 0, 2, 3) // order to draw vertices
// initialize vertex byte buffer for shape coordinates
private val vertexBuffer: FloatBuffer =
// (# of coordinate values * 4 bytes per float)
ByteBuffer.allocateDirect(squareCoords.size * 4).run {
order(ByteOrder.nativeOrder())
asFloatBuffer().apply {
put(squareCoords)
position(0)
}
}
// initialize byte buffer for the draw list
private val drawListBuffer: ShortBuffer =
// (# of coordinate values * 2 bytes per short)
ByteBuffer.allocateDirect(drawOrder.size * 2).run {
order(ByteOrder.nativeOrder())
asShortBuffer().apply {
put(drawOrder)
position(0)
}
}
}
Java
public class Square {
private FloatBuffer vertexBuffer;
private ShortBuffer drawListBuffer;
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float squareCoords[] = {
-0.5f, 0.5f, 0.0f, // top left
-0.5f, -0.5f, 0.0f, // bottom left
0.5f, -0.5f, 0.0f, // bottom right
0.5f, 0.5f, 0.0f }; // top right
private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices
public Square() {
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
squareCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(squareCoords);
vertexBuffer.position(0);
// initialize byte buffer for the draw list
ByteBuffer dlb = ByteBuffer.allocateDirect(
// (# of coordinate values * 2 bytes per short)
drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
}
}
यह उदाहरण आपको बताता है कि OpenGL के साथ ज़्यादा जटिल आकार बनाने के लिए क्या करना पड़ता है. तय सीमा में
सामान्य रूप से, आप ऑब्जेक्ट बनाने के लिए त्रिभुजों के संग्रह का इस्तेमाल करते हैं. अगले लेसन में, आपको ड्रॉइंग बनाने का तरीका पता चलेगा
दिखाई दे सकता है.
इस पेज पर मौजूद कॉन्टेंट और कोड सैंपल कॉन्टेंट के लाइसेंस में बताए गए लाइसेंस के हिसाब से हैं. Java और OpenJDK, Oracle और/या इससे जुड़ी हुई कंपनियों के ट्रेडमार्क या रजिस्टर किए हुए ट्रेडमार्क हैं.
आखिरी बार 2025-07-27 (UTC) को अपडेट किया गया.
[null,null,["आखिरी बार 2025-07-27 (UTC) को अपडेट किया गया."],[],[],null,["# Define shapes\n\nBeing able to define shapes to be drawn in the context of an OpenGL ES view is the first step in\ncreating high-end graphics for your app. Drawing with OpenGL ES can be a little tricky without\nknowing a few basic things about how OpenGL ES expects you to define graphic objects.\n\nThis lesson explains the OpenGL ES coordinate system relative to an Android device screen, the\nbasics of defining a shape, shape faces, as well as defining a triangle and a square.\n\nDefine a triangle\n-----------------\n\nOpenGL ES allows you to define drawn objects using coordinates in three-dimensional space. So,\nbefore you can draw a triangle, you must define its coordinates. In OpenGL, the typical way to do\nthis is to define a vertex array of floating point numbers for the coordinates. For maximum\nefficiency, you write these coordinates into a [ByteBuffer](/reference/java/nio/ByteBuffer), that is passed into the\nOpenGL ES graphics pipeline for processing. \n\n### Kotlin\n\n```kotlin\n// number of coordinates per vertex in this array\nconst val COORDS_PER_VERTEX = 3\nvar triangleCoords = floatArrayOf( // in counterclockwise order:\n 0.0f, 0.622008459f, 0.0f, // top\n -0.5f, -0.311004243f, 0.0f, // bottom left\n 0.5f, -0.311004243f, 0.0f // bottom right\n)\n\nclass Triangle {\n\n // Set color with red, green, blue and alpha (opacity) values\n val color = floatArrayOf(0.63671875f, 0.76953125f, 0.22265625f, 1.0f)\n\n private var vertexBuffer: FloatBuffer =\n // (number of coordinate values * 4 bytes per float)\n ByteBuffer.allocateDirect(triangleCoords.size * 4).run {\n // use the device hardware's native byte order\n order(ByteOrder.nativeOrder())\n\n // create a floating point buffer from the ByteBuffer\n asFloatBuffer().apply {\n // add the coordinates to the FloatBuffer\n put(triangleCoords)\n // set the buffer to read the first coordinate\n position(0)\n }\n }\n}\n```\n\n### Java\n\n```java\npublic class Triangle {\n\n private FloatBuffer vertexBuffer;\n\n // number of coordinates per vertex in this array\n static final int COORDS_PER_VERTEX = 3;\n static float triangleCoords[] = { // in counterclockwise order:\n 0.0f, 0.622008459f, 0.0f, // top\n -0.5f, -0.311004243f, 0.0f, // bottom left\n 0.5f, -0.311004243f, 0.0f // bottom right\n };\n\n // Set color with red, green, blue and alpha (opacity) values\n float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };\n\n public Triangle() {\n // initialize vertex byte buffer for shape coordinates\n ByteBuffer bb = ByteBuffer.allocateDirect(\n // (number of coordinate values * 4 bytes per float)\n triangleCoords.length * 4);\n // use the device hardware's native byte order\n bb.order(ByteOrder.nativeOrder());\n\n // create a floating point buffer from the ByteBuffer\n vertexBuffer = bb.asFloatBuffer();\n // add the coordinates to the FloatBuffer\n vertexBuffer.put(triangleCoords);\n // set the buffer to read the first coordinate\n vertexBuffer.position(0);\n }\n}\n```\n\nBy default, OpenGL ES assumes a coordinate system where \\[0,0,0\\] (X,Y,Z) specifies the center of\nthe [GLSurfaceView](/reference/android/opengl/GLSurfaceView) frame,\n\\[1,1,0\\] is the top right corner of the frame and\n\\[-1,-1,0\\] is bottom left corner of the frame. For an illustration of this coordinate system, see the\n[OpenGL ES developer\nguide](/develop/ui/views/graphics/opengl/about-opengl#coordinate-mapping).\n\nNote that the coordinates of this shape are defined in a counterclockwise order. The drawing\norder is important because it defines which side is the front face of the shape, which you typically\nwant to have drawn, and the back face, which you can choose to not draw using the OpenGL ES cull\nface feature. For more information about faces and culling, see the\n[OpenGL ES](/develop/ui/views/graphics/opengl/about-opengl#faces-winding) developer guide.\n\nDefine a square\n---------------\n\nDefining triangles is pretty easy in OpenGL, but what if you want to get a just a little more\ncomplex? Say, a square? There are a number of ways to do this, but a typical path to drawing such a\nshape in OpenGL ES is to use two triangles drawn together:\n\n\n**Figure 1.** Drawing a square using two triangles.\n\nAgain, you should define the vertices in a counterclockwise order for both triangles that\nrepresent this shape, and put the values in a [ByteBuffer](/reference/java/nio/ByteBuffer). In order to avoid\ndefining the two coordinates shared by each triangle twice, use a drawing list to tell the\nOpenGL ES graphics pipeline how to draw these vertices. Here's the code for this shape: \n\n### Kotlin\n\n```kotlin\n// number of coordinates per vertex in this array\nconst val COORDS_PER_VERTEX = 3\nvar squareCoords = floatArrayOf(\n -0.5f, 0.5f, 0.0f, // top left\n -0.5f, -0.5f, 0.0f, // bottom left\n 0.5f, -0.5f, 0.0f, // bottom right\n 0.5f, 0.5f, 0.0f // top right\n)\n\nclass Square2 {\n\n private val drawOrder = shortArrayOf(0, 1, 2, 0, 2, 3) // order to draw vertices\n\n // initialize vertex byte buffer for shape coordinates\n private val vertexBuffer: FloatBuffer =\n // (# of coordinate values * 4 bytes per float)\n ByteBuffer.allocateDirect(squareCoords.size * 4).run {\n order(ByteOrder.nativeOrder())\n asFloatBuffer().apply {\n put(squareCoords)\n position(0)\n }\n }\n\n // initialize byte buffer for the draw list\n private val drawListBuffer: ShortBuffer =\n // (# of coordinate values * 2 bytes per short)\n ByteBuffer.allocateDirect(drawOrder.size * 2).run {\n order(ByteOrder.nativeOrder())\n asShortBuffer().apply {\n put(drawOrder)\n position(0)\n }\n }\n}\n```\n\n### Java\n\n```java\npublic class Square {\n\n private FloatBuffer vertexBuffer;\n private ShortBuffer drawListBuffer;\n\n // number of coordinates per vertex in this array\n static final int COORDS_PER_VERTEX = 3;\n static float squareCoords[] = {\n -0.5f, 0.5f, 0.0f, // top left\n -0.5f, -0.5f, 0.0f, // bottom left\n 0.5f, -0.5f, 0.0f, // bottom right\n 0.5f, 0.5f, 0.0f }; // top right\n\n private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices\n\n public Square() {\n // initialize vertex byte buffer for shape coordinates\n ByteBuffer bb = ByteBuffer.allocateDirect(\n // (# of coordinate values * 4 bytes per float)\n squareCoords.length * 4);\n bb.order(ByteOrder.nativeOrder());\n vertexBuffer = bb.asFloatBuffer();\n vertexBuffer.put(squareCoords);\n vertexBuffer.position(0);\n\n // initialize byte buffer for the draw list\n ByteBuffer dlb = ByteBuffer.allocateDirect(\n // (# of coordinate values * 2 bytes per short)\n drawOrder.length * 2);\n dlb.order(ByteOrder.nativeOrder());\n drawListBuffer = dlb.asShortBuffer();\n drawListBuffer.put(drawOrder);\n drawListBuffer.position(0);\n }\n}\n```\n\nThis example gives you a peek at what it takes to create more complex shapes with OpenGL. In\ngeneral, you use collections of triangles to draw objects. In the next lesson, you learn how to draw\nthese shapes on screen."]]