#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);
}
}

glsl 多重纹理-LMLPHP

glsl 多重纹理-LMLPHP

顶点着色器代码

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 多重纹理-LMLPHP

05-11 20:06