我正在尝试在opengl项目上绘制一条线条。
如果我将glTranslatef函数用于变换矩阵,则绘制洋红色线条,如图所示:
移动视图时,线条在不同的点被打破,或正确绘制,或根本不绘制。
如果我手动翻译点,线条总是正确显示。
其他行(红色的:GL_LINE_LOOP,青色的:GL_LINES)是手动翻译并正常工作的。
以下是glTranslate的代码:
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef( offs_x, offs_y, 0);
glLineWidth(2.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertexPointer( 3, GL_FLOAT, 0, trailPoints );
glDrawArrays(GL_LINE_STRIP,0,numTrailPoints);
glPopMatrix();
这里是手工翻译的工作代码:
for (i=0; i< numTrailPoints; i++)
{
translatedTrailPoints[i].x = trailPoints[i].x + offs_x;
translatedTrailPoints[i].y = trailPoints[i].y + offs_y;
translatedTrailPoints[i].z = trailPoints[i].z;
}
glLineWidth(2.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertexPointer( 3, GL_FLOAT, 0, translatedTrailPoints);
glDrawArrays(GL_LINE_STRIP,0,numTrailPoints);
我在这里缺少什么?
编辑:
要完成此问题,以下是数据结构(为了更好的可读性,采用反向声明顺序):
vec3 translatedTrailPoints[C_MAX_NUM_OF_TRAIL_POINTS];
vec3 trailPoints[C_MAX_NUM_OF_TRAIL_POINTS];
typedef union
{
float array[3];
struct { float x,y,z; };
struct { float r,g,b; };
struct { float s,t,p; };
struct { vec2 xy; float zz; };
struct { vec2 rg; float bb; };
struct { vec2 st; float pp; };
struct { float xx; vec2 yz; };
struct { float rr; vec2 gb; };
struct { float ss; vec2 tp; };
struct { float theta, phi, radius; };
struct { float width, height, depth; };
struct { float longitude, latitude, altitude; };
struct { float pitch, yaw, roll; };
} vec3;
typedef union
{
float array[2];
struct { float x,y; };
struct { float s,t; };
} vec2;
我想第二个datenwolf的建议,但没有成功:我在vec2和vec3声明之前试过pragma pack(1 | 2 | 4),我尝试用/ Zp1编译/ Zp2 | / Zp4(我在VisualStudio 2008下),但断线/点仍然存在。
EDIT2:
纹理四边形的相同问题:
vec3 point;
point.x = lon;
point.y = lat;
point.z = 500;
glTranslatef( offs_x, offs_y, 0);
glBindTexture(GL_TEXTURE_2D, iconTextures[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(point.x-C_ICON_WORLD_SIZE, point.y-C_ICON_WORLD_SIZE, point.z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(point.x+C_ICON_WORLD_SIZE, point.y-C_ICON_WORLD_SIZE, point.z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(point.x+C_ICON_WORLD_SIZE, point.y+C_ICON_WORLD_SIZE, point.z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(point.x-C_ICON_WORLD_SIZE, point.y+C_ICON_WORLD_SIZE, point.z);
glEnd();
更改视图的结果:
EDIT3:
通过翻译(point.x + offs_x,point.y + offs_y,point.z)并删除glVertex定义中的点坐标,我能够纠正纹理四边形的情况。以前模式中的行为仍然困扰着我。
答案 0 :(得分:0)
尝试在glPushMatrix()和glPopMatrix()调用之间使用glLoadIdentity(),因为它会重置坐标系并将转换应用于新的矩阵。