Opengl es 如何在OpenGLES中使用GL_三角形_条绘制直线?
目前我正在使用openGLES的简单游戏应用程序中工作,使用GL_Lines模式绘制一条线,工作正常,我想使用GL_TRIANGLE_STRIP绘制一条线,是否可以使用GL_TRIANGLE_STRIP绘制一条线 提前谢谢 我尝试了源代码:Opengl es 如何在OpenGLES中使用GL_三角形_条绘制直线?,opengl-es,gl-triangle-strip,Opengl Es,Gl Triangle Strip,目前我正在使用openGLES的简单游戏应用程序中工作,使用GL_Lines模式绘制一条线,工作正常,我想使用GL_TRIANGLE_STRIP绘制一条线,是否可以使用GL_TRIANGLE_STRIP绘制一条线 提前谢谢 我尝试了源代码: glClearColor(0.65f, 0.65f, 0.65f, 1.0f); //Background color changes, its like Red,Green,Blue,Alpha color. glClear(GL_COLOR
glClearColor(0.65f, 0.65f, 0.65f, 1.0f); //Background color changes, its like Red,Green,Blue,Alpha color.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[self.effect prepareToDraw];
const GLfloat line[] =
{
0.0f, 0.5f,
0.0f, -1.5f,
0.0f,-1.5f,
};
// Create buffer object array
GLuint bufferObjectNameArray;
// Have OpenGL generate a buffer name and store the buffer object array
glGenBuffers(1, &bufferObjectNameArray);
// Bind the buffer object array to the GL_ARRAY_BUFFER target buffer
glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray);
// Send the line data over to the target buffer
glBufferData(
GL_ARRAY_BUFFER, // the target buffer
sizeof(line), // the number of bytes to put into the buffer
line, // a pointer to the data being copied
GL_STATIC_DRAW); // the usage pattern of the data
// Enable vertex data to be fed down the graphics pipeline to be drawn
glEnableVertexAttribArray(GLKVertexAttribPosition);
// Specify how the GPU looks up the data
glVertexAttribPointer(
GLKVertexAttribPosition, // the currently bound buffer holds the data
2, // number of coordinates per vertex
GL_FLOAT, // the data type of each component
GL_FALSE, // can the data be scaled
2*4, // how many bytes per vertex (2 floats per vertex)
NULL); // offset to the first coordinate, in this case 0
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
需要复制直线的每个顶点并将其偏移:
const GLfloat line[] =
{
-1.0f, 0.5f, 1.0f, 0.5f,//0.0f, 0.5f,
-1.0f, 0.5f, 1.0f, 1.5f,//0.0f, -1.5f,
};
...
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
它只适用于具有预定义线方向的线。否则,您需要计算与直线方向垂直的偏移方向。我尝试了您的代码,输出显示三角形,但我只想画一条直线,请帮助我。调整偏移值(-1.0f和1.0f)。至于三角形,您是否将glDrawArrays(…)中的
3
更改为4
?能否请您告诉我,我不知道您场景的细节,因此可能只建议<代码>-1和1
(line[]
数组中的值)定义了本例中的线条厚度-2
。要使线条更细,请尝试使用例如0.5f
而不是1
。