C 圆的纹理映射

C 圆的纹理映射,c,opengl,textures,C,Opengl,Textures,首先,我使用以下资源生成我的圆圈 现在我正试图用下面的方法将纹理应用到一个圆上,但我似乎无法正确计算 void drawCircle(float cx, float cy, float cz, float r, int points, float red, float green, float blue) { float theta; theta = 2 * PI / (float)points; float c = cosf(theta);//precalc

首先,我使用以下资源生成我的圆圈

现在我正试图用下面的方法将纹理应用到一个圆上,但我似乎无法正确计算

void drawCircle(float cx, float cy, float cz,
  float r, int points,
  float red, float green, float blue) 
{ 

  float theta;

  theta = 2 * PI / (float)points; 


  float c = cosf(theta);//precalculate the sine and cosine
  float s = sinf(theta);
  float t;
  int i;

  float x = r; //we start at angle = 0 
  float y = 0;

  float tx = c * 0.5 + 0.5;
  float ty = s * 0.5 + 0.5;

  glPushMatrix();
  glTranslatef(cx, cy, cz);

  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, newBelgTex);

  glBegin(GL_POLYGON); 
  // glColor3f(red/255.0, green/255.0, blue/255.0);
  for(i = 0; i < points; i++) 
  { 

    glTexCoord2f(tx, ty);
    glVertex2f(x, y);//output vertex 

    //apply the rotation matrix
    t = x;
    x = c * x - s * y;
    y = s * t + c * y;

   // Not sure how to update tx and ty
  }
  glVertex2f(-r, 0);


  glEnd();
  glPopMatrix(); 
}
void drawCircle(浮点数cx、浮点数cy、浮点数cz、,
浮点r,整数点,
浮动红、浮动绿、浮动蓝)
{ 
浮动θ;
θ=2*PI/(浮点)点;
float c=cosf(θ);//预先计算正弦和余弦
浮点数s=sinf(θ);
浮动t;
int i;
float x=r;//我们从角度=0开始
浮动y=0;
浮动tx=c*0.5+0.5;
浮点数ty=s*0.5+0.5;
glPushMatrix();
glTranslatef(cx,cy,cz);
glEnable(GL_纹理_2D);
glBindTexture(GL_TEXTURE_2D,newBelgTex);
glBegin(GL_多边形);
//glColor3f(红色/255.0,绿色/255.0,蓝色/255.0);
对于(i=0;i

我尝试了一些不同的方法,但在正确更新
tx
ty
方面似乎都失败了。

I从c和s计算tx和ty,从x和y计算它们。像这样:

float tx = (x/r + 1)*0.5;
float ty = (y/r + 1)*0.5;
在调用glTexCoord之前,在内部循环中执行此操作

另一方面,GL_三角形_风扇更适合您的几何图形。为了简化拓扑结构,我将从位于位置0,0,0的中心顶点开始。这也会去除循环后的最后一个顶点,即

  glBegin(GL_TRIANGLE_FAN); 
  glTexCoord2f(0,0);
  glVertex2f(0,0);
  for(i = 0; i < points; i++) 
  { 
    float const tx = (x/r + 1)*0.5;
    float const ty = (y/r + 1)*0.5;

    glTexCoord2f(tx, ty);
    glVertex2f(x, y);

    //apply the rotation matrix
    t = x;
    x = c * x - s * y;
    y = s * t + c * y;
  }
  glEnd();
glBegin(GL\u三角形\u扇);
glTexCoord2f(0,0);
glVertex2f(0,0);
对于(i=0;i

注意GLREST…GLIME立即模式被禁止,你应该考虑建立一个VBO并上传它。我尝试使用三角扇形,但由于某些原因,我的颜色出现了一些不需要的效果,并且图像没有正确映射。中心的纹理坐标看起来错误。我想你想要(0.5f,0.5f)的中心。我也会用
r
乘以坐标,而不是除以纹理坐标,因为乘法通常比除法更有效。并以浮点形式计算所有内容,而不是部分使用double。您还需要重复第一个顶点作为最后一个顶点来创建闭合圆。请参见此处:。问题是关于圆柱体的,但第一部分(顶盖)展示了如何画圆。