OpenGL 학습 노트:색인 버퍼 대상 사용

6227 단어 OpenGL 학습 노트
#include 
#include 

#include 
#include 

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

//        
const char *vertexShaderSource = "#version 330 core
" "layout (location = 0) in vec3 aPos;
" "void main()
" "{
" " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
" "}\0"; // const char *fragmentShaderSource = "#version 330 core
" "out vec4 FragColor;
" "void main()
" "{
" " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
" "}
\0"; void framebuffer_size_callback(GLFWwindow *window, int width, int height) { // , glViewport(0, 0, width, height); } void processInput(GLFWwindow *window) { if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); } int main(int argc, char **argv) { // , , glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "mytest", NULL, NULL); if (!window) { std::cout << "Create Window Error!
"; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); // glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // glad // GLAD OpenGL 。 // GLFW glfwGetProcAddress, 。 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; glfwTerminate(); glfwDestroyWindow(window); return -1; } unsigned int vertexShader; // // // vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glCompileShader(vertexShader); int success; char infoLog[512] = { 0 }; // glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { // , glGetShaderInfoLog(vertexShader, sizeof(infoLog), NULL, infoLog); std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED
" << infoLog << std::endl; } // , int fragmentShader; fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glCompileShader(fragmentShader); glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { memset(infoLog, 0, sizeof(infoLog)); glGetShaderInfoLog(fragmentShader, sizeof(infoLog), NULL, infoLog); std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED
" << infoLog << std::endl; } /* , */ // unsigned int shaderProgram; shaderProgram = glCreateProgram(); // glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); // glLinkProgram(shaderProgram); // , glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if (!success) { memset(infoLog, 0, sizeof(infoLog)); glGetProgramInfoLog(shaderProgram, sizeof(infoLog), NULL, infoLog); std::cout << "ERROR::SHADER::PROGRAM::LINK_FAILED
" << infoLog << std::endl; } // glUseProgram(shaderProgram); // , , glDeleteShader(vertexShader); glDeleteShader(fragmentShader); /************************************************ * (OpenGL ), * , * , , * , ***********************************************/ // 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 ! 0, 1, 3, // 1, 2, 3 // }; unsigned int VBO; unsigned int VAO; unsigned int EBO; // glGenBuffers(1, &VBO); // glGenBuffers(1, &EBO); // glGenVertexArrays(1, &VAO); // ,,。 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); // , OpenGL glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); // glEnableVertexAttribArray(0); // , , // glVertexAttribPointer VBO , // glBindBuffer(GL_ARRAY_BUFFER, 0); // VAO, VAO VAO, 。 // VAO glBindVertexArray, VAO( VBO)。 glBindVertexArray(0); // while (!glfwWindowShouldClose(window)) { // processInput(window); // // glClear , , // glClearColor // , glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // glUseProgram(shaderProgram); // VAO, , , glBindVertexArray(VAO); // , , // glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // glDrawArrays // , glDrawArrays 。 // , 6, 6 。 // , GL_UNSIGNED_INT。 // EBO ( , ), 0。 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // , , VBO ( VAO ) 。 //glDrawArrays(GL_TRIANGLES, 0, 3); glfwPollEvents(); glfwSwapBuffers(window); Sleep(1); } glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glfwTerminate(); glfwDestroyWindow(window); return 0; }

좋은 웹페이지 즐겨찾기