Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: MultiTexturing/GLSL

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    4

    MultiTexturing/GLSL

    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

    Code :
    const GLchar* VertexShader =
    {
    	"#version 330\n"\
     
    	"layout(location=0) in vec4 in_Pos;\n"\
    	"layout(location=1) in vec4 in_Color;\n"\
    	"layout(location=2) in vec4 vNor;\n"\
    	"layout(location=3) in vec2 vtexcoord;\n"\
     
    	"out vec4 ex_Color;\n"\
    	"out vec2 tex_coord;\n"\
     
    	"uniform mat4 ModelViewMatrix;\n"\
    	"uniform mat4 ProjectionMatrix;\n"\
     
    	"void main(void)\n"\
    	"{\n"\
    	"	gl_Position = ProjectionMatrix*ModelViewMatrix*in_Pos/in_Pos.w;\n"\
    	"	ex_Color = in_Color;\n"\
    	"	tex_coord = vtexcoord;\n"\
    	"}\n"
    };
     
    const GLchar* FragmentShader =
    {
    	"#version 330\n"\
     
    	"in vec4 ex_Color;\n"\
    	"in vec2 tex_coord;\n"\
     
    	"uniform int VAONumber;\n"\
    	"uniform sampler2D mytexP;\n"\
    	"uniform sampler2D mytexR;\n"\
     
    	"out vec4 out_Color;\n"\
     
    	"void main(void)\n"\
    	"{\n"\
    	"	out_Color = ex_Color;\n"\
    	"	if (VAONumber == 1) out_Color = ex_Color;\n"\
    	"   if (VOANumber == 2) out_Color = ex_Color*texture(mytexP, vtexcoord.st) "
    	"	if (VAONumber == 3) out_Color = ex_Color*texture(mytexR, vtexcoord.st);\n"\
     
     
    	"}\n"
    };

    Key Board Function:
    Code :
    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
    Code :
    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:
    Code :
    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);
     
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    Could you please state what's going wrong? What are we looking for here?

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    4
    Hello,

    I am expecting to be able to apply texture via keyboard commands, but getting wrong display. In fact, I am not sure that i am applying the texture in GLSL right. To be accurate, I have got some issues with following lines from fragment shader. I appreciate if you take look at it and let me have your comments.

    Thank you very much

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

  4. #4
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    Your render code just calls glActiveTexture but you aren't binding a texture (glBindTexture).

    I can see that you are binding some texture where it says case 0, case 1, case 2. You seem to bind to GL_TEXTURE0 only.
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    4
    I do texture binding via keyboard function. Hope not doing any mistake.

    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;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •