Converting to OpenGL 4

Started by
4 comments, last by CommanderLake 7 years, 1 month ago

I use an OpenTK GLcontrol as a faster alternative to GDI+ for displaying images and I would like to convert the old deprecated code to OpenGL 4 but I'm having trouble adapting code samples in guides I have found to my needs so would anyone kindly show me how to convert this:


GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, rez.Width, rez.Height, PixelFormat.Bgr, PixelType.UnsignedByte, currentframe);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.Enable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, tex);
GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(0.0f, 1.0f);
GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(1.0f, 1.0f);
GL.TexCoord2(0.0f, 0.0f);
GL.Vertex2(-1.0f, 1.0f);
GL.End();
OGLControl.SwapBuffers();
Advertisement

Virtually every single line of code you have is deprecated ("old") OpenGL, and unfortunately you're in a position where your question is effectively a case of "can somebody show me how to write a whole new renderer", because you'll need to rewrite all of it.

The matrix stack will need to be replaced with a matrix library.

You'll need to write shader loading and management code.

You'll need to write vertex buffer creation and management code.

The best way to move forward from code like this is one step at a time; attempting to replace all of it with modern OpenGL will just be confusing, error-prone, and you'll likely not learn much.

So I suggest that instead of looking for OpenGL 4 tutorials you instead pick one part of the code, replace it with what the equivalent could be, leaving the rest as-is, and learn from that before moving to the next.

You could replace the matrix stack with a matrix library, using glLoadMatrixf, and change nothing else.

You could add simple vertex and fragment shaders to do your drawing but change nothing else (because contrary to what you might read elsewhere, you absolutely can use vertex and fragment shaders with glBegin/glEnd code).

You could add a vertex buffer and glDrawArrays to replace your glBegin/glEnd code; once again, you don't need to implement both shaders and vertex buffers, each is capable of working without the other.

That would get you a GL 2.0-ish baseline that nonetheless uses modern rendering features, and that you can then more easily lift up to full GL4.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Thanks for the advice, Is there by any chance a more direct way of drawing pixel data without scaling etc. with modern OpenGL?

There is sadly not but that is what makes "modern" graphics so flexible. You need to manage anything by your own from vertex management to the render/processing pipeline.

I did need a small class for debug code output in the past but even that needed a min of a vertex buffer to tell the graphics where are the pixel areas I want to write to, a texture holding thatever has to be shown (text in my case) and at least to shaders (vertex and fragment) to bring whatever up to the screen.

Matrix calculations are now no longer made by GL since you need to roll your own or use a library as mentioned because you need to transform every pixel by the shader for getting expected results. Passing an identity matrix does not do what you might expect here so calculating ortho or projection view is needed. OpenTK has this capability already so dont worry about that, just calc a matrix and load it into your shader.

Anything between begin and end needs to be put into a VBO/VAO once so making a general setup here saves you a lot of render time.

It might look like a mountain of work but it isnt and it is worth it

If you only want to get a big 2d pixel array to the screen your current code is just fine from a performance standpoint.

With OpenGL or D3D11 you can't access texture data directly and always neet to use a api copy command to get data into a texture before you can then get that texture onto the screen.

Only D3D12 and Vulkan give you the abbility to draw directly from client memory to the screen.

If you do blitting, like put you scene together by drawing many square images (sprites). Then it may be already worth to consider a more modern approche.

Edit: I hate forums that fuck up formating if your don't use javascript. -_-

I'd love to be able to use Vulcan but from what I've read its difficult even for an experienced graphics programmer to use and I can barely get my head around my bit of GL.

This topic is closed to new replies.

Advertisement