您现在的位置是:首页» windows系统» opengl颜色处理,如何用OpenGL显示图片

opengl颜色处理,如何用OpenGL显示图片

2024-07-13 02:16:15
本内容由系统网小编为大家分享,Windows系统安装教程、办公系统、软件怎么使用、软件使用教程、办公软件攻略等信息。第一种方法:在绘制过程中通过uniform变量将颜色信息传给GPU:1. 首先绘制程序定义变量:GLuint oc;2. 片

本内容由系统网小编为大家分享,Windows系统安装教程、办公系统、软件怎么使用、软件使用教程、办公软件攻略等信息。

第一种方法:在绘制过程中通过uniform变量将颜色信息传给GPU:

1. 首先绘制程序定义变量:

GLuint oc;

2. 片段着色器中添加

uniform vec3 objectColor;

3. 模型绘制前(循环中)加上:

oc=glGetUniformLocation(renderingProgram,"objectColor");

4. 根据需要的颜色定义:

glm::vec3 objectColor(1.0f, 0.0f, 0.0f);

5. 将值传给uniform变量

glUniform3fv(oc,1,glm::value_ptr(objectColor));

这样,就能根据需要变换不同的颜色了。

也可以通过函数将颜色值放到顶点数组中,和顶点一起传送给GPU处理:

1. //函数用来将颜色值添加到坐标值+法向值后面;第一个变量是顶点数组,第二个是数组长度, 第三个是颜色信息,第四个容器用于输出

void appendColor(float * vertex,int vectexLen,float * color,vector<float> &vec){//函数中如果要返回数组,需要定义成static(局部变量无法引用)//但是这时候要求数组的维数固定,所以最好是将数组部分拿到函数外面for(int m=0;m<vectexLen;m++){vec.push_back(vertex[m]);if((m+1)%6==0){vec.push_back(color[0]);vec.push_back(color[1]);vec.push_back(color[2]);}}}

//正方形有6个面,每个面有两个三角形,每个三角形有三个顶点,每个顶点有三个坐标

//有6*2*3*3=108个坐标值,加上法向坐标值,共有216个数值

float vertex[216]={-1.0f,1.0f,-1.0f,0.0f,0.0f,-1.0f,-1.0f,-1.0f,-1.0f,0.0f,0.0f,-1.0f,1.0f,-1.0f,-1.0f,0.0f,0.0f,-1.0f,1.0f,-1.0f,-1.0f,0.0f,0.0f,-1.0f,1.0f,1.0f,-1.0f,0.0f,0.0f,-1.0f,-1.0f,1.0f,-1.0f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0f,1.0f};int lenver=sizeof(vertex)/sizeof(float);

vector<float> vec1;appendColor(vertex,lenver,color1,vec1);int n = vec1.size();float arr1[n];for(int i=0; i<n; i++){arr1[i] = vec1[i];}

2. 在顶点着色器中定义输入变量:

layout (location=0) in vec3 position;

layout (location=1) in vec3 innormal;

layout (location=2) in vec3 VertexColor;

3. 解释并缓存数据

//启用指定属性glEnableVertexAttribArray(0);//数据解释:顶点着色器中location的值;顶点属性的维数;数据的类型;是否数据被标准化;步长,0-让OpenGL自己去识别;起始位置的偏移量glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)0);

//启用指定属性glEnableVertexAttribArray(1);glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)(3*sizeof(float)));

//启用指定属性glEnableVertexAttribArray(2);glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)(6*sizeof(float)));

glBufferData(GL_ARRAY_BUFFER, sizeof(arr1), arr1, GL_STATIC_DRAW);

4. 直接绘制图形

glBindVertexArray(vao[0]);glDrawArrays(GL_TRIANGLES,0,36);glBindVertexArray(0);

截图中的图片经过了渲染,有光照效果,否则看上去边角不清楚。

XTw.com.Cn系统网专业应用软件下载教程,免费windows10系统,win11,办公软件,OA办公系统,OA软件,办公自动化软件,开源系统,移动办公软件等信息,解决一体化的办公方案。

免责声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。内容仅供参考使用,不准确地方联系删除处理!

联系邮箱:773537036@qq.com

标签: 多个 两种 物体