c++ - OpenGL 旋转问题

标签 c++ opengl sdl rotation

解决方案:

strtod 而不是 atof 解决了它。 谢谢!

TODO:将此作为接受的答案


我正在从一个 XML 文件中加载一个值,该值指示纹理应该旋转多少。

看起来像这样:

string rotv = ItemElem->FirstChild("Rotation")->FirstChild()->Value();
rotation = -(atof(rotv.c_str()))*57.2957795;

在我的关卡编辑器中,纹理被正确旋转; pi 等于 180 度等等。 但是在引擎中;他们不是,似乎小数点被忽略了。因此,例如,应旋转 3.14 弧度的纹理仅旋转 3 弧度,依此类推。 我尝试了很多不同的方法来确保包含小数,但我无法让它工作。

我像这样旋转纹理:

glBindTexture(GL_TEXTURE_2D, texture->GetImage());
glMatrixMode(GL_TEXTURE);

glLoadIdentity();
glTranslatef(0.5, 0.5, 0);
glRotatef(rotation, 0, 0, 1);
glTranslatef(-0.5, -0.5, 0);

glMatrixMode(GL_PROJECTION);

xml 文件中的值使用点而不是逗号,我将其转换为这样,我还单独存储小数点以方便调试。

 for (int x = 0; x < rotv.size(); x++)
     {
          if (afterComa)
          {
                  int temp = (int) rotv[x] - '0';
              if (temp <10 && temp >= 0)
              {
                  decimals[decimalPos] = temp;
                  decimalPos++;
              }
              else
              {
                  break;
              }

          }
         if (rotv[x] == '.')
         {
             rotv[x] = ',';
             afterComa = true;
          }
      }

float decimalValue = decimals[0]/10 + decimals[1]/100 + decimals[2]/1000 + decimals[3]/10000 + decimals[4]/100000 + decimals[5]/1000000;

小数已正确存储,我将它们添加到旋转值中,但仍然无济于事。

最佳答案

打破它,确保你认为正在发生的事情真的正在发生:

string rot_str = ItemElem->FirstChild("Rotation")->FirstChild()->Value();
float rot_rad = atof( rotv.c_str() );
float rot_deg = rot_rad * 57.295779;
rotation = rot_deg;

使用调试器逐步执行每个操作并验证值是否符合预期。

关于c++ - OpenGL 旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8662829/

相关文章:

c++ - C++ String length() 和 size() 哪个更快?

c++ - SublimeText C++ 防止在生成时运行

c++ - 更大字符串的最长公共(public)子序列?

c - OpenGL 4.1 : Vertex Array Object draws nothing

c - 使用SDL-C库,简单游戏中的鼠标运动错误

c++ - sleep 或等待不会停止整个程序?

c++ - 如何使用C++ 11声明静态查找表

OpenGL 不会使用左上角坐标进行渲染

windows - Qt 创建者 - LNK1104 : cannot open file "glu32.lib"

c++ - glCheckFramebufferStatus 返回 0 并且在 glGetError 中没有错误