C ++小浮点值在传递给函数

时间:2017-04-13 18:58:31

标签: c++ floating-point

通过std :: cout-ing我的一些小浮点值,我在代码中发现了一个错误。我对C ++很陌生,但我认为这不是故意的功能。

这些值介于1和-1之间,类似于我的OpenGL转换所需的一组标准化坐标。

我正在使用SDL获取我的窗口坐标,并通过以下方式抵消每个坐标:

...
    case SDL_MOUSEMOTION:
            {
                xMouse=(event.motion.x-halfX)/(halfX);
                yMouse=(event.motion.y-halfY)/(halfY);



                std::cout<<"Mouse at: "<<xMouse<<','<<yMouse<<'\n';
            }

...

halfX和halfY是我窗口高度和宽度的一半,这是因为我希望我的(0,0)位于中心。

以下是此cout的一些印刷品:

Mouse at: 0.794444,-0.857639
Mouse at: 0.805556,-0.888889
Mouse at: 0.830556,-0.934028
Mouse at: 0.838889,-0.951389
Mouse at: 0.844444,-0.961806
Mouse at: 0.844444,-0.96875
Mouse at: 0.85,-0.972222
Mouse at: 0.852778,-0.979167
Mouse at: 0.855556,-0.982639
Mouse at: 0.861111,-0.986111
Mouse at: 0.863889,-0.989583
Mouse at: 0.866667,-0.993056
Mouse at: 0.869444,-0.996528
Mouse at: 0.872222,-1
Mouse at: 0.877778,-1

看起来很好。但是当我将值传递给这样的函数时:

void draw(Ship &_player, GLfloat _rot,int _xMouse, int _yMouse)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    std::cout<<"From draw(Ship,GLfloat,int,int):"<<'\n'<<"Mouse at: "<<_xMouse<<','<<_yMouse<<'\n';
    _player.followMouse(_xMouse,_yMouse);
}

我的参数值为:

From SDL_MOUSEMOTION:
Mouse at: 0.688889,-0.982639
From draw(Ship,GLfloat,int,int):
Mouse at: 0,0
From SDL_MOUSEMOTION:
Mouse at: 0.694444,-0.993056
From draw(Ship,GLfloat,int,int):
Mouse at: 0,0
From SDL_MOUSEMOTION:
Mouse at: 0.713889,-1
From draw(Ship,GLfloat,int,int):
Mouse at: 0,-1

我没有在函数调用之前编辑值,它看起来有点像我的编译器试图舍入值。但我不确定,以及如何解决它。

如果我们需要它,这是我的主要

#ifdef WIN32
  #include <Windows.h>
#endif

#if defined (__linux__) || defined (WIN32)
  #include <GL/gl.h>
#endif
#ifdef __APPLE__
  #include <OpenGL/gl.h>
#endif

#include <iostream>
#include <cstdlib>
#include "GameObjects.h"
#include "Ship.h"
#include "Camera.h"
#include "SDLOpenGL.h"

#undef main

// function to init the basic OpenGL scene for this demo
void initOpenGL();
// function to render our scene.
void draw(Ship &_player,GLfloat _rot,int _xMouse, int _yMouse);



int main(int argc, char *argv[])
{

    int winXLeng=720;
    int winYLeng=576;
    float halfX=winXLeng/2;
    float halfY=winYLeng/2;
    // create our SDLWindow
    SDLOpenGL win("GLFunctions Demo",100,100,winXLeng,winYLeng);
    // this makes sure the window is active for OpenGL calls, if we have
    // more than one window we need to call this for the window we want to
    // set OpenGL for
    win.makeCurrent();
    // setup our default OpenGL window state
    initOpenGL();
    Ship player;
    float xMouse=0.00f;
    float yMouse=0.00f;
    float newRot=0.00f;
    bool quit=false;
    while(!quit)
    {
      SDL_Event event;
      // grab the event from the window (note this explicitly calls make current)
      win.pollEvent(event);
      switch (event.type)
      {
        // this is the window x being clicked.
        case SDL_QUIT : quit = true; break;
        // now we look for a keydown event
        case SDL_KEYDOWN:
        {
          switch( event.key.keysym.sym )
          {
            // if it's the escape key quit
            case SDLK_ESCAPE :  quit = true; break;
            // make OpenGL draw wireframe
            case SDLK_e : glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); break;
            // make OpenGL draw solid
            case SDLK_r : glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); break;
            default : break;
          } // end of key process
        } // end of keydown
        //Now we look for Mouse movements
        case SDL_MOUSEMOTION:
        {
            xMouse=(event.motion.x-halfX)/(halfX);
            yMouse=(event.motion.y-halfY)/(halfY);



            std::cout<<"From SDL_MOUSEMOTION:"<<'\n'<<"Mouse at: "<<xMouse<<','<<yMouse<<'\n';
        }


        default : break;
      } // end of event switch
     // draw scene

     draw(player,newRot,xMouse,yMouse);
     // update the buffer so we can see what we have drawn.
     win.swapWindow();
    }

    return EXIT_SUCCESS;
}

void initOpenGL()
{
  // this sets the background colour
  glClearColor(0,0,0,1.0);
  // this is how big our window is for drawing
  glViewport(0,0,720,576);
  Camera::perspective(60,float(720/576),0.01,500);

  Camera::lookAt(Vec4(0,30,0.1),Vec4(0,0,0),Vec4(0,1,0));
  glDisable(GL_LIGHTING);
  //glEnable(GL_LIGHTING);
  //glEnable(GL_LIGHT0);
  glColor3f(1,1,0);
  glEnable(GL_COLOR_MATERIAL);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);

}

void draw(Ship &_player, GLfloat _rot,int _xMouse, int _yMouse)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    std::cout<<"From draw(Ship,GLfloat,int,int):"<<'\n'<<"Mouse at: "<<_xMouse<<','<<_yMouse<<'\n';
    _player.followMouse(_xMouse,_yMouse);
}

任何人都有线索?

提前致谢〜

编辑:nvm ...但我会记得下次检查我的参数类型...

0 个答案:

没有答案