1 #include <iostream>
2 using namespace std;
3
4 #define GLEW_STATIC
5 #include <GL/glew.h>
6 #include <GLFW/glfw3.h>
7 #include "shader.h"
8
9 const unsigned int SCR_WIDTH = 800;
10 const unsigned int SCR_HEIGHT = 600;
11 void framebuffer_size_callback(GLFWwindow* window, int width, int height)
12 {
13 glViewport(0, 0, width, height);
14 }
15
16 void processInput(GLFWwindow* window)
17 {
18 if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
19 glfwSetWindowShouldClose(window, true);
20 }
21
22 int main()
23 {
24 glfwInit();
25 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
26 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
27 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
28
29 //创建一个glfw窗口
30 GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
31 if (!window)
32 {
33 cout << "Failed to create GLFW window!\n" << endl;
34 glfwTerminate();
35 return -1;
36 }
37
38 glfwMakeContextCurrent(window);
39 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); //改变窗口大小的回调函数
40
41 glewExperimental = GL_TRUE;
42 if (glewInit() != GLEW_OK)
43 {
44 cout << "Failed to create initialize GLEW!\n" << endl;
45 return -1;
46 }
47
48 Shader ourShader("E:\\C++\\1.txt", "E:\\C++\\2.txt");
49
50 GLfloat vertices[] = {
51 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
52 -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
53 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
54 };
55 unsigned int VBO, VAO;
56 glGenVertexArrays(1, &VAO);
57 glGenBuffers(1, &VBO);
58
59 glBindVertexArray(VAO);
60 glBindBuffer(GL_ARRAY_BUFFER, VBO);
61 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
62
63 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); //位置属性
64 glEnableVertexAttribArray(0); //启用这个属性
65
66 //glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); //颜色属性
67 //glEnableVertexAttribArray(1);
68
69 while (!glfwWindowShouldClose(window))
70 {
71 processInput(window);
72
73 glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
74 glClear(GL_COLOR_BUFFER_BIT);
75
76 ourShader.use();
77
78 //GLfloat offset = 0.5f;
79 //ourShader.setFloat("xOffset", offset);
80
81 glBindVertexArray(VAO);
82 glDrawArrays(GL_TRIANGLES, 0, 3);
83
84
85 glfwSwapBuffers(window);
86 glfwPollEvents();
87
88
89 }
90
91 glDeleteVertexArrays(1, &VAO);
92 glDeleteBuffers(1, &VBO);
93
94 glfwTerminate();
95
96 return 0;
97
98 }