#include<iostream>
#define GLEW_STATIC
#include<GL/glew.h>
#include<GLFW/glfw3.h>
float vertices[] = {
//位置 //颜色
-0.5f, -0.5f, 0.0f, 1.0f, 0 , 0, //0 红
0.5f, -0.5f, 0.0f, 0, 1.0f, 0, //1 绿
0.0f, 0.5f, 0.0f, 0, 0, 1.0f ,//2 蓝
//0.5f, -0.5f, 0.0f,//第4、5行和第2、3行一样,第6行乱造的顶点
//0.0f, 0.5f, 0.0f,
0.8f, 0.8f, 0.0f, 1.0f, 0, 1.0f //3 最后没有逗号
};
//第4、5行和第2、3行一样,所以一共4个不同的顶点,有重复的
unsigned int indices[] = {
0,1,2, //第一个三角形
2,1,3 //第二个三角形
};
const char* vertexShaderSource =
"#version 330 core n "
"layout(location = 6) in vec3 aPos; n "
"layout(location = 7) in vec3 aColor; n "
"out vec4 vertexColor; n "
"void main() {n "
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);n "
" vertexColor = vec4(aColor.x, aColor.y, aColor.z, 1.0 ); n "
"} n ";
const char* fragmentShaderSource =
"#version 330 core n "
"in vec4 vertexColor; n "
" uniform vec4 ourColor; n "
"out vec4 FragColor; n "
"void main() {n "
" FragColor = vertexColor ;} n ";
void processInput(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//create window
GLFWwindow * window = glfwCreateWindow(800, 600, "my opengl window", NULL, NULL);
if (window == NULL) {
printf("create window failed.");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//init glew
glewExperimental = true;
if (glewInit() != GLEW_OK) {
printf("init glew failed.");
glfwTerminate();
return -1;
}
glViewport(0, 0,800, 600);
//glEnable(GL_CULL_FACE);//cull是剔除的意思
//glCullFace(GL_BACK);//这句会把第二个三角形剔除,如果填GL_FRONT,则第一个三角形会被剔除
//ctrl k c 可以添加注解‘//’
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);//这句会以线框模式画出
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
//位置属性
glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(6);
//颜色属性
glVertexAttribPointer(7, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)) );
glEnableVertexAttribArray(7);
while (!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(0.5, 0.2, 0.5, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
float timeValue = glfwGetTime();
float greenValue = (sin(timeValue) / 2.0f) + 0.5f;//sin在正负1之间,除2在正负0.5之间,+0.5在0到1之间
int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
glUseProgram(shaderProgram);
glUniform4f(vertexColorLocation, 0, greenValue, 0, 1.0f);
//glDrawArrays(GL_TRIANGLES, 0, 6);//这是triangle,画四边形要两个3角形,所以前面vertices6个顶点。
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}