在现代 OpenGL 中,用 GL_TRIANGLE 奇怪的 z 轴行为绘制实心圆
In Modern OpenGL, drawing filled circle with GL_TRIANGLE weird z-axis behaviour
我正在尝试使用 GL_TRIANGLE 在现代 opengl 中绘制实心圆。我正在为圆形顶点填充圆形数组,然后我使用圆形顶点填充点数组以使用 triangle.I 中的顶点我使用 vec4(齐次坐标)并将顶点 z 轴值设置为 0.0 并使用默认投影应该每个轴的范围从 -1 到 1。但不知何故,我让所有屏幕都涂上了颜色而不是圆圈。奇怪的是,当我设置 [0-0.4] 范围以外的点的 z 轴值时,我可以绘制圆圈,当我增加 z 的值时,它就像透视一样,无论我设置什么半径,它都是当 z 增加时变小。我不明白为什么 z 值会那样。
using namespace Angel;
const int NumVertices = 90;
const double PI = 3.141592653589793238463;
const GLint width = 500;
const GLint height = 500;
vec4 points[NumVertices];
vec4 colors[NumVertices];
vec4 circle[30];
vec4 color = { 1.0,0.0,0.0,1.0 };
vec4 center = { 0.0,0.0,0.0,1.0 };
GLfloat radius = 0.5;
int Index = 0;
void triangle(int b, int c) {
colors[Index] = color; points[Index] = center; Index++;
colors[Index] = color; points[Index] = circle[b%30]; Index++;
colors[Index] = color; points[Index] = circle[c%30]; Index++;
}
void fill() {
for (int i = 0; i < 30; i++)
{
float angle = 2 * PI * i / 30;
vec4 point;
point.x = center.x + (GLfloat)cos(angle) * radius;
point.y = center.y + (GLfloat)sin(angle) * radius;
point.z = 0.0 ; // PROBLEM !!
circle[i] = point;
}
for (int i = 0; i <= 29; i++) {
triangle(i, i + 1);
}
}
void init()
{
fill();
// Create a vertex array object
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Create and initialize a buffer object
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors),
NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), points);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors);
// Load shaders and use the resulting shader program
GLuint program = InitShader("vshader.glsl", "fshader.glsl");
glUseProgram(program);
// set up vertex arrays
GLuint vPosition = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(vPosition);
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0));
GLuint vColor = glGetAttribLocation(program, "vColor");
glEnableVertexAttribArray(vColor);
glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(sizeof(points)));
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glutCreateWindow("PirateFace");
glewExperimental = GL_TRUE;
glewInit();
init();
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
顶点着色器:
#version 150
in vec4 vPosition;
in vec4 vColor;
out vec4 color;
void main()
{
gl_Position = vPosition;
color = vColor;
}
片段着色器:
#version 150
in vec4 color;
out vec4 fColor;
void main()
{
fColor = color;
}
I am using vec4(homogeneous coordinates)
您需要将 vec4
值的 w
组件正确初始化为 1.0
。
请注意,在大多数情况下,您不需要在输入顶点数组中存储齐次坐标。您可以在顶点着色器中保留 in vec4
并仅使用 3 维向量作为输入,GL 会自动将向量扩展为 (x,y,z,1)
.
我正在尝试使用 GL_TRIANGLE 在现代 opengl 中绘制实心圆。我正在为圆形顶点填充圆形数组,然后我使用圆形顶点填充点数组以使用 triangle.I 中的顶点我使用 vec4(齐次坐标)并将顶点 z 轴值设置为 0.0 并使用默认投影应该每个轴的范围从 -1 到 1。但不知何故,我让所有屏幕都涂上了颜色而不是圆圈。奇怪的是,当我设置 [0-0.4] 范围以外的点的 z 轴值时,我可以绘制圆圈,当我增加 z 的值时,它就像透视一样,无论我设置什么半径,它都是当 z 增加时变小。我不明白为什么 z 值会那样。
using namespace Angel;
const int NumVertices = 90;
const double PI = 3.141592653589793238463;
const GLint width = 500;
const GLint height = 500;
vec4 points[NumVertices];
vec4 colors[NumVertices];
vec4 circle[30];
vec4 color = { 1.0,0.0,0.0,1.0 };
vec4 center = { 0.0,0.0,0.0,1.0 };
GLfloat radius = 0.5;
int Index = 0;
void triangle(int b, int c) {
colors[Index] = color; points[Index] = center; Index++;
colors[Index] = color; points[Index] = circle[b%30]; Index++;
colors[Index] = color; points[Index] = circle[c%30]; Index++;
}
void fill() {
for (int i = 0; i < 30; i++)
{
float angle = 2 * PI * i / 30;
vec4 point;
point.x = center.x + (GLfloat)cos(angle) * radius;
point.y = center.y + (GLfloat)sin(angle) * radius;
point.z = 0.0 ; // PROBLEM !!
circle[i] = point;
}
for (int i = 0; i <= 29; i++) {
triangle(i, i + 1);
}
}
void init()
{
fill();
// Create a vertex array object
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Create and initialize a buffer object
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors),
NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), points);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors);
// Load shaders and use the resulting shader program
GLuint program = InitShader("vshader.glsl", "fshader.glsl");
glUseProgram(program);
// set up vertex arrays
GLuint vPosition = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(vPosition);
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0));
GLuint vColor = glGetAttribLocation(program, "vColor");
glEnableVertexAttribArray(vColor);
glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(sizeof(points)));
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glutCreateWindow("PirateFace");
glewExperimental = GL_TRUE;
glewInit();
init();
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
顶点着色器:
#version 150
in vec4 vPosition;
in vec4 vColor;
out vec4 color;
void main()
{
gl_Position = vPosition;
color = vColor;
}
片段着色器:
#version 150
in vec4 color;
out vec4 fColor;
void main()
{
fColor = color;
}
I am using vec4(homogeneous coordinates)
您需要将 vec4
值的 w
组件正确初始化为 1.0
。
请注意,在大多数情况下,您不需要在输入顶点数组中存储齐次坐标。您可以在顶点着色器中保留 in vec4
并仅使用 3 维向量作为输入,GL 会自动将向量扩展为 (x,y,z,1)
.