# include <glad/glad.h>
# include <GLFW/glfw3.h> # include <iostream> float vertices[ ] = { 0.5f , 0.5f , 0.0f , 0.5f , - 0.5f , 0.0f , - 0.5f , - 0.5f , 0.0f , - 0.5f , 0.5f , 0.0f ,
} ; unsigned int indices[ ] = { 0 , 1 , 3 , 1 , 2 , 3
} ; using namespace std;
const unsigned int SCR_WIDTH = 800 ;
const unsigned int SCR_HEIGHT = 600 ;
void processInput ( GLFWwindow* window)
{ if ( glfwGetKey ( window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose ( window, true ) ; }
}
void framebuffer_size_callback ( GLFWwindow* window, int width, int height)
{ glViewport ( 0 , 0 , width, height) ;
}
const char * vertexShaderSource = R"( #version 460 corelayout (location = 0) in vec3 aPos;void main(){gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);}
)" ;
const char * fragmentShaderSource = R"( #version 460 coreout vec4 FragColor;void main(){FragColor = vec4(0.0f, 0.0f, 1.0f, 1.0f);}
)" ;
int main ( )
{ glfwInit ( ) ; if ( ! glfwInit ( ) ) { std:: cerr << "Failed to initialize GLFW" << std:: endl; return - 1 ; } glfwWindowHint ( GLFW_CONTEXT_VERSION_MAJOR, 4 ) ; glfwWindowHint ( GLFW_CONTEXT_VERSION_MINOR, 6 ) ; glfwWindowHint ( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE) ; GLFWwindow* window = glfwCreateWindow ( SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL" , nullptr , nullptr ) ; if ( window == nullptr ) { cout << "Failed to create GLFW window" << endl; glfwTerminate ( ) ; return - 1 ; } glfwMakeContextCurrent ( window) ; glfwSetFramebufferSizeCallback ( window, framebuffer_size_callback) ; if ( ! gladLoadGLLoader ( ( GLADloadproc) glfwGetProcAddress) ) { std:: cout << "Failed to initialize GLAD" << std:: endl; return - 1 ; } int vertexShader = glCreateShader ( GL_VERTEX_SHADER) ; 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 << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std:: endl; } int fragmentShader = glCreateShader ( GL_FRAGMENT_SHADER) ; glShaderSource ( fragmentShader, 1 , & fragmentShaderSource, NULL ) ; glCompileShader ( fragmentShader) ; glGetShaderiv ( fragmentShader, GL_COMPILE_STATUS, & success) ; glGetShaderiv ( fragmentShader, GL_COMPILE_STATUS, & success) ; if ( ! success) { glGetShaderInfoLog ( fragmentShader, sizeof ( infoLog) , nullptr , infoLog) ; std:: cerr << "Fragment shader compilation failed: " << infoLog << std:: endl; } int shaderProgram = glCreateProgram ( ) ; glAttachShader ( shaderProgram, vertexShader) ; glAttachShader ( shaderProgram, fragmentShader) ; glLinkProgram ( shaderProgram) ; glGetProgramiv ( shaderProgram, GL_LINK_STATUS, & success) ; if ( ! success) { glGetProgramInfoLog ( shaderProgram, 512 , NULL , infoLog) ; std:: cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std:: endl; } glDeleteShader ( vertexShader) ; glDeleteShader ( fragmentShader) ; unsigned int VBO, VAO, EBO; glGenVertexArrays ( 1 , & VAO) ; glBindVertexArray ( VAO) ; glGenBuffers ( 1 , & VBO) ; glBindBuffer ( GL_ARRAY_BUFFER, VBO) ; glBufferData ( GL_ARRAY_BUFFER, sizeof ( vertices) , vertices, GL_STATIC_DRAW) ; glGenBuffers ( 1 , & EBO) ; glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, EBO) ; glBufferData ( GL_ARRAY_BUFFER, sizeof ( vertices) , vertices, GL_STATIC_DRAW) ; glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeof ( indices) , indices, GL_STATIC_DRAW) ; glVertexAttribPointer ( 0 , 3 , GL_FLOAT, GL_FALSE, 3 * sizeof ( float ) , ( void * ) 0 ) ; glEnableVertexAttribArray ( 0 ) ; while ( ! glfwWindowShouldClose ( window) ) { processInput ( window) ; glClearColor ( 0.2f , 0.3f , 0.3f , 1.0f ) ; glClear ( GL_COLOR_BUFFER_BIT) ; glUseProgram ( shaderProgram) ; glBindVertexArray ( VAO) ; glDrawElements ( GL_TRIANGLES, 6 , GL_UNSIGNED_INT, 0 ) ; glPolygonMode ( GL_FRONT_AND_BACK, GL_LINE) ; glfwSwapBuffers ( window) ; glfwPollEvents ( ) ; } glDeleteVertexArrays ( 1 , & VAO) ; glDeleteBuffers ( 1 , & VBO) ; glDeleteBuffers ( 1 , & EBO) ; glDeleteProgram ( shaderProgram) ; glfwTerminate ( ) ; return 0 ;
}