用OpenGL / GLSL 1.2改变纹理色调

时间:2014-01-05 06:03:16

标签: c++ opengl glsl

我希望图片的纹理改变色调。我有以下代码:

//Function main

  ...

  GLuint idvert = glGetAttribLocation(prog,"posicion");
  GLuint idcol  = glGetAttribLocation(prog,"color");
  GLuint idtex1 = glGetAttribLocation(prog,"coordtx1");
  GLuint px     = glGetAttribLocation(prog,"px");
  GLuint unit1  = glGetUniformLocation(prog,"tex1");

  ...

float ctex[] = {1.0,0.0,
              0.0,1.0,
              1.0,1.0,
              0.0,0.0};

float data[] = {1.0, 1.0,-5.0,
             -1.0,-1.0,-5.0,
              1.0,-1.0,-5.0,
             -1.0, 1.0,-5.0};

GLuint ind[] = {0,1,2,0,3,1};
GLfloat col[] = {1.0,0.0,0.0,1.0};


glGenBuffers(1,&triangleVBO);
glBindBuffer(GL_ARRAY_BUFFER,triangleVBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(data),data,GL_STATIC_DRAW);

glGenBuffers(1,&triangleIND);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,triangleIND);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(ind),ind,GL_STATIC_DRAW);

glGenBuffers(1,&triangleCOL);
glBindBuffer(GL_ARRAY_BUFFER,triangleCOL);
glBufferData(GL_ARRAY_BUFFER,sizeof(col),col,GL_STATIC_DRAW);

glGenBuffers(1,&triangleT[0]);
glBindBuffer(GL_ARRAY_BUFFER,triangleT[0]);
glBufferData(GL_ARRAY_BUFFER,sizeof(ctex),ctex,GL_STATIC_DRAW);

//main loop

  glUseProgram(prog);
  glVertexAttrib1f(px,posicion);
  glVertexAttrib1fv(idcol,col);

  glActiveTexture(GL_TEXTURE0);
  glUniform1i(unit1,0);
  glBindTexture(GL_TEXTURE_2D,idtextura[0]);

  glEnableVertexAttribArray(idtex1);
  glEnableVertexAttribArray(idcol);
  glEnableVertexAttribArray(idvert);

  glBindBuffer(GL_ARRAY_BUFFER,triangleT[0]);
  glVertexAttribPointer(idtex1,2,GL_FLOAT,GL_FALSE,0,(void*)0);

  glBindBuffer(GL_ARRAY_BUFFER,triangleCOL);
  glVertexAttribPointer(idcol,4,GL_FLOAT,GL_FALSE,0,(void*)0);

  glBindBuffer(GL_ARRAY_BUFFER,triangleVBO);
  glVertexAttribPointer(idvert,3,GL_FLOAT,GL_FALSE,0,(void*)0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,triangleIND);
  glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,(void*)0);

  glBindTexture(GL_TEXTURE_2D,0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);

  glDisableVertexAttribArray(idvert);
  glDisableVertexAttribArray(idcol);
  glDisableVertexAttribArray(idtex1);
  glUseProgram(0);

这些是我的着色器:

//Vertex shader
#version 120

attribute vec4 posicion;
attribute vec4 color;
attribute vec2 coordtx1;
attribute float px;
varying vec2 UV1;
varying vec4 colfrag;
vec4 tmp;

void main(){
  tmp = vec4(posicion.x + px,posicion.y,posicion.z,1.0);
  gl_Position = gl_ModelViewProjectionMatrix * tmp;  
  colfrag = color;
  UV1 = coordtx1;
} 

//Fragment Shader

#version 120

uniform sampler2D tex1;
varying vec2 UV1;
varying vec4 colfrag;

void main(){
  vec4 color1 = texture2D(tex1,UV1);
  gl_FragColor = color1 * colfrag;
}

问题是当我渲染我的图像时是完全黑的......出了什么问题?

1 个答案:

答案 0 :(得分:0)

与tex1相对应的图像数据似乎没有在任何地方加载 - 您缺少对glTexImage2D的调用,此处可以加载此数据。如果您在绘制调用后检查glGetError,则可能会看到错误。