PDA

View Full Version : How to map texture to primitive?



sandbucket
09-08-2013, 02:45 PM
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.

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.

menzel
09-09-2013, 01:15 AM
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?

sandbucket
09-09-2013, 07:10 AM
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

"#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

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

menzel
09-09-2013, 07:40 AM
Your texture coordinates go from -0.5 to 0.5, they should go from 0-1.

sandbucket
09-09-2013, 08:25 AM
i still dont get it. I changed my vertex data to

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?

sandbucket
09-09-2013, 08:58 AM
also even just the source code for mapping a texture to a specific point on the screen would be very helpful

sandbucket
09-09-2013, 09:46 AM
i just figured it out thanks much for the advice about the texture coordinates menzel.