Skip to content

Instantly share code, notes, and snippets.

@benkant
Created April 25, 2024 02:28
Show Gist options
  • Save benkant/f0a026ccb48ed8e3ac04381cdd2b3e74 to your computer and use it in GitHub Desktop.
Save benkant/f0a026ccb48ed8e3ac04381cdd2b3e74 to your computer and use it in GitHub Desktop.
textcoords

You can conditionally set the texcoord data in the C++ code based on whether you're rendering the view or output. You can create two std::array<glm::vec2, 4> - one for the view coordinates and one for the output coordinates.

Here's how you could structure your code to use different texcoord for view and output, while keeping uvpos = texcoord in your vertex shader:

std::array<glm::vec2, 4> texcoord_view = {
    { {0.0f, 1.0f}, {1.0f, 1.0f}, {1.0f, 0.0f}, {0.0f, 0.0f} }};

std::array<glm::vec2, 4> texcoord_output = {
    { {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}} };

Then, before calling your draw function, you would bind the appropriate texcoord data depending on whether you're rendering the view or output:

if(rendering_view) {
    glBindBuffer(GL_ARRAY_BUFFER, VBO_texcoord);
    glBufferData(GL_ARRAY_BUFFER, texcoord_view.size() * sizeof(glm::vec2), texcoord_view.data(), GL_STATIC_DRAW);
}
else if(rendering_output) {
    glBindBuffer(GL_ARRAY_BUFFER, VBO_texcoord);
    glBufferData(GL_ARRAY_BUFFER, texcoord_output.size() * sizeof(glm::vec2), texcoord_output.data(), GL_STATIC_DRAW);
}

In your vertex shader, you only need uvpos = texcoord.

Replace rendering_view and rendering_output with the correct variables or conditions in your code that determine whether you're rendering the view or the output. The idea is to bind the correct texcoord data before you call the draw function. This way, the shader will automatically use the correct texture coordinates without needing to change the shader code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment