Пример #1
0
int main(int argc, char **argv) { // {{{
	// init ledwand
	if (ledwand_init(&ledwand)) {
		perror("Fehler beim Init\n");
		exit(1);
	}
	srand(getpid());
	welcome();
	game = newTetrisGame(10, 20);
	printBoard(game);
	while (game->isRunning) {
		usleep(50000);
		processInputs(game);
	}
	destroyTetrisGame(game);
	ledwand_clear(&ledwand);
	ledwand_destroy(&ledwand);
	tcsetattr(STDIN_FILENO, TCSANOW, &game->termOrig);
	return 0;
} // }}}
Пример #2
0
void Game::run() {
	// build and compile our shader program
	Shader ourShader("basic.vert", "basic.frag");

	// load the map
	auto result = bsp.parse(mapName.c_str());
	
	prepareFaces();

	/////////////////////////////////////
	glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
	glEnable(GL_DEBUG_OUTPUT);
	glDebugMessageCallback(openglCallbackFunction, nullptr);

	GLuint VBO, VAO, EBO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glGenBuffers(1, &EBO);

	// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
	glBindVertexArray(VAO);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*indexes.size(), &indexes[0], GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, bsp.getVertexes().size() * sizeof(BSP_vertex), &bsp.getVertexes()[0], GL_STATIC_DRAW);

	// Position attribute
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(BSP_vertex), (GLvoid*)0);
	glEnableVertexAttribArray(0);

	// Color attribute
	glVertexAttribPointer(1, 4, GL_BYTE, GL_FALSE, sizeof(BSP_vertex), (GLvoid*)((10 * sizeof(GL_FLOAT))));
	glEnableVertexAttribArray(1);

	// Texel attribute
	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(BSP_vertex), (GLvoid*)((3 * sizeof(GL_FLOAT))));
	glEnableVertexAttribArray(2);

	// Texture array depth/id attribute
	glVertexAttribIPointer(3, 1, GL_INT, sizeof(BSP_vertex), (GLvoid*)((4 * sizeof(GLbyte)) + (10 * sizeof(GL_FLOAT))));
	glEnableVertexAttribArray(3);

	// Lightmap array depth/id attribute
	glVertexAttribIPointer(4, 1, GL_INT, sizeof(BSP_vertex), (GLvoid*)((1 * sizeof(GLint)) + (4 * sizeof(GLbyte)) + (10 * sizeof(GL_FLOAT))));
	glEnableVertexAttribArray(4);

	// Lightmap uv
	glVertexAttribPointer(5, 2, GL_FLOAT, GL_FALSE, sizeof(BSP_vertex), (GLvoid*)(5 * sizeof(GL_FLOAT)));
	glEnableVertexAttribArray(5);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);

	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	glBindVertexArray(0); // Unbind VAO

	glm::mat4 projection = camera.getProjection();
	camera.setPosition(glm::vec3(0.0, 0.0, 40.0));

	ourShader.Use();
	auto texLocation = glGetUniformLocation(ourShader.Program, "_texture");
	auto lmLocation = glGetUniformLocation(ourShader.Program, "_lightmap");

	glUniform1i(texLocation, 0);
	glUniform1i(lmLocation, 1);

	assert(texLocation != -1, "texLocation uniform was optimized");
	assert(lmLocation != -1, "lmLocation uniform was optimized");

	while (!glfwWindowShouldClose(window))
	{
		deltaTime = glfwGetTime() - lastFrameTime;
		lastFrameTime = glfwGetTime();

		// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
		glfwPollEvents();
		processInputs();

		// Render
		// Clear the colorbuffer
		glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glActiveTexture(GL_TEXTURE0 + 0);
		glBindTexture(GL_TEXTURE_2D_ARRAY, bsp.getTextureHandle());

		glActiveTexture(GL_TEXTURE0 + 1);
		glBindTexture(GL_TEXTURE_2D_ARRAY, bsp.getLightmapHandle());

		GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
		GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
		GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
		GLint angleLoc = glGetUniformLocation(ourShader.Program, "angle");

		glm::mat4 view;
		camera.setDeltaTime(deltaTime);
		view = camera.getView();

		// Get their uniform location
		glUniform1f(angleLoc, glfwGetTime());
		glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
		glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));

		glm::mat4 model = glm::mat4(1.0);	// just identity

		glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

		glBindVertexArray(VAO);

		glBindBuffer(GL_ARRAY_BUFFER, VBO);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);

		glDrawElements(GL_TRIANGLES, indexes.size(), GL_UNSIGNED_INT, 0);

		glBindVertexArray(0);

		glfwSwapBuffers(window);
	}

	// Properly de-allocate all resources once they've outlived their purpose
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);

	glfwTerminate();
}