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

  • SEARCH
  • Home
  • 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 6381857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:26:54+00:00 2026-05-25T02:26:54+00:00

I need to initialize an OpenGL 2D texture with zeroes. Why? I render to

  • 0

I need to initialize an OpenGL 2D texture with zeroes.

Why? I render to this texture with OpenCL. Roughly, rendering stages are:

  1. Create RGBA texture, initialize with zeros (so it contains transparent black pixels)
  2. OpenCL: compute color of pixels where some objects are visible and write them to this texture
  3. OpenGL: add environment mapping to the transparent pixels and display final image

I therefore do not need to pass any actual texture data to glTexImage2d. But according to the spec, when I pass data=NULL, the texture memory is allocated but not initialized.

Allocating a client-side memory filled with zeroes and passing to glTexImage2D is not a very good option although only one I can think of.

EDIT: I actually want to init only alpha component (RGB will get overriden anyway), but I doubt this make the situation any easier.

  • 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-25T02:26:55+00:00Added an answer on May 25, 2026 at 2:26 am

    There are several ways to do this:

    1: Given a width and a height for a texture:

    std::vector<GLubyte> emptyData(width * height * 4, 0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, &emptyData[0]);
    

    For maximum efficiency, you should leave some time between when you call this and when OpenCL starts its work.

    2: Attach it to an FBO and use glClearBuffer.

    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
    glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0); //Only need to do this once.
    glDrawBuffer(GL_COLOR_ATTACHMENT0); //Only need to do this once.
    GLuint clearColor[4] = {0, 0, 0, 0};
    glClearBufferuiv(GL_COLOR, 0, clearColor);
    

    This seems like it should be faster, since there is no CPU-GPU DMA going on. But the binding and unbinding of FBOs may introduce inefficiencies. Then again, it may not. Profile it to make sure.

    Note that this only works for image formats that can be used as render targets. So not compressed textures.

    3: Given sufficient drivers, you can use the new ARB_clear_texture extension:

    glClearTexImage(texture, 0, GL_BGRA, GL_UNSIGNED_BYTE, &clearColor);
    

    Note that this only works for image formats whose data can be provided by standard pixel transfers. So not compressed formats.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To initialize an int array with all zeros, do I need to use: int
I have a class: class One def initialize; end end I need to create
I need to create an enumeration that I will need to initialize from a
I need to initialize few objects, it can take a while so I want
How do I go about if I need to initialize an object's base with
I'm writing a web application and need to initialize some parameters that I'm pulling
I need a 'good' way to initialize the pseudo-random number generator in C++. I've
Do I need to initialize each level of a multi-level list in R? l=list()
Working with several DBs in parallel and need to initialize some records with hashed
I have WCF service that is hosted in IIS . I need to initialize

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.