本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
c++ opengl绘制三角形
奔跑的男人 · 751浏览 · 发布于2019-09-23 +关注

最近在看opengl,记录一下:

#include <glad/glad.h>
#include <gl/GL.h>
#include <GLFW/glfw3.h>
#include <string>
#include <iostream>
typedef unsigned int uint;
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
GLFWwindow* showWindowSomething();
// template<class T, int N>
void renderTriangle(int shaderProgram, unsigned int VAO, unsigned int VBO, unsigned int EBO,uint verticesSizeof,uint indicesSizeof,float* elementVertices,uint* indices);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int (*sum)(int a, int b);
int sumA(int (&arr)[5]);
void renderTwoTriangle(unsigned int VAO, unsigned int VBO, float* vertices, uint verticesSizeof);
int main()
{
/*int max;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max);
cout << "max is " << max << endl;*/
int temp[] = { 2,3,4,5,5 };
int len = sizeof(temp) / sizeof(int);
int (&arr)[5] = temp;
sumA(arr);
GLFWwindow* window = showWindowSomething();
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
 
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
unsigned int VAO, EBO, VBO,VAO1,VBO1;
int shaderProgram,yellowShaderProgram;
    // 原始的program
shaderProgram = glCreateProgram();
// 黄色的program
yellowShaderProgram = glCreateProgram();
glGenBuffers(1, &EBO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &VBO1);
glGenVertexArrays(1, &VAO);
glGenVertexArrays(1, &VAO1);
// 顶点着色器
const char* vertSharderCode = "#version 330 core\n"
"layout(location = 0) in vec3 aPos;\n"
"out vec4 outputColor;\n"
"void main()\n"
"{\n"
""
"gl_Position = vec4(aPos,1.0f);\n"
"   outputColor = vec4(aPos,1.0);\n"
"}";
// 声明顶点着色器
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertSharderCode, NULL);
glCompileShader(vertexShader);
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
cout << "vertex compile error" << endl;
cout << infoLog << endl;
}
// 片段着色器
const char* fragmentShaderCode = "#version 330 core\n"
"in vec4 outputColor;\n"
"out vec4 fragColor;\n"
"uniform vec4 outcolor;\n"
"void main(){\n"
"fragColor = outcolor;\n"
"}\n";
const char* fragmentShaderCode2 = "#version 330 core\n"
"out vec4 fragColor;\n"
"in vec4 outputColor;\n"
"uniform vec4 outColor;\n"
"void main(){\n"
"fragColor = outColor;\n"
"}\n";
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderCode, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
cout << "fragmentshader compile error" << endl;
cout << infoLog << endl;
}
// 编译第二个着色器
uint fragmentShader2 = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader2, 1, &fragmentShaderCode2, NULL);
glCompileShader(fragmentShader2);
// 附加到程序上
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
// 链接
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
cout << infoLog << endl;
cout << "链接程序失败" << endl;
}
glAttachShader(yellowShaderProgram, vertexShader);
glAttachShader(yellowShaderProgram, fragmentShader2);
glLinkProgram(yellowShaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteShader(fragmentShader2);
// 定义三角形所需要的顶点数据
/*float vertices[] = {
0.5f,-0.5f,0.0f,
0.0f,0.5f,0.0f,
-0.5f,-0.5f,0.0f
};*/
float elementVertices[] = {
0.5f, 0.5f, 0.0f,   // 右上角
0.5f, -0.5f, 0.0f,  // 右下角
-0.5f, -0.5f, 0.0f, // 左下角
-0.5f, 0.5f, 0.0f   // 左上角
};
unsigned int indices[] = {
0, 1, 3, // 第一个三角形
1, 2, 3  // 第二个三角形
};
uint eLen = sizeof(elementVertices);
uint iLen = sizeof(indices);
cout << "索引数组的sizeof is " << sizeof(indices) << endl;
// 绘制三角形
// renderTriangle(shaderProgram,VAO,VBO,NULL,vertices,nullptr);
// 绘制矩形
// renderTriangle(shaderProgram, VAO, VBO, EBO,eLen,iLen,elementVertices,indices);
// 绘制两个三角形
// 第一个三角形的坐标值
float firstTiangle[] = {
0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f,
1.0f,0.0f,0.0f
};
float secondTiangle[] = {
0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f,
-1.0f,0.0f,0.0f
};
renderTwoTriangle(VAO,VBO,firstTiangle,sizeof(firstTiangle));
renderTwoTriangle(VAO1,VBO1,secondTiangle,sizeof(secondTiangle));
 
cout << "送往显卡数据完成,准备渲染" << endl;
// 设置线框模式
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
 
// render a triangle
float timeValue = glfwGetTime();
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// 改变第一个shader的颜色值
float fredValue = (cos(timeValue) / 5.0f) + 0.8f;
float fgreenValue = (sin(timeValue) / 10.0f) + 0.9f;
float fblueValue = (sin(timeValue) / 5.0f) + 0.8f;
// 获取到shader中uniform变量的位置
int locationf = glGetUniformLocation(shaderProgram, "outcolor");
glUseProgram(shaderProgram);
glUniform4f(locationf,fredValue,fgreenValue,fblueValue,1.0f);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,3);
 
