SDL2 Opengl Problem Stencil Buffer setup

Hello,

i have a problem using the stencil buffer in sdl2 with opengl.
Before it was working on my laptop. Then i reset my laptop. Now it doesnt work.

My current OS:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal

Installed packages:
install glew: sudo apt-get install libglew-dev
install sdl2: sudo apt-get install libsdl2-dev
install build-essential: sudo apt-get install build-essential

I checked my available renderer drivers:
opengl,
opengles2
software

my current video driver: x11

I set stencil buffer to 8 bits before initializing the video mode: SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);

    		SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
			if (SDL_Init(SDL_INIT_VIDEO) < 0)
			{
				std::cout << "Failed to init SDL\n";
			}
			window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
			if(!window) {
				std::cout << "Unable to create window\n";
				CheckSDLError(__LINE__);
			}
			context = SDL_GL_CreateContext(window);
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
			SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
			SDL_GL_SetSwapInterval(1);

What is wrong with it?
Checking stencil-buffer-size outputs 0 instead of 8…
Is it a new sdl2 bug? glew bug?

Thanks

jaa

Hi!
For first you need to install SDL2 and OpenGL drivers correctly: sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade && sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-net-dev mesa-common-dev build-essential

  1. By using SDL_GL_SetAttribute (), you can’t determine if there is an error setting OpenGL in your code!
  2. You need to compiler your code correctly: g++ file.cpp -o file -lSDL2 `sdl-config --cflags --libs` -lGL
  3. Why would you want to install GLEW when you have SDL2?
  4. You can’t use SDL2 drawing utilities when you are using OpenGL to draw!
  5. Try this code:
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>

SDL_Window *window;
SDL_Event event;
SDL_GLContext glContext;

const struct WindowData{
	int WIDTH=800, HEIGHT=800;
	int POS_X=SDL_WINDOWPOS_CENTERED, POS_Y=SDL_WINDOWPOS_CENTERED;
	const char *TITLE="WINDOW";
	int FLAG=SDL_WINDOW_OPENGL;
} WindowData;

bool isOpen(){
	return window != NULL ? true : false;
}

void pollEvent(){
	while(SDL_PollEvent(&event)){
		switch(event.type){
			case SDL_QUIT:
				window=NULL;
				break;
		}
	}
}

int main(){
	SDL_Init(SDL_INIT_EVERYTHING);

	window=SDL_CreateWindow(WindowData.TITLE, WindowData.POS_X,
							WindowData.POS_Y, WindowData.WIDTH,
							WindowData.HEIGHT, WindowData.FLAG);

	SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

	glContext=SDL_GL_CreateContext(window);

	SDL_GL_SetSwapInterval(SDL_FALSE);

	while(isOpen()){
		pollEvent();
	}
}

Hence, if you want to combine SDL2 drawing utilities with separate OpenGL code, you cannot do that.

Hi RedBull4,

thank you for your answer.

First of all, i dont use sdl2 draw utilities.
I draw with opengl-context.

I build my project with cmake:
cmake_minimum_required(VERSION “3.7.1”)
project(“Test”)
file(GLOB SOURCES framework/.cpp framework/.h)
file(GLOB SOURCES usage/.cpp usage/.h)
add_executable("${PROJECT_NAME}" “${SOURCES}”)
target_include_directories("${PROJECT_NAME}" PRIVATE glm)
target_link_libraries("${PROJECT_NAME}" PRIVATE GL GLEW SDL2)

I can draw, everything is fine (depth_buffer, lightning, etc.)
My only problem is that i cant use the stencil buffer anymore.
It is not available to me.
Before i reset my laptop it has worked.

Regards

jaa

So everything is fine? : >

no, i dont have a stencil buffer

So if you want to draw something with stencil buffer, you need to enable it!

Example:

glPushMatrix();
glEnable(GL_STENCIL_TEST);
... YOUR CODE.
glDisable(GL_STENCIL_TEST);
glPopMatrix();

i am afk a few hours. i will write in the evening.

Read this post : D

Hey, ok i finished my things earlier.
My problem is not how to use stencil buffer.
I know that stuff.
My problem is, that the stencil buffer is not available to me:
i set it to 8 bits, but it gives me 0 bits.
-> no stencil buffer

i think now i have an idea about my problem:
my video driver is x11.
my os is focal fossa.
There is no error thrown by my code.
-> the problem is the play of x11, opengl and my graphics-card.
Could it be a os-specific bug?

Get in touch with your graphics card community!

Hey, yes u are right.
It must be the graphics card driver, because i use direct rendering.
Therefore it cant be x11-issue.
Thanks.

I have intel hd 5500 graphics card.
It is known for bugs: https://bugs.launchpad.net/xserver-xorg-video-intel/+bug/1432194

I think this bug comes with a ubuntu-driver-update for focal-fossa…