Javier Ramírez
Javier Ramírez

Reputation: 1011

Change tone of texture with OpenGL/GLSL 1.2

I want the texture of a picture change of tone. I have the following code:

//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);

and these are my shaders:

//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;
}

The problem is that when I render my image is completely black ... What is wrong?

Upvotes: 0

Views: 213

Answers (1)

Prabindh
Prabindh

Reputation: 3504

The image data corresponding to tex1 does not appear to be loaded anywhere - you are missing a call to glTexImage2D, where this data can be loaded. If you check for glGetError after draw call, you will likely see an error.

Upvotes: 0

Related Questions