glVertexPointer
时间: 2023-11-15 19:06:03 浏览: 79
glVertexPointer是OpenGL中的一个函数,它用于告诉OpenGL如何解释顶点数组中的数据。函数原型如下:
```
void glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
```
参数说明:
- size:每个顶点的坐标分量数,可以是2,3或4。例如,二维坐标需要设置为2,三维坐标需要设置为3。
- type:每个分量的数据类型,可以是GL_BYTE、GL_SHORT、GL_INT、GL_FLOAT或GL_DOUBLE等。
- stride:相邻两个顶点之间的字节数,如果数据是紧密排列的,则设置为0。
- pointer:指向顶点数据的指针,如果使用顶点数组,则指向数组的第一个元素。
使用glVertexPointer函数之后,我们就可以通过其他OpenGL函数来绘制各种形状了,例如glDrawArrays和glDrawElements等函数。
相关问题
void drawSquareAnnulus() { // Bind vertex buffer and reserve space. glBindBuffer(GL_ARRAY_BUFFER, buffer[VERTICES]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), NULL, GL_STATIC_DRAW); // Copy vertex coordinates data into first half of vertex buffer. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Copy vertex color data into second half of vertex buffer. glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors); // Bind and fill indices buffer. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer[INDICES]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(stripIndices), stripIndices, GL_STATIC_DRAW); glVertexPointer(3, GL_FLOAT, 0, 0); glColorPointer(3, GL_FLOAT, 0, (void*)(sizeof(vertices))); }将上述代码改成绘制圆形的
要绘制圆形,可以使用三角扇或三角形条带来逼近圆形。以下是使用三角扇绘制圆形的示例代码:
```
void drawCircle() {
const int numSegments = 32; // 圆形的线段数
const float radius = 1.0f; // 圆形半径
// 顶点坐标和颜色数组
GLfloat vertices[(numSegments + 2) * 3];
GLfloat colors[(numSegments + 2) * 3];
// 圆心点
vertices[0] = 0.0f;
vertices[1] = 0.0f;
vertices[2] = 0.0f;
colors[0] = 1.0f;
colors[1] = 1.0f;
colors[2] = 1.0f;
// 计算圆周上的点的坐标和颜色
for (int i = 0; i <= numSegments; i++) {
float angle = i * 2.0f * M_PI / numSegments;
int offset = (i + 1) * 3;
vertices[offset] = cos(angle) * radius;
vertices[offset + 1] = sin(angle) * radius;
vertices[offset + 2] = 0.0f;
colors[offset] = 1.0f;
colors[offset + 1] = 1.0f;
colors[offset + 2] = 1.0f;
}
// 绘制三角扇
glBindBuffer(GL_ARRAY_BUFFER, buffer[VERTICES]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors);
glVertexPointer(3, GL_FLOAT, 0, 0);
glColorPointer(3, GL_FLOAT, 0, (void*)(sizeof(vertices)));
glDrawArrays(GL_TRIANGLE_FAN, 0, numSegments + 2);
}
```
注意到圆形的顶点数是固定的,因此在画圆形时不需要使用索引缓冲区。在顶点坐标和颜色数组中,第一个顶点是圆心,其它顶点按逆时针顺序排列在圆周上。最后使用 `glDrawArrays` 函数绘制三角扇即可。
#define _USE_MATH_DEFINES #include <cstdlib> #include <cmath> #include <iostream> #include <GL/glew.h> #include <GL/freeglut.h> // Globals. static float R = 40.0; // Radius of circle. static float X = 50.0; // X-coordinate of center of circle. static float Y = 50.0; // Y-coordinate of center of circle. static const int numVertices = 50; // Number of vertices on circle. static int verticesColors[6 * numVertices]; void generateVertices() { float t = 0; // Angle parameter. for (int i = 0; i < 6*numVertices; i+=6) { verticesColors[] = X + R * cos(t); //x verticesColors[] = Y + R * sin(t); //y verticesColors[] = 0.0; //z verticesColors[] = 1.0; //r verticesColors[] = 0.0; //g verticesColors[] = 0.0; //b t += 2 * M_PI / numVertices; //angle } } // Drawing routine. void drawScene(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1, 0, 0); glLineWidth(5); glDrawArrays(GL_LINE_LOOP, 0, 50); glFlush(); } // Initialization routine. void setup(void) { glClearColor(1.0, 1.0, 1.0, 0.0); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), &verticesColors[0]); glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), &verticesColors[3]) } // OpenGL window reshape routine. void resize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } // Keyboard input processing routine. void keyInput(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; default: break; } } // Main routine. int main(int argc, char** argv) { generateVertices(); glutInit(&argc, argv); glutInitContextVersion(4, 3); glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("circle.cpp"); glutDisplayFunc(drawScene); glutReshapeFunc(resize); glutKeyboardFunc(keyInput); glewExperimental = GL_TRUE; glewInit(); setup(); glutMainLoop(); }怎么修改
这段代码存在一些错误,需要进行修改,具体如下:
1. 在函数 generateVertices 中,数组 verticesColors[] 的下标没有被正确设置,应该是 verticesColors[i]。
2. 在函数 setup 中,第二个 glEnableClientState(GL_COLOR_ARRAY) 应该是 glEnableClientState(GL_COLOR_ARRAY);。
3. 在函数 setup 中,第一个 glVertexPointer 函数应该是 glColorPointer。
4. 在函数 drawScene 中,应该使用 glDrawArrays(GL_POLYGON, 0, numVertices) 来绘制圆形。
修改后的代码如下:
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)