Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Rendering to a texture..

  1. #1

    Rendering to a texture..

    Hi all,

    Im having problems rendering to a texture, the way that i understand it works is by:
    1) creating a blank texture (initialization)
    2) render the scene, and then copy it to the blank texture
    3) render the scene

    Create:
    [pascal]
    SetLength(Indexs, 1);

    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, @Indexs[0]);
    glBindTexture(GL_TEXTURE_2D, Indexs[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, Width, Height, 0);
    [/pascal]

    Draw scene to texture:
    [pascal]
    If Textures[Texture] = Nil Then Exit;

    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

    glViewport(0, 0, Textures[Texture].Width, Textures[Texture].Height);

    glMatrixMode(GL_TEXTURE);
    glLoadIdentity;
    glMatrixMode(GL_MODELVIEW);

    FollowTarget(Pos, Heading, Distance);

    glBindTexture(GL_TEXTURE_2D, Indexs[0]);
    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, Width, Height);

    glViewPort(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [/pascal]

    I have bound got something wrong, all i get is a blank white texture
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  2. #2

    Rendering to a texture..

    You need to initialize the texture with glteximage2d(optionally with data set to nil) before you can call glcopyteximage2d.

    Looking at your other code everything else seems to look fine
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  3. #3

    Rendering to a texture..

    Quote Originally Posted by JSoftware
    You need to initialize the texture with glteximage2d(optionally with data set to nil) before you can call glcopyteximage2d.
    He bypases that with glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, Width, Height, 0); when creating.
    White is what you usually get when texture state is invalid, you sure you're creating it before using? What if you replaced glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, Width, Height); with glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, Width, Height, 0);

  4. #4

    Rendering to a texture..

    Render to framebuffer textures are VERY buggy as I found out...

    You also have to fill in the mipmap states (Even if you're not using mipmaps its usually a good idea to fully create the texture with mipmaps anyway)

    Also the glTexImage2d() with the data argument set to nil can cause crashes on some systems, so I found out. So this is the code I used in my project:



    [pascal]
    // Note that FTarget is just set to GL_TEXTURE_2D.
    // In the original code, it was also used with Cubemap textures too.

    // Create a blank texture image first. It's handle is stored in FTexObject

    glBindTexture(FTarget, FTexObject);

    { Create mipmap images too. This is to keep the texture from being
    inconsistent at startup. Use GL_SGIS_generate_mipmaps to actually fill in
    the mipmaps. }
    i := 0;
    mw := w;
    mh := h;
    done := FALSE;

    try
    Addtolog('Uploading mipmaps');
    GetMem(Data, w*h*3);
    ZeroMemory(Data, w*h*3);

    while not done do
    begin
    if (mw = 1) and (mh = 1) then done := TRUE;
    glTexImage2d(FTarget, i, 3, mw, mh, 0, GL_RGB, GL_UNSIGNED_BYTE, Data);
    INC(i);
    if mw > 1 then mw := mw shr 1;
    if mh > 1 then mh := mh shr 1;
    end;
    glFinish;
    finally
    FreeMem(Data, w*h*3);
    Data := nil;
    end;
    [/pascal]

    It's not pretty and you could probably get away with setting it to nil and not worrying about the glFinish command either, but this seems to work best for all the pc's its run on.
    My site: DelphiTuts.com (coming soon)...

    Download Font Studio 4.21 here.

  5. #5

    Rendering to a texture..

    use FBO (with latest video drivers)
    There are only 10 types of people in this world; those who understand binary and those who don't.

  6. #6

    Rendering to a texture..

    This is one area where DirectX is truly better than OpenGL, D3D had proper render to texture for ages and FBOs only work on GeForce4 Ti and up hardware.

  7. #7

    Rendering to a texture..

    ... and radeon 9500 and up.

    Now the problem with your code is that you don't initialize the code correctly. You can't specify an internalformat with glcopyteximage2d. You need glteximage2d.

    There's no idea in building mipmaps as he doesn't use any mipmap filtering. besides he can't mipmap textures which a copied from screen unless he has fbo's (glgeneratemipmapext)
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  8. #8

    Rendering to a texture..

    Quote Originally Posted by JSoftware
    Now the problem with your code is that you don't initialize the code correctly. You can't specify an internalformat with glcopyteximage2d. You need glteximage2d.
    Why not? works for me

  9. #9

    Rendering to a texture..

    Dammit paulius.. You got me there ops:

    I don't know why that works. My long time experiences tell me that people are always told to initialize their textures using glteximage2d.

    Back on topic(now that i know that the code actually works ):
    Have you checked that the texture id you are using is the same that you initialized. Have you checked viewport size? Have you checked to see what it renders when you copy to texture? have you textures disabled or set to something else before rendering?
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  10. #10

    Rendering to a texture..

    The way it all works in my game is:
    1) the textures class is created and then i create the blank texture (i have changed the creation class to:
    [pascal]
    SetLength(Indexs, 1);

    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, @Indexs[0]);
    glBindTexture(GL_TEXTURE_2D, Indexs[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    // (target: GLenum; level, internalformat: GLint; width, height: GLsizei; border: GLint; format, atype: GLenum; const pixels: Pointer);

    Done := False;
    i := 0;
    Try
    GetMem(Data, Width*Height*3);
    ZeroMemory(Data, Width*Height*3);
    While Not Done Do
    Begin
    If (Width = 1) And (Height = 1) Then Done := True;
    glTexImage2d(GL_TEXTURE_2D, i, 3, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, Data);
    Inc(i);
    If Width > 1 Then Width := Width Shr 1;
    If Height > 1 Then Height := Height Shr 1;
    End;
    glFinish;
    Finally
    FreeMem(Data, Width*Height*3);
    End;
    [pascal]
    I still seem to be getting the white texture..

    2) during the rendering process, i call the procedure to update the texture
    3) render the scene

    * Im not sure if the texture id is set, i will check that now
    * The viewport size doesnt seem to change, im constantly checking it during the rendering process to position elements.
    * It supposed to render from the original camera, so im pretty sure that works
    * I call glEnable(GL_Texture_2D) everytime i call the texture.bind procedure
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •