|
| 1 | +#include <iostream> |
| 2 | +// GLEW |
| 3 | +#define GLEW_STATIC |
| 4 | +#include <GL/glew.h> |
| 5 | +// GLFW |
| 6 | +#include <GLFW/glfw3.h> |
| 7 | +// Other includes |
| 8 | +#include "shader.h" |
| 9 | + |
| 10 | +// Function prototypes |
| 11 | +void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); |
| 12 | + |
| 13 | +// Window dimensions |
| 14 | +const GLuint WIDTH = 800, HEIGHT = 600; |
| 15 | + |
| 16 | +// The MAIN function, from here we start the application and run the game loop |
| 17 | +int main() |
| 18 | +{ |
| 19 | + // Init GLFW |
| 20 | + glfwInit(); |
| 21 | + // Set all the required options for GLFW |
| 22 | + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); |
| 23 | + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); |
| 24 | + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); |
| 25 | + glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); |
| 26 | + |
| 27 | + // Create a GLFWwindow object that we can use for GLFW's functions |
| 28 | + GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr); |
| 29 | + glfwMakeContextCurrent(window); |
| 30 | + |
| 31 | + // Set the required callback functions |
| 32 | + glfwSetKeyCallback(window, key_callback); |
| 33 | + |
| 34 | + // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions |
| 35 | + glewExperimental = GL_TRUE; |
| 36 | + // Initialize GLEW to setup the OpenGL Function pointers |
| 37 | + glewInit(); |
| 38 | + |
| 39 | + // Define the viewport dimensions |
| 40 | + glViewport(0, 0, WIDTH, HEIGHT); |
| 41 | + |
| 42 | + |
| 43 | + //��ȡshader�ļ��������룬��shader.h���� |
| 44 | + Shader ourShader("shader.vs", "shader.frag"); |
| 45 | + |
| 46 | + |
| 47 | + // һά���飬ÿ��������һ���������ԣ�ǰ��������λ�����ԣ�������������ɫ���� |
| 48 | + GLfloat vertices[] = { |
| 49 | + // Positions // Colors |
| 50 | + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Right |
| 51 | + -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // Bottom Left |
| 52 | + 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // Top |
| 53 | + }; |
| 54 | + GLuint VBO, VAO;//�������㻺�壬���������������ڹ����������� |
| 55 | + glGenVertexArrays(1, &VAO);//�����������飬����һ����һ������������ʶ���� |
| 56 | + glGenBuffers(1, &VBO);//�������㻺�壬����һ����һ������������ʶ������ |
| 57 | + |
| 58 | + glBindVertexArray(VAO);//�������� |
| 59 | + glBindBuffer(GL_ARRAY_BUFFER, VBO);//���㻺�� |
| 60 | + //ָ���������������ԴΪvertices�����ĸ����������Կ���ι������������ݣ�GL_STATIC_DRWA������������ı� |
| 61 | + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); |
| 62 | + |
| 63 | + // ָ���������ԵĽ�����ʽ��������δӶ��㻺���ȡ��Ӧ�Ķ������Ժ���Ӧ����ɫ���ԡ�����˵��������ɫ�������֪��ȥ�ĸ��������Է�������ɫ�� |
| 64 | + //��ÿһ��������ԣ�������2�֣�һ��λ�����ԣ�������ɫ���ԣ����ÿ����������������һ�������λ�ú���ɫ |
| 65 | + |
| 66 | + //������ɫ����ʹ��layout(location = 0)������position�������Ե�λ��ֵ(Location)����˵�һ���������������Է��������� |
| 67 | + //������������λ�����Ե�ά�ȣ��������������������������ͣ�������:�Ƿ�����������壬����λ�����Ե����ֽڳ��ȣ����������ڻ��������е�ƫ����������ʼλ�� |
| 68 | + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); |
| 69 | + glEnableVertexAttribArray(0);//��������0����ΪĬ���ǽ��õ� |
| 70 | + |
| 71 | + // ����һ����Ӧ������ɫ���е�layout (location = 1) in vec3 color;��������˵����ɫ���Ե�ƫ������������������������verticesһ�� |
| 72 | + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); |
| 73 | + glEnableVertexAttribArray(1);//��������1. |
| 74 | + |
| 75 | + //�����������(Vertex Array Object, VAO)�ĺô����ǣ������ö�������ָ��ʱ����ֻ��Ҫ������Ĵ������ִ��һ�Σ�֮���ٻ��������ʱ��ֻ��Ҫ����Ӧ��VAO�����ˡ�������ѭ���еİ��ٽ�� |
| 76 | + glBindVertexArray(0); // ��� VAO |
| 77 | + // Game loop |
| 78 | + while (!glfwWindowShouldClose(window)) |
| 79 | + { |
| 80 | + // ����¼���������Ӧ�Ļص������������ĵ�key_callback���� |
| 81 | + glfwPollEvents(); |
| 82 | + |
| 83 | + // Render |
| 84 | + // Clear the colorbuffer |
| 85 | + glClearColor(0.2f, 0.3f, 0.3f, 1.0f);//��Ⱦ��ɫ����̨���� |
| 86 | + glClear(GL_COLOR_BUFFER_BIT);//���ǰ̨���� |
| 87 | + |
| 88 | + // Draw the triangle |
| 89 | + ourShader.Use();//������ɫ������ |
| 90 | + glBindVertexArray(VAO);//ÿ��ѭ�������ã�������VAO |
| 91 | + glDrawArrays(GL_TRIANGLES, 0, 3); |
| 92 | + glBindVertexArray(0);//��� |
| 93 | + |
| 94 | + // Swap the screen buffers |
| 95 | + glfwSwapBuffers(window); |
| 96 | + } |
| 97 | + // Properly de-allocate all resources once they've outlived their purpose |
| 98 | + glDeleteVertexArrays(1, &VAO); |
| 99 | + glDeleteBuffers(1, &VBO); |
| 100 | + // Terminate GLFW, clearing any resources allocated by GLFW. |
| 101 | + glfwTerminate(); |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +// Is called whenever a key is pressed/released via GLFW |
| 107 | +void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) |
| 108 | +{ |
| 109 | + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) |
| 110 | + glfwSetWindowShouldClose(window, GL_TRUE); |
| 111 | +} |
0 commit comments