You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SDL2双纹理视频分屏渲染问题:如何仅渲染纹理半区?

Fixing SDL Partial Texture Rendering for Split Video Streams

Hey there, let's get your split video stream rendering working! The core issue with your current code is that you're not specifying a source rectangle to crop the texture—you're either rendering the entire texture (using NULL for the source rect) or only adjusting where it draws to the window (the destination rect). Here's how to fix it:

Key Concept

To render only a portion of your texture, you need to define two rectangles for each video section:

  • Source Rect: Which part of the texture you want to extract (e.g., left half or right half of the video buffer)
  • Destination Rect: Where that extracted section should draw in your window

Step-by-Step Solution

  1. Capture your texture's actual dimensions
    First, confirm the width and height of your texture (this should match your video stream's frame size). Let's assume your texture is textureWidth pixels wide and textureHeight pixels tall, and your window is windowWidth x windowHeight.

  2. Define source rectangles for left/right halves

    • For the left half of the video:
      SDL_Rect leftSrcRect = {0, 0, textureWidth / 2, textureHeight};
      
    • For the right half of the video:
      SDL_Rect rightSrcRect = {textureWidth / 2, 0, textureWidth / 2, textureHeight};
      
  3. Define destination rectangles for window placement

    • Draw left half to the left side of the window:
      SDL_Rect leftDestRect = {0, 0, windowWidth / 2, windowHeight};
      
    • Draw right half to the right side of the window:
      SDL_Rect rightDestRect = {windowWidth / 2, 0, windowWidth / 2, windowHeight};
      
  4. Update and render the texture
    Replace your existing render code with this (keep your texture update logic intact):

    // Update texture with your video buffer data
    SDL_UpdateTexture(texture, NULL, buffer_start, fmt.fmt.pix.width * 2);
    
    SDL_RenderClear(renderer);
    
    // Render left half of the texture to left window area
    SDL_RenderCopy(renderer, texture, &leftSrcRect, &leftDestRect);
    // Render right half of the texture to right window area
    SDL_RenderCopy(renderer, texture, &rightSrcRect, &rightDestRect);
    
    SDL_RenderPresent(renderer);
    

Additional Checks

  • Double-check that textureWidth matches the actual width from fmt.fmt.pix.width. Your pitch calculation (fmt.fmt.pix.width * 2) looks right for 2-byte per pixel formats like RGB565—just confirm it matches your buffer's row size.
  • If your texture width is odd, adjust the source rect widths (e.g., (textureWidth + 1)/2 for the left half) to avoid missing a pixel.
  • Ensure your window size is set correctly so the destination rects fit without overlapping or gaps.

内容的提问来源于stack exchange,提问作者Carlos Pinto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:03:32