glsl 多重纹理
#include"glsl.h"
void SHADER::drawBox()
{
glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glNormal3f( 0.0f, 0.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
} void SHADER::drawMuBox()
{
glBegin(GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0,0.0f, 0.0f);
glMultiTexCoord2f(GL_TEXTURE1,0.0f, 0.0f);
glVertex2f(-10.0 , -10.0 ); glMultiTexCoord2f(GL_TEXTURE0,1.0f, 0.0f);
glMultiTexCoord2f(GL_TEXTURE1,1.0f, 0.0f);
glVertex2f( , -10.0 ); glMultiTexCoord2f(GL_TEXTURE0,1.0f, 1.0f);
glMultiTexCoord2f(GL_TEXTURE1,1.0f, 1.0f);
glVertex2f(10.0 , 10.0 ); glMultiTexCoord2f(GL_TEXTURE0,0.0f, 1.0f);
glMultiTexCoord2f(GL_TEXTURE1,0.0f, 1.0f);
glVertex2f(-10.0 , 10.0);
glEnd();
}
void SHADER::setup()
{
init_light();
setShaders();
m_fps.setup();
m_img1.Load("2.jpg");
m_img2.Load("3.jpg");
}
char * SHADER::textFileRead(char *fn)
{ FILE *fp;
char *content = NULL; int count=; if (fn != NULL)
{
fp = fopen(fn,"rt"); if (fp != NULL)
{ fseek(fp, , SEEK_END);
count = ftell(fp);
rewind(fp); if (count > )
{
content = (char *)malloc(sizeof(char) * (count+));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
} void SHADER::setShaders()
{
char *vs = NULL,*fs = NULL,*fs2 = NULL;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
f2 = glCreateShader(GL_FRAGMENT_SHADER); vs = textFileRead("toonf2.vert");
fs = textFileRead("toonf2.frag"); const char * vv = vs;
const char * ff = fs; glShaderSource(v, , &vv,NULL);
glShaderSource(f, , &ff,NULL); free(vs);free(fs); glCompileShader(v);
glCompileShader(f); printShaderInfoLog(v);
printShaderInfoLog(f);
printShaderInfoLog(f2); p = glCreateProgram();
glAttachShader(p,v);
glAttachShader(p,f); glLinkProgram(p);
printProgramInfoLog(p); glUseProgram(p);
loc = glGetUniformLocation(p,"time"); samplerA = glGetUniformLocation(p, "texA");
glUniform1i(samplerA, ); // pass in texture samplerB = glGetUniformLocation(p, "texB");
glUniform1i(samplerB, ); // pass in texture
} void SHADER::draw()
{
static float angle =0.0f;
static float time =0.0f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();
glTranslatef(,-,-);
glRotatef(angle++,,,); glUniform1f(loc, time);
glScalef(6.0,6.0f,6.0);
//glutSolidTeapot(1);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_img1.ID);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,m_img2.ID);
// glBindTexture(GL_TEXTURE_2D,m_img1.ID);
drawBox();
//drawMuBox();
glDisable(GL_TEXTURE_2D);
time+=0.01f;
m_fps.update();
//printf("fps:%d \n",m_fps.getFPS()); } void SHADER::init_light()
{
static GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f }; // 环境光参数
static GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }; // 漫射光参数
static GLfloat LightPosition[]= { 0.0f, 100.0f, 2.0f, 1.0f }; // 光源位置 glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // 设置环境光
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // 设置漫射光
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // 设置光源位置 glEnable(GL_LIGHT1); // 启用一号光源
glEnable(GL_LIGHTING); } int SHADER::printOglError(char *file, int line)
{
//
// Returns 1 if an OpenGL error occurred, 0 otherwise.
//
GLenum glErr;
int retCode = ; glErr = glGetError();
while (glErr != GL_NO_ERROR)
{
printf("glError in file %s @ line %d: %s\n", file, line, gluErrorString(glErr));
retCode = ;
glErr = glGetError();
}
return retCode;
} void SHADER::printShaderInfoLog(GLuint obj)
{
int infologLength = ;
int charsWritten = ;
char *infoLog; glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength); if (infologLength > )
{
infoLog = (char *)malloc(infologLength);
glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
free(infoLog);
}
} void SHADER::printProgramInfoLog(GLuint obj)
{
int infologLength = ;
int charsWritten = ;
char *infoLog; glGetProgramiv(obj, GL_INFO_LOG_LENGTH,&infologLength); if (infologLength > )
{
infoLog = (char *)malloc(infologLength);
glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
free(infoLog);
}
}
顶点着色器代码
uniform float time;
varying float colorx;
varying vec4 TexCoord;
void main(void)
{
vec4 v = vec4(gl_Vertex);
float temp;
temp = sin(time*5.0);
colorx =temp;
if(colorx<0.2)
colorx =0.2;
if(temp<0.5)
temp=0.5;
if(temp>1.0)
temp =1.0;
//v =v*temp;
v.z =v.z*temp;
gl_TexCoord[] =gl_MultiTexCoord0;
TexCoord = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * v;
}
像素着色器代码:
varying float colorx; varying vec4 TexCoord;
uniform sampler2D texA;
uniform sampler2D texB;
void main(void)
{ vec3 color = texture2D(texA,gl_TexCoord[].st).rgb + texture2D(texB,gl_TexCoord[].st).rgb;
//vec3 color = texture2D(texB,gl_TexCoord[0].st).rgb;
//vec3 color = texture2D(texA,gl_TexCoord[0].st).rgb;
color.r =color.r*colorx;
color.g =color.g*colorx;
color.b =color.b*colorx;
gl_FragColor =vec4(color,1.0); }
效果:
glsl 多重纹理的更多相关文章
- cocos2d-js Shader系列3:多重纹理 multiple textures multiple samplers
上一篇,我们学习了怎么便捷的控制sprite的颜色,而这个都是默认一个texture的,如果要实现类似mask的效果,或者更个性化的多纹理效果,怎么实现呢? 这就是这一节需要介绍的内容. 例如上图的效 ...
- 【GLSL教程】(八)纹理贴图 【转】
http://blog.csdn.net/racehorse/article/details/6664717 简单的纹理贴图(Simple Texture) 为了在GLSL中应用纹理,我们需要访问每个 ...
- GLSL纹理贴图 【转】
转载:http://blog.csdn.net/hgl868/article/details/7872466 简单的纹理贴图(Simple Texture) 为了在GLSL中应用纹理,我们需要访问每个 ...
- Linux OpenGL 实践篇-5 纹理
纹理 在之前的实践中,我们所渲染的物体的表面颜色都是纯色或者根据顶点位置计算出的一个颜色,这种方式在表现物体细节方面是比较吃资源的,因为我们每增加一个细节,我们就需要定义更多的顶点及其属性.所以美术人 ...
- OpenGL纹理高级
矩形纹理 对于二维纹理来说,除了GL_TEXTURE_2D之外,使用GL_TEXTURE_RECTANGLE就可以使用矩形纹理. 矩形纹理几大特点: 不能Mip,只能加载glTexImage2D的le ...
- android openGL ES2 一切从绘制纹理開始
纹理.在openGL中,能够理解为载入到显卡显存中的图片.Android设备在2.2開始支持openGL ES2.0.从前都是ES1.0 和 ES1.1的版本号.简单来说,openGL ES是为了嵌入 ...
- SharpGL学习笔记(十六) 多重纹理映射
多重纹理就把多张贴图隔和在一起.比如下面示例中,一个表现砖墙的纹理,配合一个表现聚光灯效果的灰度图,就形成了砖墙被一个聚光灯照亮的效果,这便是所谓的光照贴图技术. 多重纹理只在OpenGL扩展库中才提 ...
- Cocos2D粒子发射器的纹理
每个例子发射器只能使用单个纹理发射粒子. 如果你需要在相同粒子效果中组合多重纹理,你将不得不创建多重的发射器节点并且决定谁的粒子将在其它粒子之上或之下显示.
- OpenGL chapter5 基础纹理
Chapter5 基础纹理 Contents: ==================================================== | 任务 | 使用的函数 ========== ...
随机推荐
- Python学习笔记11:标准库之文件管理(os包,shutil包)
1 os包 os包包含各种各样的函数,以实现操作系统的很多功能.这个包很庞杂.os包的一些命令就是用于文件管理. 我们这里列出最经常使用的: mkdir(path) 创建新文件夹.path为一个字符串 ...
- 【Android】3.15 短串分享功能
分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 短串分享是指,用户搜索查询后得到的每一个地理位置结果将会对应一条短串(短链接),用户可以通过短信.邮 ...
- Centos7安装TensorFlow
TensorFlow也火了一段时间,想想既然要研究NLP.为什么不好好应用一下Google开源的Deep Learning平台呢,一切还是先从搭建好开发环境開始. 非常多大神们也做了这方面的工作.汲取 ...
- JSP/Servlet中文乱码处理总结
下面的任何一条缺一不可,注意,我之所以全部都用的XXX,意思就是这几个最好全部都一致! 1.HTML中要用meta content="text/html; charset=XXX" ...
- ios开发中,xib加载view,loadNibNamed方法奔溃原因之一
xib中某一属性在代码中已删除,但在xib中没有解除关联
- Winform DatagridviewcomboboxColumn Disable Style
DataGridViewComboBoxCell cell =(DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex]; c ...
- TCP/IP状态详解[转]
TCP正常建立和关闭的状态变化 TCP连接的建立可以简单的称为三次握手,而连接的中止则可以叫做 四次握手. 建立连接 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建 ...
- eclipse安装中文补丁包
第一步:打开http://www.eclipse.org/babel/ 第二步:找到downloads. 第三步:点击Oxygen. 第四步:找到简体中文的zip插件并兵下载. 第五步:解压. 第六步 ...
- kafka_zookeeper_配置文件配置
server.properties配置如下: broker.id=0 num.network.threads=2 num.io.threads=8 socket.send.buffer.bytes=1 ...
- FTP内容
1.1.1 作业FTP部署. FTP工具或者浏览器默认使用的都是PASV模式连接FTP服务器 1.先检查有没有安装 rpm -q vsftpd 如果没有安装 yum install vsftp ...