How can I make a camera for my game?

I want to move camera with keyboard in OpenGL. I followed the LearnOpenGL Camera tutorial, but I have no idea how can I use them.
Here is my code:


#include <stdlib.h>

#include <GLFW/glfw3.h>

#include <GL/gl.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

GLFWwindow *window;

void startGame()
    float vertices[] = {
            -0.5f, -0.5f, 0.0f,
            0.5f, -0.5f, 0.0f,
            0.0f,  0.5f, 0.0f
    unsigned int VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void * )0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    float cameraSpeed = 0.05f;
    glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
    glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
    glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
    glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
        cameraPos += cameraSpeed * cameraFront;
    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
        cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
    if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
        cameraPos -= cameraSpeed * cameraFront;
    if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
        cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
    glUniformMatrix4fv(glGetUniformLocation(0, "view"), 1, GL_FALSE, &view[0][0]);

int main(int argc, char **argv)
    if (!glfwInit())
    GLFWmonitor *monitor = glfwGetPrimaryMonitor();
    const GLFWvidmode *mode = glfwGetVideoMode(monitor);
    glfwWindowHint(GLFW_RED_BITS, mode->redBits);
    glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
    glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
    glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
    window = glfwCreateWindow(mode->width, mode->height, "KGCraft", monitor, NULL);
    while (!glfwWindowShouldClose(window))

You should investigate further how to properly initiate the context. Shouldn’t there be a
initGLEW( … ) ?

and it would need callbacks that you should have set up too. That’s the functions that responds to your mouse move and such … they are checked when you call glfwPollEvents

You need to discerne between code that’s executed once (initiation code), code that applies to hardware input (callbacks) and called by poll-events and finally the loop() .

Currently you call startGame() 60 times/sec. Only the draw-call within should be called.

There should be some shaders too (initiation). They can be very simple though.

Once you’ve sat that up, you’ll need to check for errors when you try out the code.

I believe that all these problems are touched in the tutorial. Unless I’ve misunderstood your situation, you’r pretty far from goal. A lot of your code looks proper. Try draw just a triangle first. And, implement the error-stuff early; it’ll come in handy when you tap through your code, one line at the time. You could use std::cout and print to the console to check variables too.

Good hunting!