i am new in opengl and i need to help to fix my camera view using these codes;
SDL_Window*window1;
SDL_Event event;
class cam
{
glm::mat4 ViewMatrix;
glm::mat4 ProjectionMatrix;
double currentTime = 0;
double lastTime = 0;
float deltaTime = 0.0;
// Initial position : on +Z
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;
public:
void computeMatricesFromInputs()
{
currentTime = SDL_GetTicks();
lastTime = currentTime;
deltaTime = (float)(currentTime - lastTime) / 1000;
int xpos, ypos;
SDL_GetMouseState(&xpos, &ypos);
// 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 (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_UP:
position += direction * deltaTime * speed;
cout << "hello
";
break;
case SDLK_DOWN:
position -= direction * deltaTime * speed;
break;
case SDLK_RIGHT:
position += right * deltaTime * speed;
break;
case SDLK_LEFT:
position -= right * deltaTime * speed;
break;
default:
break;
}
}
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)
);
}
glm::mat4 getViewMatrix(){
return ViewMatrix;
}
glm::mat4 getProjectionMatrix(){
return ProjectionMatrix;
}
ands here my render
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
cam a;
a.computeMatricesFromInputs();
glm::mat4 ProjectionMatrix = a.getProjectionMatrix();
glm::mat4 ViewMatrix = a.getViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
MatrixID = glGetUniformLocation(programID, “MVP”);
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
codes maybe too many but i may think that the main error is the deltatime wrong usage or maybe theres something else error in my codes;