Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

I need in a code to display cube on opengl glfw glew and c++

  1. User

    I need in a code to display cube on opengl glfw glew and c++

  2. Assistant

    Here is an example code for displaying a cube using OpenGL, GLFW, GLEW, and C++:

    #include <iostream>
    #include <GL/glew.h>
    #include <GLFW/glfw3.h>
    
    #define WIDTH 800
    #define HEIGHT 600
    
    const char* vertexShaderSource = "#version 330 core\n"
                                     "layout (location = 0) in vec3 aPos;\n"
                                     "void main()\n"
                                     "{\n"
                                     "   gl_Position = vec4(aPos, 1.0);\n"
                                     "}\0";
    
    const char* fragmentShaderSource = "#version 330 core\n"
                                       "out vec4 FragColor;\n"
                                       "void main()\n"
                                       "{\n"
                                       "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
                                       "}\n\0";
    
    int main(void)
    {
        // Initialize GLFW
        if (!glfwInit()) {
            std::cout << "Failed to initialize GLFW.\n";
            return -1;
        }
    
        // Create a window
        GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Cube", NULL, NULL);
        if (!window) {
            std::cout << "Failed to create GLFW window.\n";
            glfwTerminate();
            return -1;
        }
    
        // Make the context of the window the current one
        glfwMakeContextCurrent(window);
    
        // Initialize GLEW
        if (glewInit() != GLEW_OK) {
            std::cout << "Failed to initialize GLEW.\n";
            glfwTerminate();
            return -1;
        }
    
        // Create the vertex shader object
        unsigned int vertexShader;
        vertexShader = glCreateShader(GL_VERTEX_SHADER);
    
        // Compile the vertex shader source code and check for errors
        glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
        glCompileShader(vertexShader);
        int success;
        char infoLog[512];
        glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
        if (!success) {
            glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
            std::cout << "Vertex shader compilation failed:\n" << infoLog << std::endl;
            glfwTerminate();
            return -1;
        }
    
        // Create the fragment shader object
        unsigned int fragmentShader;
        fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    
        // Compile the fragment shader source code and check for errors
        glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
        glCompileShader(fragmentShader);
        glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
        if (!success) {
            glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
            std::cout << "Fragment shader compilation failed:\n" << infoLog << std::endl;
            glfwTerminate();
            return -1;
        }
    
        // Create the shader program object
        unsigned int shaderProgram;
        shaderProgram = glCreateProgram();
    
        // Attach the vertex and fragment shaders to the shader program object
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
    
        // Link the shader program and check for errors
        glLinkProgram(shaderProgram);
        glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
        if (!success) {
            glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
            std::cout << "Shader program linkage failed:\n" << infoLog << std::endl;
            glfwTerminate();
            return -1;
        }
    
        // Delete the vertex and fragment shaders
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
    
        // Define the vertices of the cube
        float vertices[] = {
            // Front face
            -0.5f, -0.5f,  0.5f,
             0.5f, -0.5f,  0.5f,
             0.5f,  0.5f,  0.5f,
            -0.5f,  0.5f,  0.5f,
            // Back face
            -0.5f, -0.5f, -0.5f,
            -0.5f,  0.5f, -0.5f,
             0.5f,  0.5f, -0.5f,
             0.5f, -0.5f, -0.5f,
            // Top face
            -0.5f,  0.5f, -0.5f,
            -0.5f,  0.5f,  0.5f,
             0.5f,  0.5f,  0.5f,
             0.5f,  0.5f, -0.5f,
            // Bottom face
            -0.5f, -0.5f, -0.5f,
             0.5f, -0.5f, -0.5f,
             0.5f, -0.5f,  0.5f,
            -0.5f, -0.5f,  0.5f,
            // Right face
             0.5f, -0.5f, -0.5f,
             0.5f,  0.5f, -0.5f,
             0.5f,  0.5f,  0.5f,
             0.5f, -0.5f,  0.5f,
            // Left face
            -0.5f, -0.5f, -0.5f,
            -0.5f, -0.5f,  0.5f,
            -0.5f,  0.5f,  0.5f,
            -0.5f,  0.5f, -0.5f,
        };
    
        // Define the indices of the cube's triangles
        unsigned int indices[] = {
            0, 1, 2,
            0, 2, 3,
            4, 5, 6,
            4, 6, 7,
            8, 9, 10,
            8, 10, 11,
            12, 13, 14,
            12, 14, 15,
            16, 17, 18,
            16, 18, 19,
            20, 21, 22,
            20, 22, 23
        };
    
        // Create the VAO, VBO, and EBO
        unsigned int VAO, VBO, EBO;
        glGenVertexArrays(1, &VAO);
        glGenBuffers(1, &VBO);
        glGenBuffers(1, &EBO);
    
        // Bind the VAO, VBO, and EBO
        glBindVertexArray(VAO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    
        // Set the vertex attribute pointers
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
    
        // Unbind the VBO and VAO
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    
        // Set the clear color
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    
        // Render loop
        while (!glfwWindowShouldClose(window)) {
            // Clear the screen
            glClear(GL_COLOR_BUFFER_BIT);
    
            // Use the shader program
            glUseProgram(shaderProgram);
    
            // Draw the cube
            glBindVertexArray(VAO);
            glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
    
            // Swap the buffers
            glfwSwapBuffers(window);
    
            // Poll for events
            glfwPollEvents();
        }
    
        // Cleanup
        glDeleteVertexArrays(1, &VAO);
        glDeleteBuffers(1, &VBO);
        glDeleteBuffers(1, &EBO);
    
        // Terminate GLFW
        glfwTerminate();
        return 0;
    }
    

    This code defines the vertices and indices of a cube, creates and compiles vertex and fragment shaders, creates a shader program, creates a VAO, VBO, and EBO for the cube, sets the vertex attribute pointers, and enters a render loop that clears the screen, draws the cube, and swaps the buffers. It then cleans up the VAO, VBO, and EBO before terminating GLFW.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.