का उपयोग करते समय उल्लंघन का उल्लंघन करें मुझे वर्टेक्स बफर ऑब्जेक्ट का उपयोग करके कुछ ज्यामिति को प्रस्तुत करने में परेशानी है। मैं अंक के एक विमान को आकर्षित करना चाहता हूं, इसलिए मूल रूप से मेरी जगह में प्रत्येक अलग स्थिति में एक कशेरुका। हालांकि, मैं उस विमान को प्रस्तुत नहीं कर सकता, क्योंकि हर बार जब मैं glDrawElements (...) कहता हूं, तो एप्लिकेशन एक उल्लंघन उल्लंघन अपवाद लौटने पर क्रैश हो जाता है। प्रारंभिक होने पर कुछ गलती होनी चाहिए, मुझे लगता है। इस पृष्ठ पर दूसरे उदाहरण मेंओपनजीएल 3.x: वर्टेक्स बफर ऑब्जेक्ट और glDrawElements (...)
#define SPACE_X 512
#define SPACE_Z 512
typedef struct{
GLfloat x, y, z; // position
GLfloat nx, ny, nz; // normals
GLfloat r, g, b, a; // colors
} Vertex;
typedef struct{
GLuint i; // index
} Index;
// create vertex buffer
GLuint vertexBufferObject;
glGenBuffers(1, &vertexBufferObject);
// create index buffer
GLuint indexBufferObject;
glGenBuffers(1, &indexBufferObject);
// determine number of vertices/primitives
const int numberOfVertices = SPACE_X * SPACE_Z;
const int numberOfPrimitives = numberOfVertices; // As I'm going to render GL_POINTS, number of primitives is the same as number of vertices
// create vertex array
Vertex* vertexArray = new Vertex[numberOfVertices];
// create index array
Index* indexArray = new Index[numberOfPrimitives];
// create planes (vertex array)
// color of the vertices is red for now
int index = -1;
for(GLfloat x = -SPACE_X/2; x < SPACE_X/2; x++) {
for(GLfloat z = -SPACE_Z/2; z < SPACE_Z/2; z++) {
index++;
vertexArray[index].x = x;
vertexArray[index].y = 0.0f;
vertexArray[index].z = z;
vertexArray[index].nx = 0.0f;
vertexArray[index].ny = 0.0f;
vertexArray[index].nz = 1.0f;
vertexArray[index].r = 1.0;
vertexArray[index].g = 0.0;
vertexArray[index].b = 0.0;
vertexArray[index].a = 1.0;
}
}
// bind vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
// buffer vertex array
glBufferData(GL_ARRAY_BUFFER, numberOfVertices * sizeof(Vertex), vertexArray, GL_DTREAM_DRAW);
// bind vertex buffer again
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
// enable attrib index 0 (positions)
glEnableVertexAttribArray(0);
// pass positions in
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), vertexArray);
// enable attribute index 1 (normals)
glEnableVertexAttribArray(1);
// pass normals in
glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), &vertexArray[0].nx);
// enable attribute index 2 (colors)
glEnableVertexAttribArray(2);
// pass colors in
glVertexAttribPointer((GLuint)2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), &vertexArray[0].r);
// create index array
for(GLunit i = 0; i < numberOfPrimitives; i++) {
indexArray[i].i = i;
}
// bind buffer
glBindBuffer(GL_ELEMENET_ARRAY_BUFFER, indexBufferObject);
// buffer indices
glBufferData(GL_ELEMENET_ARRAY_BUFFER, numberOfPrimitives * sizeof(Index), indexArray, GL_STREAM_DRAW);
// bind buffer again
glBindBuffer(GL_ELEMENET_ARRAY_BUFFER, indexBufferObject);
// AND HERE IT CRASHES!
// draw plane of GL_POINTS
glDrawElements(GL_POINTS, numberOfPrimitives, GL_UNSIGNED_INT, indexArray);
// bind default buffers
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// delete vertex/index buffers
glDeleteBuffers(1, &vertexBufferObject);
glDeleteBuffers(1, &indexBufferObject);
delete[] vertexArray;
vertexArray = NULL;
delete[] indexArray;
indexArray = NULL;
कृपया हमेशा यह बताएं कि आप किस ओपनजीएल को लक्षित कर रहे हैं - 3.x थोड़ा अस्पष्ट है। बीटीडब्ल्यू- किसी भी 'glVertexAttribPointer' या' glEnableVertexAttribArray' से पहले एक कशेरुक सरणी वस्तु बनाने और बांधने का अच्छा अभ्यास है। आप शायद ओपनजीएल संगतता प्रोफ़ाइल का उपयोग कर रहे हैं, इसलिए आपको इसकी वजह से कोई त्रुटि नहीं मिली है। – Kos
पर्याप्त मेला। मुझे लगता है कि 3.x थोड़ा अस्पष्ट है। मुझे लगा कि जीएल के मामूली संस्करणों के बीच भी बड़ा अंतर है। तो, वर्तमान में मैं ओपनजीएल 3.3 का उपयोग कर रहा हूं। – Walter