C++ glm:不是类型或命名空间吗?我头上的错误

C++ glm:不是类型或命名空间吗?我头上的错误,c++,opengl,glm-math,C++,Opengl,Glm Math,我正在关注(我知道在本教程中还没有定义从GLFW2到3的更改,我已经纠正了这些方面 然而,当在完成应该工作的内容后尝试编译时,我得到了一个错误,因为我的头文件没有将glm识别为名称空间,因此它假设int,这破坏了程序的其余部分 我的标题如下: #ifndef CONTROLS_HPP #define CONTROLS_HPP void computeMatricesFromInputs(); glm::mat4 getViewMatrix(); glm::mat4 getProjecti

我正在关注(我知道在本教程中还没有定义从GLFW2到3的更改,我已经纠正了这些方面

然而,当在完成应该工作的内容后尝试编译时,我得到了一个错误,因为我的头文件没有将glm识别为名称空间,因此它假设int,这破坏了程序的其余部分

我的标题如下:

#ifndef CONTROLS_HPP
#define CONTROLS_HPP

 void computeMatricesFromInputs();
 glm::mat4 getViewMatrix();
 glm::mat4 getProjectionMatrix();

#endif
我的.cpp文件如下:

// Include GLFW
#include <glfw3.h>
extern GLFWwindow* window;

// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
using namespace glm;

#include "Outputs/controls.hpp"

glm::mat4 ViewMatrix;
glm::mat4 ProjectionMatrix;

glm::mat4 getViewMatrix(){
return ViewMatrix;
}
glm::mat4 getProjectionMatrix(){
return ProjectionMatrix;
}


// Initial position : on +Z
glm::vec3 position = glm::vec3(0, 0, 5);
// Initial horizontal angle : toward -Z
float horizontalAngle = 3.14f;
// Initial vertical angle : none
float verticalAngle = 0.0f;
// Initial Field of View
float initialFoV = 45.0f;

float speed = 3.0f; // 3 units / second
float mouseSpeed = 0.005f;



void computeMatricesFromInputs(){

// glfwGetTime is called only once, the first time this function is called
static double lastTime = glfwGetTime();

// Compute time difference between current and last frame
double currentTime = glfwGetTime();
float deltaTime = float(currentTime - lastTime);

// Get mouse position
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);

// Reset mouse position for next frame
glfwSetCursorPos(window, 1024 / 2, 768 / 2);

// Compute new orientation
horizontalAngle += mouseSpeed * float(1024 / 2 - xpos);
verticalAngle += mouseSpeed * float(768 / 2 - ypos);

// Direction : Spherical coordinates to Cartesian coordinates conversion
glm::vec3 direction(
    cos(verticalAngle) * sin(horizontalAngle),
    sin(verticalAngle),
    cos(verticalAngle) * cos(horizontalAngle)
    );

// Right vector
glm::vec3 right = glm::vec3(
    sin(horizontalAngle - 3.14f / 2.0f),
    0,
    cos(horizontalAngle - 3.14f / 2.0f)
    );

// Up vector
glm::vec3 up = glm::cross(right, direction);

// Move forward
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS){
    position += direction * deltaTime * speed;
}
// Move backward
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS){
    position -= direction * deltaTime * speed;
}
// Strafe right
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS){
    position += right * deltaTime * speed;
}
// Strafe left
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS){
    position -= right * deltaTime * speed;
}

float FoV = initialFoV;

// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
ProjectionMatrix = glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f);
// Camera matrix
ViewMatrix = glm::lookAt(
    position,           // Camera is here
    position + direction, // and looks here : at the same position, plus "direction"
    up                  // Head is up (set to 0,-1,0 to look upside-down)
    );

// For the next frame, the "last time" will be "now"
lastTime = currentTime;
}
//包括GLFW
#包括
外部GLFWwindow*窗口;
//包括GLM
#包括
#包括
#包括
使用名称空间glm;
#包括“输出/控制.hpp”
glm::mat4视图矩阵;
glm::mat4投影矩阵;
glm::mat4 getViewMatrix(){
返回视图矩阵;
}
glm::mat4 getProjectionMatrix(){
返回投影矩阵;
}
//初始位置:on+Z
glm::vec3位置=glm::vec3(0,0,5);
//初始水平角:朝向-Z
浮动水平角=3.14f;
//初始垂直角:无
浮动垂直角=0.0f;
//初始视野
浮动初始FOV=45.0f;
浮动速度=3.0f;//3个单位/秒
浮动鼠标速度=0.005f;
void computeMatricesFromInputs(){
//glfwGetTime只调用一次,即第一次调用此函数时
静态双lastTime=glfwGetTime();
//计算当前帧和最后一帧之间的时间差
double currentTime=glfwGetTime();
浮动时间=浮动时间(currentTime-lastTime);
//获取鼠标位置
双XPO,YPO;
glfwGetCursorPos(窗口、xpos和YPO);
//重置下一帧的鼠标位置
glfwSetCursorPos(窗口,1024/2768/2);
//计算新方向
水平角度+=鼠标速度*浮动(1024/2-xpos);
垂直角+=鼠标速度*浮动(768/2-ypos);
//方向:球坐标到笛卡尔坐标的转换
glm::vec3方向(
cos(垂直角)*sin(水平角),
sin(垂直角),
cos(垂直角)*cos(水平角)
);
//右向量
glm::vec3 right=glm::vec3(
sin(水平角-3.14f/2.0f),
0,
cos(水平角-3.14f/2.0f)
);
//上向量
glm::vec3 up=glm::cross(右,方向);
//前进
如果(glfwGetKey(窗口,GLFW_键向上)==GLFW_按){
位置+=方向*增量时间*速度;
}
//后退
如果(glfwGetKey(窗口,GLFW_键向下)=GLFW_按){
位置-=方向*增量时间*速度;
}
//扫射权
如果(glfwGetKey(窗口,GLFW_键右)==GLFW_按){
位置+=右侧*增量时间*速度;
}
//左扫射
如果(glfwGetKey(窗口,GLFW_键左)==GLFW_按){
位置-=右侧*增量时间*速度;
}
浮动视野=初始视野;
//投影矩阵:45°视场,4:3比例,显示范围:0.1单位100单位
ProjectionMatrix=glm::透视图(视野,4.0f/3.0f,0.1f,100.0f);
//摄像机矩阵
ViewMatrix=glm::lookAt(
位置://摄像机在这里
位置+方向,//看这里:在相同的位置,加上“方向”
向上//头部向上(设置为0,-1,0以倒置)
);
//对于下一帧,“最后一次”将是“现在”
lastTime=当前时间;
}

我很困惑这里发生了什么,使用glm的所有其他方面都很好。

Borgeader已经为我回答了这个问题(谢谢!)。它也通过在标题中使用包含glm来解决。

您应该在controls.hpp中包含所需的glm内容(包含
glm::mat4
)你指的是变量吗?不,在controls.hpp中包含定义glm::mat4的glm标头。对不起,我现在明白你的意思了,谢谢你,它已经解决了。但是,有人告诉我,我不应该在标头中使用include,我应该保留在.cpp文件中使用它们。另外,教程编译得很好,没有在那里使用include,你有可能ld解释这是怎么可能的?“但是,有人告诉我不应该在标题中使用include,我应该保留在.cpp文件中使用它们。”什么!?