clear screen / paint background before loading textures v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 29 Nov 2023 01:21:17 +0100
branchv_0
changeset 3 48dc4ae894b0
parent 2 3faef2f5128e
child 4 9aba96f0b001
clear screen / paint background before loading textures
Shark.cpp
Shark.h
--- a/Shark.cpp	Wed Nov 29 01:11:19 2023 +0100
+++ b/Shark.cpp	Wed Nov 29 01:21:17 2023 +0100
@@ -65,9 +65,13 @@
 	GLXContext glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
 	glXMakeCurrent(dpy, win, glc);
 
+	clear();
+	glXSwapBuffers(dpy, win);
+
+
 	// Load GLSL shaders:
-	GLuint shaderProgram = loadShaders();
-	loadTextures(shaderProgram);
+	shaderProgram = loadShaders();
+	loadTextures();
 	loadVertices();
 
 	auto toggleFullscreen = [&]() {
@@ -95,7 +99,7 @@
 	}
 
 	// rended the 3D scene even before the first event:
-	runShaders(shaderProgram);
+	runShaders();
 	glXSwapBuffers(dpy, win);
 
 	for (XEvent xev; keepRunningX11;) {
@@ -180,7 +184,7 @@
 				}
 
 				if (redraw) {
-					runShaders(shaderProgram);
+					runShaders();
 					glXSwapBuffers(dpy, win);
 				}
 			} else if (epoll[epollEvent].data.fd == STDIN_FILENO) {
@@ -215,18 +219,20 @@
 	XCloseDisplay(dpy);
 }
 
-void Shark::runShaders(GLuint program) {
-	std::cerr << "GLSL: runShaders(" << program << ")" << std::endl;
-	std::cerr << "background color: " << cfg.backgroundColor << std::endl;
-	glUseProgram(program);
-	checkError(&std::cerr);
-
+void Shark::clear() {
 	glClearColor(
 			(cfg.backgroundColor >> 16 & 0xFF) / 256.,
 			(cfg.backgroundColor >> 8 & 0xFF) / 256.,
 			(cfg.backgroundColor & 0xFF) / 256.,
 			1.0);
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+}
+
+void Shark::runShaders() {
+	glUseProgram(shaderProgram);
+	checkError(&std::cerr);
+
+	clear();
 
 	GLint viewport[4];
 	glGetIntegerv(GL_VIEWPORT, viewport);
@@ -342,7 +348,7 @@
 	return tex;
 }
 
-void Shark::loadTextures(GLuint shaderProgram) {
+void Shark::loadTextures() {
 	for (const Configuration::Texture& tex : cfg.textures) {
 		textures.push_back(loadTexture(tex.fileName));
 		// TODO: review texture loading and binding
--- a/Shark.h	Wed Nov 29 01:11:19 2023 +0100
+++ b/Shark.h	Wed Nov 29 01:21:17 2023 +0100
@@ -170,19 +170,20 @@
 	
 	ImageLoader imageLoader;
 	
+	GLuint shaderProgram;
 	std::vector<Texture> textures;
 
 	Configuration cfg;
 	std::ostream& logOutput = std::cerr;
 
-	void renderImmediateMode();
-	void runShaders(GLuint program);
+	void clear();
+	void runShaders();
 	Window getRootWindow(Window defaultValue);
 	void log(LogLevel level, std::string message);
 	int setNonBlocking(int fd);
 	void loadVertices();
 	Texture loadTexture(const std::string& fileName);
-	void loadTextures(GLuint shaderProgram);
+	void loadTextures();
 	GLuint loadShaders();
 public:
 	Shark(const Configuration& cfg);