问题描述
我一直在学习SDL 2D编程一段时间,现在我想使用SDL和OpenGL组合创建一个程序。我这样设置:
I have been learning about SDL 2D programming for a while and now I wanted to create a program using SDL and OpenGL combined. I set it up like this:
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("SDL and OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
程序现在只是一个黑色窗口,使用OpenGl显示一条白线。下面是渲染的代码:
The program is for now just a black window with a white line displayed using OpenGl. Here is the code for the rendering:
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2d(1, 0);
glVertex2d(-1, 0);
glEnd();
SDL_GL_SwapWindow(window);
事实是,我想使用纯SDL和SDL_Renderer对象来渲染纹理,我以前没有OpenGL。我试过了,但它没有工作。甚至可以做到这一点,如何?我做的是创建一个SDL_Renderer,然后绘制OpenGL的东西做这个:
So the thing is, I would like to render textures additionally using pure SDL and a SDL_Renderer object, as I did before without OpenGL. I tried that out but it didn't work. Is it even possible to do that and how? What I did is creating a SDL_Renderer and then after drawing OpenGL stuff doing this:
SDL_Rect fillRect;
fillRect.w = 50;
fillRect.h = 50;
fillRect.x = 0;
fillRect.y = 0;
SDL_SetRenderDrawColor(renderer, 100, 200, 100, 0);
SDL_RenderFillRect(renderer, &fillRect);
SDL_RenderPresent(renderer);
但这不起作用。矩形没有显示,虽然有些毫秒,它看起来有点。我觉得OpenGL渲染是覆盖它。
But this does not work. The rectangle is not shown, although for some milliseconds it appears slightly. I feel like the OpenGL rendering is overwriting it.
推荐答案
SDL使用OpenGL(或在某些情况下Direct3D),OpenGL有内部状态影响程序中的GL调用。 SDL的当前版本没有清楚地指示它对于任何呼叫改变或取决于哪些状态,并且它不包括重置到已知状态的功能。这意味着你不应该将SDL_Renderer与OpenGL混用。
SDL uses OpenGL (or in some cases Direct3D) and OpenGL has internal state which affects the GL calls in your program. The current version of SDL does not clearly indicate which states it changes or depends upon for any call and it does not include functions to reset to a known state. That means you should not mix SDL_Renderer with OpenGL yet.
然而,如果你真的想要这个功能,你可以尝试SDL_gpu(注意:我是作者)。它支持与OpenGL调用和自定义着色器混合。您需要指定所需的OpenGL API版本(例如,使用GPU_InitRenderer(GPU_RENDERER_OPENGL_1,...)),并重置SDL_gpu使用每个帧(GPU_ResetRendererState())的GL状态。查看SDL_gpu源代码库中的3d演示更多信息。
However, if you really want this functionality now, you can try SDL_gpu (note: I'm the author). It does support mixing with OpenGL calls and custom shaders. You would want to specify which version of the OpenGL API you need (e.g. with GPU_InitRenderer(GPU_RENDERER_OPENGL_1, ...)) and reset the GL state that SDL_gpu uses each frame (GPU_ResetRendererState()). Check out the 3d demo in the SDL_gpu source repository for more.
这篇关于使用SDL_Renderer绘制2D东西,使用SDL_GLContext绘制OpenGL东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!