我在使用現代OpenGL實現SDL中的某些事件時遇到了一些麻煩。你們能幫我嗎?SDL使用現代OpenGL導航的鍵盤和鼠標事件
我跟着這個傢伙教程 http://youtu.be/ftiKrP3gW3k?list=PLEETnX-uPtBXT9T-hD0Bj31DSnwio-ywh,直到教程3.5網格。此外,我現在試圖實現這個傢伙http://youtu.be/f-vxvZGI8R0?list=PL0AB023E769342AFE鍵盤和鼠標導航上我從「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)移動並使用鼠標指示。
我需要添加剩餘的代碼嗎?請有人可以幫我嗎?
我讓自己明白了嗎?否則請詢問。
p.s.窗口中的簡單三角形示例工作正常,但不是導航。
/K
你在哪裏投票事件? – Prismatic 2015-01-31 18:25:29
這就是問題所在,它認爲。我不知道在哪裏或如何去做。你能幫我嗎? – Kahin 2015-01-31 18:35:42