计算机图形学(四)——画⼀个彩⾊的三⾓形
画⼀个彩⾊的三⾓形
要想画⼀个彩⾊三⾓形只要在原来的代码中加上表⽰颜⾊的信息,主要区别为以下⼏点:
(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"
perl书籍"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){
if(glewInit()!= GLEW_OK){
std::cout <<"Failed to initialize GLEW"<<
javascript设计模式系统讲解与应用
std::endl;
glfwTerminate();
return-1;
}
//vs是顶点着⾊器,frag是边缘着⾊器
Shader shader =Shader("res/shaders/code.vs","res/shaders/code.fs");
//Triangle
GLfloat vertices[]=
{//位置信息(坐标轴[-1,1])            //颜⾊信息
怎样在asp下使用webservice
-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();
菜鸟工具 java在线编程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();
return0;
}
除此之外,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;
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;
}
position和location的区别}
~Shader(){
glDetachShader(this->Program, vertex);
glDetachShader(this->Program, fragment);
glDeleteShader(vertex);
glDeleteShader(fragment);
glDeleteShader(this->Program);
}
void Use(){
glUseProgram(this->Program);
}
};

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。