【问题标题】:Texture mapping a circle纹理映射一个圆圈
【发布时间】:2014-12-02 06:58:05
【问题描述】:

首先,我使用以下资源来生成我的圈子。

http://slabode.exofire.net/circle_draw.shtml

现在我正在尝试使用以下方法将纹理应用于圆形,但我似乎无法正确计算。

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(); 
}

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

【问题讨论】:

标签: c opengl textures


【解决方案1】:

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

float tx = (x/r + 1)*0.5;
float ty = (y/r + 1)*0.5;

并在调用 glTexCoord 之前的内部循环中执行此操作。

附带说明,GL_TRIANGLE_FAN 对您的几何图形更有意义。为了简化拓扑,我将从位置 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...glEnd 即时模式,您应该考虑构建 VBO 并改为上传。

【讨论】:

  • tx 和 ty 的方程式成功了!我尝试使用 TRIANGLE_FAN,但由于某种原因,我的颜色出现了一些不希望的效果,并且图像没有正确映射。
  • 中心的纹理坐标看起来不对。我想你想要 (0.5f, 0.5f) 作为中心。我还会将坐标乘以r,而不是除以纹理坐标,因为乘法通常比除法更有效。并以浮点数计算所有内容,而不是部分使用双精度数。您还需要将第一个顶点作为最后一个顶点重复以创建一个封闭的圆。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多