gpt4 book ai didi

c++ - 使用现代 OpenGL 进行导航的 SDL 键盘和鼠标事件

转载 作者:行者123 更新时间:2023-11-28 06:27:34 24 4
gpt4 key购买 nike

<分区>

我在使用现代 OpenGL 的 SDL 中实现某些事件时遇到了一些麻烦。你们能帮帮我吗?

我关注了thebennybox's tutorials直到第3.5集。另外,我现在正在尝试实现 this guy's在我从 thebennybox 获得的代码上进行键盘和鼠标导航。

我使用 MacBook Air 13'。

到目前为止,我在 main.cpp 中有这段代码:

#include <GL/glew.h>
#include <GL/glfw3.h>
#include <OpenGL/glext.h>
#include <iostream>
#include "Display.h"
#include "Shader.h"
#include "Mesh.h"
#include "Camera.h"

int main(){
Camera camera;

Uint32 start;
SDL_Event event;
//glew 1.50 which glGenVertexArrays doesnt work without the
//glewExperimental flag. so it is glew version dependent
glewExperimental = GL_TRUE;

//Create Window with SDL
Display display(800, 600, "OpenGL Terrain");

//Create the vertices we want to draw
Vertex vertices[] = { Vertex(glm::vec3(-0.5,-0.5,0)),
Vertex(glm::vec3(0,0.5,0)),
Vertex(glm::vec3(0.5,-0.5,0))};

//Send the vertices to GPU with mesh function
Mesh mesh(vertices, sizeof(vertices)/sizeof(vertices[0]) );

//Create vertex and fragment shaders
Shader shader("../shaders/shader");

//render
while(!display.IsClosed()){
//Clear the window
display.Clear(0.0f,0.15f,0.23f,1.0f);

//Bind the shaders
shader.Bind();

//Draw the vertices
mesh.Draw();

//Show it all
display.Update();

//Call to functions of the Camera Class
camera.Control(0.2,0.2,false);
camera.updateCamera();
}

return 0;
}

这在 Camera.cpp 中:

 #include "Camera.h"


bool Camera::mouseInsideOfWindow = false;

Camera::Camera() {

Camera::cameraPositionX = 0.0f;
Camera::cameraPositionY = 0.0f;
Camera::cameraPositionZ = 5.0f;
Camera::cameraPitch = 0.0f;
Camera::cameraYaw = 0.0f;


}
void Camera::lockCamera(){
//Put some restriction for the view
if(cameraPitch > 90.0f){cameraPitch = 90.0f;}
if(cameraPitch < -90.0f){cameraPitch = -90.0f;}
if(cameraYaw < 0.0f){cameraYaw += 360.0f;}
if(cameraYaw > 360.0f){cameraYaw -= 360.0f;}
}
void Camera::moveCamera(float distance, float direction){
//convert the radius to angles
float rad = (cameraYaw+direction)*M_PI/180.0f;
cameraPositionX -= sin(rad)*distance;
cameraPositionZ -= cos(rad)*distance;
}
void Camera::moveCameraUp(float distance, float direction){
float rad = (cameraPitch+direction)*M_PI/180.0f;
cameraPositionY += sin(rad)*distance;
}
void Camera::Control(float moveVelocity,float mouseVelocity,bool mouseInsideWindow){

//if the mouse is inside the window
if(mouseInsideWindow){
//std::cout << "Innan " << std::endl;
int centerWindowX = 400;
int centerWindowY = 300;
SDL_ShowCursor(SDL_DISABLE); // dont show the mouse curser
int tempX, tempY;

SDL_GetMouseState(&tempX,&tempY); // get the points where the mouse is
cameraYaw += mouseVelocity*(centerWindowX-tempX); // calculate yaw
cameraPitch += mouseVelocity*(centerWindowY-tempY); // calculate pitch
lockCamera(); // lock the view
// Put back the curser in center
SDL_WarpMouseInWindow(NULL,centerWindowX,centerWindowY);

const Uint8 *state = SDL_GetKeyboardState(NULL);
//Move the Camera
if(state[SDLK_w]){
std::cout << "W" << std::endl;
if(cameraPitch != 90 && cameraPitch != -90){
moveCamera(moveVelocity,0.0);
moveCameraUp(moveVelocity,0.0);
}
}
else if(state[SDLK_s]){
std::cout << "S" << std::endl;
if(cameraPitch != 90 && cameraPitch != -90){
moveCamera(moveVelocity,180.0);
moveCameraUp(moveVelocity,180.0);
}
}
if(state[SDLK_a]){
std::cout << "A" << std::endl;
moveCamera(moveVelocity,90.0);
}
else if (state[SDLK_d]){
std::cout << "D" << std::endl;
moveCamera(moveVelocity,270.0);
}

//Close the window

}
//Rotate the Camera
glRotatef(-cameraPitch,1.0,0.0,0.0);
glRotatef(-cameraYaw,0.0,1.0,0.0);

}
void Camera::updateCamera(){
glTranslatef(-cameraPositionX,-cameraPositionY,cameraPositionZ);
}
Camera::~Camera() {}

所以我不确定我错过了什么。我有一种感觉,它与 SDL_Event 有关吗?我需要做什么来解决这个问题?

基本上我想要的是能够使用 (w,s,a,d) 移动并使用鼠标控制方向。

我需要添加其余代码吗?有人可以帮助我吗?

我让自己明白了吗?否则请询问。

附注窗口中的简单三角形示例工作正常,但导航不行。

/K

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com