Hey Guy,
I got stuck in multi texturing and really long for some help. This is a part of the university assignment; I have to apply texture via GLSL. I really have no idea where the problem is. I am putting some parts of my code here and hope for receiving some advice. The shader and the main application file are in the separate files.
Thanks a million
const GLchar* VertexShader =
{
"#version 330
"\
"layout(location=0) in vec4 in_Pos;
"\
"layout(location=1) in vec4 in_Color;
"\
"layout(location=2) in vec4 vNor;
"\
"layout(location=3) in vec2 vtexcoord;
"\
"out vec4 ex_Color;
"\
"out vec2 tex_coord;
"\
"uniform mat4 ModelViewMatrix;
"\
"uniform mat4 ProjectionMatrix;
"\
"void main(void)
"\
"{
"\
" gl_Position = ProjectionMatrix*ModelViewMatrix*in_Pos/in_Pos.w;
"\
" ex_Color = in_Color;
"\
" tex_coord = vtexcoord;
"\
"}
"
};
const GLchar* FragmentShader =
{
"#version 330
"\
"in vec4 ex_Color;
"\
"in vec2 tex_coord;
"\
"uniform int VAONumber;
"\
"uniform sampler2D mytexP;
"\
"uniform sampler2D mytexR;
"\
"out vec4 out_Color;
"\
"void main(void)
"\
"{
"\
" out_Color = ex_Color;
"\
" if (VAONumber == 1) out_Color = ex_Color;
"\
" if (VOANumber == 2) out_Color = ex_Color*texture(mytexP, vtexcoord.st) "
" if (VAONumber == 3) out_Color = ex_Color*texture(mytexR, vtexcoord.st);
"\
"}
"
};
Key Board Function:
case '1': glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]); printf("Text 0");break;
case '2': glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[1]); printf("Text 1");break;
case '3': glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[2]); printf("Text 2");break;
Render Function
void RenderFunction(void)
{
int i,j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glUniformMatrix4fv(ModelViewMatrixLocation, 1, 0, ModelViewMatrix);
glUniformMatrix4fv(ProjectionMatrixLocation, 1, 0, ProjectionMatrix);
VAONumber = 0; /* cube */
glUniform1i(VAOLocation, VAONumber);
glBindVertexArray(VAOIds[0]);
glDrawArrays(GL_TRIANGLES, 0, 48);
VAONumber = 2; /* plane */
glUniform1i(VAOLocation, VAONumber);
glUniform1i(textureLocP, 0);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAOIds[2]);
glDrawArrays(GL_TRIANGLES, 0, 6);
VAONumber = 1; /* roof */
glUniform1i(VAOLocation, VAONumber);
glUniform1i(textureLocR, 1);
glActiveTexture(GL_TEXTURE1);
glBindVertexArray(VAOIds[1]);
glDrawArrays(GL_TRIANGLES, 0, 12);
VAONumber = 3; /* axes */
glUniform1i(VAOLocation, VAONumber);
glBindVertexArray(VAOIds[3]);
glDrawArrays(GL_LINES, 0, 6);
glFlush();
glBindVertexArray(0);
}
and the function for creating the vertex objects:
void CreateVBO(void)
{
Vertex_t House_Tri[36]; /* vertices for cube triangles (3*triangles = 3*6*2) */
Vertex_t Roof_Tri[12]; /* vertices for roof triangles (3*triangles = 3*2*2) */
Vertex_t Plane_Tri[6]; /* 2 triangles */
int i,j, ind;
GLuint loc_tex;
Image *Teximage;
float v1[2], v2[2], v3[2], w1[2], w2[2], n1[2], n2[2], n3[2];
/* House (Cube) triangles */
ind = 0;
for (i=0; i<12; i++) { /* 12: number of triangles on a cube surface*/
for (j=0; j<3; j++) { /* three vertices in each triangle */
House_Tri[ind] = Cube_Ver[Cube_Ind[i*3+j]];
ind = ind + 1;
}
}
/* Roof triangles */
ind = 0;
for (i=0; i<4; i++) {
for (j=0; j<3; j++) {
Roof_Tri[ind] = Roof_Ver[Roof_Ind[i*3+j]];
ind = ind + 1;
}
}
/* triangles for the plane (colors and normals given in the data initialization) */
ind = 0;
for (i=0; i<2; i++) {
for (j=0; j<3; j++) {
Plane_Tri[ind] = Plane_Ver[Plane_Ind[i*3+j]];
ind = ind + 1;
}
}
/* texture coordinates for the plane (x,y,z) <-> (s,t) (always consists of two triangles) */
/* s t */
tex_coord[0][0] = 0.0; tex_coord[0][1] = 0.0; /* (s,t) (0,1) *********** (1,1) */
tex_coord[1][0] = 1.0; tex_coord[1][1] = 0.0; /* * * * */
tex_coord[2][0] = 1.0; tex_coord[2][1] = 1.0; /* * * * */
tex_coord[3][0] = 0.0; tex_coord[3][1] = 0.0; /* * * * */
tex_coord[4][0] = 1.0; tex_coord[4][1] = 1.0; /* * * * */
tex_coord[5][0] = 0.0; tex_coord[5][1] = 1.0; /* (0,0) *********** (1,0) */
for (ind=0;ind<6;ind++) {
Plane_Tri[ind].St[0] = tex_coord[ind][0]; Plane_Tri[ind].St[1] = tex_coord[ind][1];
}
glGenTextures(3, textures);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, checker1);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, checker2);
glGenerateMipmap(GL_TEXTURE_2D);
Teximage = (Image *) malloc(sizeof(Image));
ImageLoad("crate.bmp", Teximage);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textures[2]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Teximage->sizeX, Teximage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE,Teximage->data);
glGenerateMipmap(GL_TEXTURE_2D);
for (ind=0;ind<6;ind++) {
Roof_Tri[ind].St[0] = tex_coord[ind][0]; Roof_Tri[ind].St[1] = tex_coord[ind][1];
Roof_Tri[6+ind].St[0] = tex_coord[ind][0]; Roof_Tri[6+ind].St[1] = tex_coord[ind][1];
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glGenVertexArrays(4, &VAOIds[0]);
glBindVertexArray(VAOIds[0]);
glGenBuffers(3, &VBOIds[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOIds[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(House_Tri), House_Tri, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4*14, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4*14, (GLvoid*)16);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 4*14, (GLvoid*)32);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArray(VAOIds[1]);
glGenBuffers(4, &VBOIds[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOIds[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(Roof_Tri), Roof_Tri, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4*14, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4*14, (GLvoid*)16);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 4*14, (GLvoid*)32);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 4*14, (GLvoid*)48);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(3);
glBindVertexArray(VAOIds[2]);
glGenBuffers(4, &VBOIds[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOIds[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(Plane_Tri), Plane_Tri, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4*14, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4*14, (GLvoid*)16);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 4*14, (GLvoid*)32);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 4*14, (GLvoid*)48);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(3);
glBindVertexArray(VAOIds[3]);
glGenBuffers(3, &VBOIds[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOIds[3]);
glBufferData(GL_ARRAY_BUFFER, sizeof(Axes), Axes, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4*14, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4*14, (GLvoid*)16);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 4*14, (GLvoid*)32);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
}