Results 1 to 4 of 4

Thread: How to set the texture coordinates?

  1. #1
    Senior Member Regular Contributor
    Join Date
    Aug 2010
    Posts
    114

    How to set the texture coordinates?

    Hi,

    Let's say I have a texture with dimension 512x512.

    If I do not set the coordinates of the texture, I just define the color of each pixels and output to a window with size 512x512. Will the color I set before displayed in order?

    Eg: I use 8-bit RGBA format. Max value =255. I set some 1st row to white and 2nd row to black and so on.

    So, I should get this:

    1st row 255 255 ...........................255 (512x4)
    2nd row 0 0 ........................... 0 (512x4)
    .
    .
    .
    512 row 255 255 ........................... 255 (512x4)

    This is what I expect. But somehow, the values I get are rubbish. The values seems like to mix up. I dun even know here is the problem. My understanding correct or not from this example?

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674

    Re: How to set the texture coordinates?

    If I do not set the coordinates of the texture, I just define the color of each pixels and output to a window with size 512x512. Will the color I set before displayed in order?
    What you said here does not make sense.

    A texture is nothing more than a fancy array. You cannot draw an array; you can only draw the elements in an array. To get an element from an array, you must index that array. For textures, the index is called a "texture coordinate".

    If you're using shaders, then you will need to provide a texture coordinate to the texture function. Whether this coordinate is interpolated from a vertex shader output, or generated on the fly in the fragment shader is irrelevant. What matters is that you need a texture coordinate.

    If you're using the fixed-function pipeline, you will need to provide texture coordinates as vertex attributes.

    Also, how do you "define the color of each pixels"? The color that is written to the framebuffer is what you output from the fragment shader (blending and other post-shader operations notwithstanding).

  3. #3
    Senior Member Regular Contributor
    Join Date
    Aug 2010
    Posts
    114

    Re: How to set the texture coordinates?

    Quote Originally Posted by Alfonse Reinheart
    If I do not set the coordinates of the texture, I just define the color of each pixels and output to a window with size 512x512. Will the color I set before displayed in order?
    What you said here does not make sense.

    A texture is nothing more than a fancy array. You cannot draw an array; you can only draw the elements in an array. To get an element from an array, you must index that array. For textures, the index is called a "texture coordinate".
    What I means here is how set the texture coordinates exactly the same on screen. If my texture is 512x512, I want to draw the elements inside the texture to a screen with 512x512 also. And, I want the elements in the texture to be exactly the same on the screen without distortion.

    May be I should explain in this way:

    coord(x,y) is the coordinates on screen. TexCoord(x,y) is the coordinates of the texture.

    I want to draw the elements of TexCoord(0,0) to coord(0,0), TexCoord(1,1) to coord(1,1) and so on.

    Quote Originally Posted by Alfonse Reinheart
    If you're using shaders, then you will need to provide a texture coordinate to the texture function. Whether this coordinate is interpolated from a vertex shader output, or generated on the fly in the fragment shader is irrelevant. What matters is that you need a texture coordinate.

    If you're using the fixed-function pipeline, you will need to provide texture coordinates as vertex attributes.

    Also, how do you "define the color of each pixels"? The color that is written to the framebuffer is what you output from the fragment shader (blending and other post-shader operations notwithstanding).
    Ya. I use fragment to output the color to frame buffer using gl_FragColor. I will get the elements in TexCoord(0,0) and output the color to coord(0,0). But I am not sure how to set the coordinates on the screen. My values all screw up. In my program, it seems like to take element in TexCoord(0,0) and output to coord(1,1) instead of coord(0,0) as I want for example.

  4. #4
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674

    Re: How to set the texture coordinates?

    I use fragment to output the color to frame buffer using gl_FragColor. I will get the elements in TexCoord(0,0) and output the color to coord(0,0). But I am not sure how to set the coordinates on the screen. My values all screw up. In my program, it seems like to take element in TexCoord(0,0) and output to coord(1,1) instead of coord(0,0) as I want for example.
    Fragment shaders can not dictate which pixel/sample location that they write to. The built-in variable gl_FragCoord has the window-space coordinates of the current fragment. However, as a shader input, it cannot be changed. And even if you could, changing it wouldn't matter.

    You can use gl_FragCoord to generate your texture coordinates if you want.

Similar Threads

  1. Texture Coordinates and Depth Buffer to obtain world coordinates
    By snkhan42 in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 04-27-2018, 11:52 AM
  2. Texture coordinates to screen pixel coordinates
    By lucafr in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 04-13-2011, 04:19 AM
  3. Texture Mapping & Texture Coordinates
    By senem in forum OpenGL: Basic Coding
    Replies: 20
    Last Post: 10-03-2007, 08:50 AM
  4. How to get texture coordinates from screen coordinates
    By geckosenator in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 01-28-2006, 07:23 AM
  5. 3D Models with Texture Coordinates and Texture images
    By dimensionX in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 04-15-2004, 11:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean