gpt4 book ai didi

c++ - glMultMatrix 在 glBegin() 内部不起作用

转载 作者:行者123 更新时间:2023-11-30 01:19:42 25 4
gpt4 key购买 nike

我正在创建一个程序,允许我在 3 个空间中绘制点,使用 Catmull-Rom 样条连接它们,然后围绕样条绘制一个圆柱体。我正在使用 GL_TRIANGLES_STRIP 以短间隔连接样条曲线周围绘制的点的圆圈,希望将它们连接在一起形成样条曲线周围的圆柱体。

我已经设法以这些间隔绘制完整的点圆,使用 GL_POINTS,并根据 Frenet Frame 将它们正确定向到直线上。不幸的是,要使用 GL_TRIANGLE_STRIP,我认为我需要在一组两个点圆之间一次绘制一个点。

我遇到的问题是 glMultMatrixglBegin 中似乎不起作用。下面的代码将绘制一圈点,但在原点,我用来平移和定位点圈的 glMultMatrix 中似乎不适用开始。有解决办法吗?

  //The matrixes that are applied to the circle of points
GLfloat M1[16]={
N1.x(),N1.y(),N1.z(),0,
B1.x(),B1.y(),B1.z(),0,
T1.x(),T1.y(),T1.z(),0,
fromPoint->x,fromPoint->y,fromPoint->z,1
};

GLfloat M2[16]={
N2.x(),N2.y(),N2.z(),0,
B2.x(),B2.y(),B2.z(),0,
T2.x(),T2.y(),T2.z(),0,
toPoint->x,toPoint->y,toPoint->z,1
};

glBegin(GL_TRIANGLE_STRIP);
GLfloat x, y;
GLfloat radius = 0.4f;
GLint pointCount = 180;
for (GLfloat theta = 0; theta < 2*M_PI; theta += (2*M_PI)/pointCount) {
x = radius * cos(theta);
y = radius * sin(theta);

// Now push a matrix, multiply it, draw a point and pop the matrix
glPushMatrix();
glMultMatrixf(& M1[0]);
// Draw the point here
glVertex3f(x, y, 0);
glPopMatrix();

// Do the same again for the second section
glPushMatrix();
glMultMatrixf(& M2[0]);
glVertex3f(x, y, 0);
glPopMatrix();
}
glEnd();

最佳答案

The problem I am having, is that the glMultMatrix doesn't seem to work when inside a glBegin

Unsurprising:

Only a subset of GL commands can be used between glBegin and glEnd. The commands are glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial, and glEdgeFlag. Also, it is acceptable to use glCallList or glCallLists to execute display lists that include only the preceding commands. If any other GL command is executed between glBegin and glEnd, the error flag is set and the command is ignored.

glMultMatrix() 之前 glBegin():

//The matrixes that are applied to the circle of points
GLfloat M1[16]=
{
N1.x(),N1.y(),N1.z(),0,
B1.x(),B1.y(),B1.z(),0,
T1.x(),T1.y(),T1.z(),0,
fromPoint->x,fromPoint->y,fromPoint->z,1
};

GLfloat M2[16]=
{
N2.x(),N2.y(),N2.z(),0,
B2.x(),B2.y(),B2.z(),0,
T2.x(),T2.y(),T2.z(),0,
toPoint->x,toPoint->y,toPoint->z,1
};

GLfloat x, y;
GLfloat radius = 0.4f;
GLint pointCount = 180;
for (GLfloat theta = 0; theta < 2*M_PI; theta += (2*M_PI)/pointCount)
{
x = radius * cos(theta);
y = radius * sin(theta);

// Now push a matrix, multiply it, draw a point and pop the matrix
glPushMatrix();
glMultMatrixf(& M1[0]);
// Draw the point here
glBegin(GL_POINTS);
glVertex3f(x, y, 0);
glEnd();
glPopMatrix();

// Do the same again for the second section
glPushMatrix();
glMultMatrixf(& M2[0]);
glBegin(GL_POINTS);
glVertex3f(x, y, 0);
glEnd();
glPopMatrix();
}

或者在客户端应用变换,然后将一大块顶点交给 OpenGL 以一次性渲染。

编辑:或者将那些矩阵乘法完全拉出循环:

GLfloat x, y;
GLfloat radius = 0.4f;
GLint pointCount = 180;

glPushMatrix();
glMultMatrixf(& M1[0]);
glBegin(GL_POINTS);
for (GLfloat theta = 0; theta < 2*M_PI; theta += (2*M_PI)/pointCount)
{
x = radius * cos(theta);
y = radius * sin(theta);

// Draw the point here
glVertex3f(x, y, 0);
}
glEnd();
glPopMatrix();

glPushMatrix();
glMultMatrixf(& M2[0]);
glBegin(GL_POINTS);
for (GLfloat theta = 0; theta < 2*M_PI; theta += (2*M_PI)/pointCount)
{
x = radius * cos(theta);
y = radius * sin(theta);

// Draw the point here
glVertex3f(x, y, 0);
}
glEnd();
glPopMatrix();

关于c++ - glMultMatrix 在 glBegin() 内部不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20552681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com