Adding support for retina display in OpenGL ES, which does not draw anything

I have an application that draws a scene in OpenGL ES 2.0. Everything is correctly displayed on the iPhone and iPad simulators, as well as on real devices, until I try to use the retina screen. Retina devices are still drawing, but with the same resolution as older devices.

On new devices, I get only a black screen. On the retina simulators, I get a pink screen. The program will not even use the value that I set with glClearColor ();

I can correctly set the scale factor for the device using the following code:

_eaglLayer = (CAEAGLLayer *)self.layer; _eaglLayer.opaque = YES; self.contentScaleFactor = [[UIScreen mainScreen] scale]; _eaglLayer.contentsScale = [[UIScreen mainScreen] scale]; 

And I can correctly create the render buffer here:

 glGenRenderbuffers(1, &_depthRenderBuffer); glBindRenderbuffer(GL_RENDERBUFFER, _depthRenderBuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, self.frame.size.width, self.frame.size.height); //Context is an instance of EAGLContext [_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer]; 

I set the viewport size as follows:

 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_backingWidth); glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_backingHeight); ... glViewport(0, 0, _backingWidth, _backingHeight); 

All this code works on older devices, and it works flawlessly if I comment out the lines:

 self.contentScaleFactor = [[UIScreen mainScreen] scale]; _eaglLayer.contentsScale = [[UIScreen mainScreen] scale]; 

I registered the scale when loading the view, and it correctly sets the value to 2.0 on retina devices or 1.0 on older devices.

I tried a number of different solutions and scale factor adjustment methods, but to no avail. I know something that I just don’t see, but I can’t find it. Suggestions?

+4
source share
2 answers

I had the same problem. Call eaglLayer.contentsScale = [[UIScreen mainScreen] scale]; not required, and you need to multiply the frame size using the screen scale. So the solution for your problem would be:

 CGFloat screenScale = [UIScreen mainScreen].scale; glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, CGRectGetWidth(self.bounds) * screenScale, CGRectGetHeight(self.bounds) * screenScale); 
+4
source

Someone else said that calling eaglLayer.contentsScale is not needed, but for some reason this only works when I call it. I use the following code in a subclass of UIView, and rendering works fine on all devices:

 CGFloat scale = [[UIScreen mainScreen] scale]; _eaglLayer.contentsScale = scale; [_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer]; CGSize s = self.frame.size; glViewport(0, 0, s.width * scale, s.height * scale); 
+5
source

Source: https://habr.com/ru/post/1489621/


All Articles