// 改变第二个shader的颜色
// 红色的分量
float redValue = (sin(timeValue) / 4.0f) + 0.75f;
// 绿色的分量
float greenValue = (sin(timeValue) / 2.0f) + 0.5f;
// 获取到声明的uniform变量的位置
int vertexColorLocation = glGetUniformLocation(yellowShaderProgram,"outColor");
glUseProgram(yellowShaderProgram);
glUniform4f(vertexColorLocation,redValue,greenValue,0.0f,1.0f);
glBindVertexArray(VAO1);
glDrawArrays(GL_TRIANGLES,0,3);
 
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glBindVertexArray(0);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// 删除对应的缓冲对象
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
// glfw: terminate, clearing all previously allocated GLFW resources.
// 回收占用的内存
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
int sumA(int (&arr)[5]) {
cout << "sizeof arr is " << sizeof(&arr) << endl;
cout << arr << endl;
return sizeof(&arr);
}
// 渲染具体的物体对象方法
/**
   绘制三角形对象
*/
void renderTriangle(int shaderProgram, unsigned int VAO, unsigned int VBO, unsigned int EBO, uint verticesSizeof, uint indicesSizeof, float* elementVertices, uint* indices)
{
cout << "顶点数组的sizeof is " << verticesSizeof << endl;
// 定义顶点缓冲对象 GL_ARRAY_BUFFER
glBindVertexArray(VAO);
// 绑定缓冲对象 修改opengl状态机从此以后任何的缓冲调用都会来配置当前绑定的缓冲
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, verticesSizeof, elementVertices, GL_/static_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesSizeof, indices, GL_/static_DRAW);
 
// 配置顶点属性指针
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
// 链接都对象后删除着色器对象我们不在需要了
// 数据已经送往了GPU内存中了可以解绑了
glBindBuffer(GL_ARRAY_BUFFER, 0);
 
glBindVertexArray(0);
}
//渲染两个相连的三角形
void renderTwoTriangle(unsigned int VAO,unsigned int VBO,float* vertices,uint verticesSizeof) {
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,verticesSizeof,vertices,GL_/static_DRAW);
// 配置顶点属性指针
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
 
}
GLFWwindow* showWindowSomething() {
// glfw: initialize and configure
// ------------------------------
glfwInit();
// glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
 
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "OpenGL_GAME", NULL, NULL);
 
return window;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and 
// height will be significantly larger than specified on retina displays.
cout << "width is " << width << "height is " << height << endl;
glViewport(0, 0, width, height);
}


20190921154429942.gif

相关推荐

图形学基础 | 实现OBJ文件的载入

iamitnan · 1702浏览 · 2019-05-29 10:10:17
干货!麻将平胡算法

· 998浏览 · 2019-06-06 11:45:17
Java桌球小游戏

奔跑的男人 · 644浏览 · 2019-06-11 09:37:46
图形用户界面和游戏开发

qq2360248666 · 716浏览 · 2019-06-11 09:57:01
Three.js模型隐藏或显示

吴振华 · 563浏览 · 2019-06-14 10:18:27
Cocos工程命名规则整理(node部分)

吴振华 · 882浏览 · 2019-06-14 10:24:18
加载中

0评论

评论
分类专栏
小鸟云服务器
扫码进入手机网页