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 7 of 7

Thread: How to map texture to primitive?

  1. #1
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    70

    Question How to map texture to primitive?

    hi ive been struggling with this. ive been reading the opengl programming guide and cant figure out how to map a texture to a primitive. my vertex shader looks like this.
    Code :
    const char *fshader =
     
    	        "#version 430 core                                                              \n"
     
    	        "                                                                               \n"
     
    	        "uniform sampler2D tex1;                                                           \n"
     
    	        "uniform sampler2D tex2;							\n"
     
    		"in vec2 texcoord;\n"
     
    		"                                                                               \n"
     
    	        "out vec4 color;                                                                \n"
     
    	        "                                                                               \n"
     
    	        "void main(void)                                                                \n"
     
    	        "{                                                                              \n"
     
    	        "    color = texture(tex1, texcoord) + texture(tex2, texcoord);                   \n"
     
    	        "}                                                                              \n";

    no matter what values i give to texcoord the images appear in the top right corner. im using SOIL.

  2. #2
    Member Regular Contributor
    Join Date
    Jan 2012
    Location
    Germany
    Posts
    325
    Hello,

    you posted a fragment shader (which looks good), but how does your vertex shader look like? What mesh do you want to render? Can you post a screenshot?

  3. #3
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by menzel View Post
    Hello,

    you posted a fragment shader (which looks good), but how does your vertex shader look like? What mesh do you want to render? Can you post a screenshot?
    here is my vertex shader
    Code :
    "#version 430 core                                                              \n"
    	        "layout (location = 0) in vec4 vertices;\n"
    	        "out vec2 texcoord;\n"
    	        "                                                                               \n"
     
    	        "void main(void)                                                                \n"
     
    	        "{                                                                              \n"
     
    	        "                                                                               \n"
     
    	        "texcoord = vertices.xy;\n"
    	        "    gl_Position = vertices;                                       \n"
    	        "}                                                                              \n"

    and im trying to map it to a square by drawing two triangles
    Code :
    GLfloat vertices[] = {
    		-.5, .5, 0, 1,
    		-.5, -.5, 0, 1,
    		.5, -.5, 0, 1,
    		-.5, .5, 0, 1,
    		.5, -.5, 0, 1,
    		.5, .5, 0, 1
    	};
    Click image for larger version. 

Name:	Screenshot.jpg 
Views:	64 
Size:	6.7 KB 
ID:	1148
    this photo actually shows the images in two seperate corners. i can get the image to go to the bottom left corner by:

    texture(tex1, gl_FragCoord.xy / textureSize(tex1, 0))

  4. #4
    Member Regular Contributor
    Join Date
    Jan 2012
    Location
    Germany
    Posts
    325
    Your texture coordinates go from -0.5 to 0.5, they should go from 0-1.

  5. #5
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    70
    i still dont get it. I changed my vertex data to
    Code :
    GLfloat vertices[] = {
     
    		0, 0, 0, 1,
    		0, .25, 0, 1,
    		.25, .25, 0, 1,
    		.25, 0, 0, 1
    	};
    and drew a square triangle fan, and it still puts the texture in the top right corner.
    does anyone know a good tutorial online for textures using 4.30 compatible code?

  6. #6
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    70
    also even just the source code for mapping a texture to a specific point on the screen would be very helpful

  7. #7
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    70
    i just figured it out thanks much for the advice about the texture coordinates menzel.

Posting Permissions

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