OpenGL in my HWND - opengl

Right now, I'm trying to port a Direct3D renderer from my engine. I'm and OpenGL begginer so i dont have much knowlegde about OpenGL, as now i can create windows and do my render via glut, but i can't use glut for my project, because the HWND is created in my code and then sent to the renderer DLL
// Where pWindow is already a valid HWND target of the renderer
//(Currently Direct3D9 and Direct3D10
pRenderer = pCreateGraphics(800, 600, false, pWindow);
My question is: Is there library similar to GLUT that has a similar behaviour as GLUT, but allowing my to use my own window handle?
Note: I prefer using a lib instead of reinventing the wheel, but i will do if there is not a library that can help me

Well you can have a look at GLFW's source code or NeHe tutorials and grab the OpenGL init code.

SDL lets you get the window handle.
I don't know if the used pixel format can be assumed to be DirectX compatible though.

Related

OpenVR. Rendering in OpenGL without SDL

Is this possible in nature? What role of SDL library in OpenVR API? Does it needed for OpenGL context or only for mirroring the stereo image to SDL window?
It is possible. If you don't use SDL, you'll have to create your rendering context and window by yourself. The whole code would be too long for this answer, but on Windows you could use functions like CreateWindowEx and wglCreateContext. OpenVR doesn't require anything different from a normal context setup, but you need to use a somewhat modern version of OpenGL (4.1 at least works for me).

Using a SDL window with OpenGL the other way around

I just recently changed all rendering in my application/game from SDL to OpenGL. I still use SDL for keyboard input, loading images and creating the window.
But since i only render in OpenGL do you think that i should change the window to a OpenGL initialized window instead of a SDL window. The header i use for OpenGL functions at the moment is "SDL_opengl.h". Does that affect things?
What are the advantages and disadvantages if i do this? Right now it feels like it's the logical "next step" since i got rid of all other SDL rendering code.
Just keep on using SDL for input and window management.
There's no provision for either in the OpenGL spec so there's really no such thing as an "OpenGL initialized window".
As genpfault said, you should keep using SDL for your window initialization. It's more clean and portable than OS-specific methods of initializing a window for OpenGL rendering.
I would also recommend switching from SDL_opengl.h to SDL.h and gl.h; the definitions in SDL_opengl.h aren't necessary for basic window management, and they conflict with the definitions in other OpenGL libraries like GLEW (which you may want to use later for pixel shaders and framebuffers).

Creating cross platform OpenGL off-screen context

I have a task to implement offscreen OpenGL renderer both for Window and Linux in C++.I have such a version already written in Java using LWJGL lib.There I used PBuffer object ,which under hood creates Pbuffers based on the used OS.First I thought to re-implement the full PBuffer creation logic just as it i done in native source of LWJGL.Then I read this post on StackOverflow.com where it is suggested using the standard context creation ,let's say using GLFW (which is cross platform) but not to create the actual window.Is it the right way to go? What are pros and cons vs using Pbuffer in such a case?
Update:
I just want to emphasize that I use FBOs to render the frames so my problem here is not how to render in offscreen mode but how to create a context without window both in Windows and Linux OSs.
I'd highly recommend not to use PBuffers anymore but to use Frame Buffer Objects (FBOs) instead. FBOs offer much better performance as using them does not require a context switch and they have several other advantages.
LWJGL supports FBOs, but GLFW is "just" for cross-platform setup of OpenGL and not for rendering. For convenient cross-platform FBO usage I'd recommend to use a library like OGLplus on top of GLFW. See here for a render-to-texture example.
The Simple DirectMedia Layer (SDL) library is worth a try. It simplifies cross-platform OpenGL context creation, with the ability to use memory surfaces for off-screen rendering.
The only thing you would have to do extra, is to include your OpenGL and SDL headers from different locations, depending on your platform. This can be done with simple pre-processor directives.
As far as I know there is no cross-platform way to create contexts, you will have to create your own abstraction and then implement it for each platform.
On windows I have used the following code to create a second context to do loading of content in a background thread (this program used GLFW but that should not matter):
void Program::someFunction()
{
HDC hdc = wglGetCurrentDC();
HGLRC hglrc = wglGetCurrentContext();
HGLRC hglrc_new = wglCreateContext(hdc);
wglShareLists(hglrc, hglrc_new);
loadThread = boost::thread(&Program::loadFunc, this, hdc, hglrc_new);
}
/**
* Imports all our assets. Supposed to run in its own thread with its own OpenGL context.
* #param hdc The current device context.
* #param hglrc A OpenGL context thats shares its display list with the main rendering context.
*/
void Program::loadFunc(HDC hdc, HGLRC hglrc)
{
wglMakeCurrent(hdc, hglrc);
//Do stuff...
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hglrc);
}

Use SDL inside Irrlicht

I know you can do the same in lrrlicht, but I want to use SDL code/ functions to draw text, images inside Irrlicht (to handle 2d) and use Irrlicht to do the hardcore 3D thing, how can you apply text or images from sdl to this Irrlicht Engine, can you show me simple code, so that I can understand?
In the SDL you can do such:
// I start by declare the SDL video Name
SDL_Surface *screen;
// set the video mode:
screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN); if (screen == NULL) { printf("Unable to set video mode: %s\n", SDL_GetError()); return 1; }
// I can now display data, image, text on the screen by using the declared SDL video Name " screen "
SDL_BlitSurface(my_image, &src, screen, &dest);
If you are using / targeting Windows , and are a little familiar with the WinApi, you may be able to use SDL with Irrlicht by running both inside a Win32 Window, using a combination of the SDL Window ID Hack (also see this thread) and running Irrlicht in a Win32 Window. The only problems you may face is that running SDL in a Win32 Window is extremely difficult and unpredictable.
You may be also be able to achieve a similar result using GTK+ (for cross-platform purposes), but I have personally never succeeded in running SDL or Irrlicht in a GTK+ Window.
Also, if you want a light graphics and media library like SDL , may I suggest SFML. It can run in Win32 and X/11 windows without any problems and may easily work with Irrlicht.

2D graphic library for windows form application

I need a 2d graphic library for windows form application(Visual studio 2010).I used to work with SDL in console applications.It was really great:simple and powerful.But a friend of mine told me that it won't work in windows form application.Can you please suggest me a good library for 2d drawing in form applications written in C++?Or is there a way to use SDL in windows form applications?
*it would be great if it has these features:
load .bmp and .gif and popular image formats.
Simple to learn.
Draw text and simple 2d shapes.
Thanks.
(Sorry for my bad english)
SDL supports Windows. You should be able to use SDL by just using a Window's Handle from a control in Windows Forms. For example, if you initialize SDL to use the a "panel" by passing the panel->Handle property as the HWND, it should work fine.
Alternatively, you can use Windows Form's built-in drawing in the System.Drawing namespace.