画一个彩色的三角形
要想画一个彩色三角形只要在原来的代码中加上表示颜色的信息,主要区别为以下几点:
(1)GLfloat vertices[]={}中新增了颜色信息
(2)下列几处有改动
三角形每个顶点处有六个信息,所以要把原来的3 * sizeof(GLfloat)改成6 * sizeof(GLfloat),(GLvoid*)0表示颜色信息,改成(GLvoid*)(3*sizeof(GLfloat)),因为只有三个表示颜色。
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
glEnableVertexAttribArray(1);
main.cpp
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include<GLFW/glfw3.h>
#include"Shader.h"
const GLint WIDTH = 800, HEIGHT = 600;
//顶点着色器
const GLchar* vertexShaderSource = "#version 330 core\n" //版本
"layout(location = 0) in vec3 position;\n" //position变量名;vec3(三维向量)类型,向量的每个分量都是浮点型;in表示这个变量要从外界输入
"void main()\n"
"{\n"
"gl_Position = vec4(position.x, position.y, position.z, 1.0f);\n" //gl_Position预保留的变量,包含顶点信息,不需要定义;齐次坐标器(增加一维)
"}";
//边缘着色器
const GLchar* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" //用 = 进行赋值
"}";
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Learn OpenGL B18080312", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if (window == nullptr) {
std::cout << "Failed to create GLFW window" <<
std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" <<
std::endl;
glfwTerminate();
return -1;
}
//vs是顶点着色器,frag是边缘着色器
Shader shader = Shader("res/shaders/code.vs", "res/shaders/code.fs");
//Triangle
GLfloat vertices[] =
{//位置信息(坐标轴[-1,1]) //颜色信息
-0.5f,-0.5f,0.0f, 1.0f,0.0f,0.0f,
0.5f,-0.5f,0.0f, 0.0f,1.0f,0.0f,
0.0f,0.5f,0.0f, 0.0f,0.0f,1.0f
};
//VAO将数据和显存中的物理地址相对应,数据放在VBO里,VAO与VBO成对出现
//一组数据有不同的解读方式,可能有多个VAO,一个VAO一定要对应一个VBO
//VAO想象成一个说明书
//EBO存储VAO和VBO的连接信息
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//把顶点数据传输到显存的物理地址上
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);//GL_ARRAY_BUFFER代表顶点信息
// 参数“0”对应VAO的第一行,参数“3”和“ GL_FLOAT”输入三个浮点型,“ GL_FALSE”表示不需要进行标准化
//“3 * sizeof(GLfloat)”表示步长,“(GLvoid*)0”第一个起始点在哪找
// GL_STATIC_DRAW不改动写入,对应了GL_DYNAMIC_DRAW
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);//顶点属性指针 n * sizeof(GLfloat)每隔n个数据获得定点信息
//允许使用
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while (!glfwWindowShouldClose(window)) {
glViewport(0, 0, screenWidth, screenHeight);
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
shader.Use();
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 6);//6为顶点数
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}
除此之外,code.vc和code.fs也需要做些许改动
code.vs
#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
out vec3 ourColor;
void main()
{
gl_Position = vec4(position.x, position.y, position.z, 1.0f);
ourColor=color;
};
code.fs
#version 330 core
in vec3 ourColor;
out vec4 color;
void main()
{
color = vec4(ourColor, 1.0f);
};
Shader.h
#pragma once
//将编译、链接的Shader部分放入Shader.h中
#include<string>
#include<fstream>
#include<sstream>
#include<iostream>
#include<GL/glew.h>
class Shader {
GLuint vertex, fragment;//两个着色器
public:
GLuint Program;
//Constructor;
Shader(const GLchar *vertexPath,const GLchar *fragmentPath) {//两个字符串常量,代表两个文件的位置
//Retrieve the source code from file
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions(std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::badbit);
try {//一般用try_catch来打开文件
//Open file
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
//Stream the code
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
//Close file
vShaderFile.close();
fShaderFile.close();
//Convert the stream to string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}catch (std::ifstream::failure e) {
std::cout << "ERROR:SHADER::FILE_NOT_SUCCESSFULLIY_READ" << std::endl;
}
const GLchar* vShaderCode = vertexCode.c_str();//字符串转换,转换成c语言的 字符串
const GLchar* fShaderCode = fragmentCode.c_str();
//2.Compile Shaders
GLint success;
GLchar infoLog[512];
vertex=glCreateShader(GL_VERTEX_SHADER);//vertex可以当作着色器的一个容器,GL_VERTEX_SHADER是一个顶点着色器
glShaderSource(vertex, 1, &vShaderCode, NULL);//1:只传一个;NULL:整个代码全都传输进去(最后一个参数表示传输长度)
glCompileShader(vertex);//获取编译状态
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);//把vertex的编译状态获得,传到success,最大可以包含512个,若不设置长度,那么有多少获得多少
if (!success) {//如果出现错误,就把infolog打出来
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n"
<< infoLog << std::endl;
}
fragment=glCreateShader(GL_FRAGMENT_SHADER);//GL_FRAGMENT_SHADER边缘着色器
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n"
<< infoLog << std::endl;
}
//Program
this->Program = glCreateProgram();//Program渲染管线
glCreateProgram();
glAttachShader(this->Program, vertex);
glAttachShader(this->Program, fragment);
glLinkProgram(this->Program);//链接
glGetProgramiv(this->Program, GL_LINK_STATUS, &success);//检查链接状态
if (!success) {
glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINK_FAILED\n"
<< infoLog << std::endl;
}
}
~Shader() {
glDetachShader(this->Program, vertex);
glDetachShader(this->Program, fragment);
glDeleteShader(vertex);
glDeleteShader(fragment);
glDeleteShader(this->Program);
}
void Use() {
glUseProgram(this->Program);
}
};