Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6048213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:26:17+00:00 2026-05-23T07:26:17+00:00

I have a vertex/fragment shader that draws a rectangle performing many calculations based on

  • 0

I have a vertex/fragment shader that draws a rectangle performing many calculations based on a texture. (It blends pixels, modifies them, etc). The thing is that each rectangle and the pixels it contains will not change. I only move the entire rectangle(s) around and zoom them.

Is there any way to optimize the fragment shader since the rectangles do not really need to be recomputed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:26:17+00:00Added an answer on May 23, 2026 at 7:26 am

    So, if I understood you correctly you compute those rectangles once and then want to reuse them? This kind of task is solved by rendering to a texture and then use the generated textures further on.

    Render to texture is easiest done through Framebuffer Objects.

    EDIT: A simple example for using FBO to render to texture

    // test_fbo_teapot.cpp
    
    #include <GL/glew.h> // Uses GLEW for extension loading
    #include <GL/glut.h> // Uses GLUT as framework
                         // Check those are on your system for compilation
                         // and if not please install them.
    
    #include <cmath>
    #include <iostream>
    
    using namespace std;
    
    namespace render
    {
        int width, height;
        float aspect;
    
        void init();
        void reshape(int width, int height);
        void display();
    
        int const fbo_width = 512;
        int const fbo_height = 512;
    
        GLuint fb, color, depth;
    };
    
    void idle();
    
    int main(int argc, char *argv[])
    {
        glutInit(&argc, argv);
        glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
    
        glutCreateWindow("FBO test");
        glutDisplayFunc(render::display);
        glutReshapeFunc(render::reshape);
        glutIdleFunc(idle);
    
        glewInit();
    
        render::init();
        glutMainLoop();
    
        return 0;
    }
    
    void idle()
    {
        glutPostRedisplay();
    }
    
    void CHECK_FRAMEBUFFER_STATUS()
    {                                                         
        GLenum status;
        status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); 
        switch(status) {
        case GL_FRAMEBUFFER_COMPLETE:
            break;
    
        case GL_FRAMEBUFFER_UNSUPPORTED:
        /* choose different formats */
            break;
    
        default:
            /* programming error; will fail on all hardware */
            throw "Framebuffer Error";
        }
    }
    
    namespace render
    {
        float const light_dir[]={1,1,1,0};
        float const light_color[]={1,0.95,0.9,1};
    
        void init()
        {
            glGenFramebuffers(1, &fb);
            glGenTextures(1, &color);
            glGenRenderbuffers(1, &depth);
    
            glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb);
    
            glBindTexture(GL_TEXTURE_2D, color);
            glTexImage2D(   GL_TEXTURE_2D, 
                    0, 
                    GL_RGBA, 
                    fbo_width, fbo_height,
                    0, 
                    GL_RGBA, 
                    GL_UNSIGNED_BYTE, 
                    NULL);
    
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
    
            glBindRenderbuffer(GL_RENDERBUFFER, depth);
            glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, fbo_width, fbo_height);
            glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
    
            CHECK_FRAMEBUFFER_STATUS();
        }
    
        void reshape(int width, int height)
        {
            render::width=width;
            render::height=height;
            aspect=float(width)/float(height);
            glutPostRedisplay();
        }
    
        void prepare()
        {
            static float a=0, b=0, c=0;
    
            glBindTexture(GL_TEXTURE_2D, 0);
            glEnable(GL_TEXTURE_2D);
            glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb);
    
            glViewport(0,0,fbo_width, fbo_height);
    
            glClearColor(1,1,1,0);
            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            gluPerspective(45, 1, 1, 10);
    
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
    
            glEnable(GL_LIGHT0);
            glEnable(GL_LIGHTING);
    
            glEnable(GL_DEPTH_TEST);
            glDisable(GL_CULL_FACE);
    
            glLightfv(GL_LIGHT0, GL_POSITION, light_dir);
            glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color);
    
            glTranslatef(0,0,-5);
    
            glRotatef(a, 1, 0, 0);
            glRotatef(b, 0, 1, 0);
            glRotatef(c, 0, 0, 1);
    
            glutSolidTeapot(0.75);
    
            a=fmod(a+0.1, 360.);
            b=fmod(b+0.5, 360.);
            c=fmod(c+0.25, 360.);
        }
    
        void intermediary()
        {
        }
    
        void final()
        {
            static float a=0, b=0, c=0;
    
            glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    
            glViewport(0,0, width, height);
    
            glClearColor(1,1,1,1);
            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            gluPerspective(45, aspect, 1, 10);
    
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glTranslatef(0,0,-5);
    
            glRotatef(b, 0, 1, 0);
    
            b=fmod(b+0.5, 360.);
    
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, color);
    
            glEnable(GL_DEPTH_TEST);
            glEnable(GL_CULL_FACE);
    
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
            glDisable(GL_LIGHTING);
    
            float cube[][5]=
            {
                {-1, -1, -1,  0,  0},
                { 1, -1, -1,  1,  0},
                { 1,  1, -1,  1,  1},
                {-1,  1, -1,  0,  1},
    
                {-1, -1,  1, -1,  0},
                { 1, -1,  1,  0,  0},
                { 1,  1,  1,  0,  1},
                {-1,  1,  1, -1,  1},
            };
            unsigned int faces[]=
            {
                0, 1, 2, 3,
                1, 5, 6, 2,
                5, 4, 7, 6,
                4, 0, 3, 7,
                3, 2, 6, 7,
                4, 5, 1, 0
            };
    
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    
            glVertexPointer(3, GL_FLOAT, 5*sizeof(float), &cube[0][0]);
            glTexCoordPointer(2, GL_FLOAT, 5*sizeof(float), &cube[0][3]);
    
            glCullFace(GL_BACK);
            glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, faces);
    
            glCullFace(GL_FRONT);
            glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, faces);
    
            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    
        }
    
        void display()
        {
            prepare();
            intermediary();
            final();
    
            glutSwapBuffers();
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have pass-through vertex and fragment shaders. vertex shader void main(void) { gl_TexCoord[0] =
I have a vertex shader that transforms vertices to create a fisheye affect. Is
I have a vertex shader in which I do a texture lookup to determine
I'm making a simple WebGL demo. I have a simple vertex shader that takes
Let's say I have a structure named vertex with a method that adds two
I have a vertex shader (2.0) doing some instancing - each vertex specifies an
I have a graph with an s and t vertex that I need to
This question is best described in code. I have a class called Vertex that
I have created a shader that works perfectly in Firefox, but in Chrome the
I have a radial blur shader in GLSL, which takes a texture, applies a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.