Compare commits

...

2 Commits

Author SHA1 Message Date
Christoph J. Scherr bac3e7dc9b wireframe 2023-10-24 10:01:10 +02:00
Christoph J. Scherr c804b558b3 polygon 2023-10-24 09:46:19 +02:00
3 changed files with 117 additions and 17 deletions

View File

@ -22,6 +22,9 @@ ShaderProgram::Base ShaderProgram::baseFromName(string name) {
} }
if (lname == "triangle") { if (lname == "triangle") {
return Triangle; return Triangle;
}
if (lname == "polygons") {
return Polygons;
} else { } else {
BOOST_LOG_TRIVIAL(fatal) << "unknown base program \"" << name << "\""; BOOST_LOG_TRIVIAL(fatal) << "unknown base program \"" << name << "\"";
exit(EXIT_USAGE); exit(EXIT_USAGE);
@ -40,6 +43,8 @@ string ShaderProgram::baseName() {
return "Empty"; return "Empty";
case ShaderProgram::Base::Triangle: case ShaderProgram::Base::Triangle:
return "Triangle"; return "Triangle";
case ShaderProgram::Base::Polygons:
return "Polygons";
default: default:
// this should never occur, as the switch case should cover all variants. // this should never occur, as the switch case should cover all variants.
throw std::runtime_error("Undefined base Program"); throw std::runtime_error("Undefined base Program");
@ -50,13 +55,14 @@ string ShaderProgram::baseName() {
* @param vertexSource C style string with NULL termination * @param vertexSource C style string with NULL termination
* @param fragSource C style string with NULL termination * @param fragSource C style string with NULL termination
*/ */
ShaderProgram::ShaderProgram(Base base, string vertexSource, ShaderProgram::ShaderProgram(Base base, string vertexSource, string fragSource,
string fragSource) { bool wireframe /* = false */) {
int success; int success;
char infoLog[512]; char infoLog[512];
const char *vertexSource_c = vertexSource.c_str(); const char *vertexSource_c = vertexSource.c_str();
const char *fragSource_c = fragSource.c_str(); const char *fragSource_c = fragSource.c_str();
this->base = base; this->base = base;
this->wireframe = wireframe;
BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\""; BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\"";
// NOTE: char arrays and char pointers are not actually the same. // NOTE: char arrays and char pointers are not actually the same.
// glShaderSource requires a char[] as it appears. We pass the // glShaderSource requires a char[] as it appears. We pass the
@ -149,8 +155,9 @@ int ShaderProgram::run(GLFWwindow *window) {
// VBOs) when it's not directly necessary. // VBOs) when it's not directly necessary.
glBindVertexArray(0); glBindVertexArray(0);
// uncomment this call to draw in wireframe polygons. if (this->wireframe) {
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
// input // input
// ----- // -----
@ -163,7 +170,8 @@ int ShaderProgram::run(GLFWwindow *window) {
// draw our first triangle // draw our first triangle
glUseProgram(this->program); glUseProgram(this->program);
glBindVertexArray(VAOTriangle); // seeing as we only have a single VAO there's no glBindVertexArray(
VAOTriangle); // seeing as we only have a single VAO there's no
// need to bind it every time, but we'll do so to // need to bind it every time, but we'll do so to
// keep things a bit more organized // keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);
@ -181,6 +189,90 @@ int ShaderProgram::run(GLFWwindow *window) {
glDeleteBuffers(1, &VBOTriangle); glDeleteBuffers(1, &VBOTriangle);
glDeleteProgram(this->program); glDeleteProgram(this->program);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
} else if (this->base == Polygons) {
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
0.5f, 0.8f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
};
unsigned int indices[] = {
// note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
unsigned int VBOTriangle, VAOTriangle, EBOindicies;
glGenBuffers(1, &EBOindicies);
glGenVertexArrays(1, &VAOTriangle);
glGenBuffers(1, &VBOTriangle);
// bind the Vertex Array Object first, then bind and set vertex buffer(s),
// and then configure vertex attributes(s).
glBindVertexArray(VAOTriangle);
glBindBuffer(GL_ARRAY_BUFFER, VBOTriangle);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOindicies);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),
(void *)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered
// VBO as the vertex attribute's bound vertex buffer object so afterwards we
// can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOindicies);
// You can unbind the VAO afterwards so other VAO calls won't accidentally
// modify this VAO, but this rarely happens. Modifying other VAOs requires a
// call to glBindVertexArray anyways so we generally don't unbind VAOs (nor
// VBOs) when it's not directly necessary.
glBindVertexArray(0);
if (this->wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
while (!glfwWindowShouldClose(window)) {
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(this->program);
glBindVertexArray(
VAOTriangle); // seeing as we only have a single VAO there's no
// need to bind it every time, but we'll do so to
// keep things a bit more organized
/* glDrawArrays(GL_TRIANGLES, 0, 3); */
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse
// moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAOTriangle);
glDeleteBuffers(1, &VBOTriangle);
glDeleteProgram(this->program);
// glfw: terminate, clearing all previously allocated GLFW resources. // glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------ // ------------------------------------------------------------------
glfwTerminate(); glfwTerminate();
@ -189,6 +281,9 @@ int ShaderProgram::run(GLFWwindow *window) {
{ {
// use the compiled shader // use the compiled shader
glUseProgram(this->program); glUseProgram(this->program);
if (this->wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
processInput(window); processInput(window);

View File

@ -7,14 +7,16 @@
class ShaderProgram { class ShaderProgram {
public: public:
enum Base { Empty, Triangle }; enum Base { Empty, Triangle, Polygons };
std::string baseName(); std::string baseName();
static Base baseFromName(std::string name); static Base baseFromName(std::string name);
ShaderProgram(Base base, std::string vertexSource, std::string fragSource); ShaderProgram(Base base, std::string vertexSource, std::string fragSource,
bool wireframe = false);
int run(GLFWwindow *window); int run(GLFWwindow *window);
protected: protected:
unsigned int vertex; unsigned int vertex;
bool wireframe;
unsigned int frag; unsigned int frag;
Base base; Base base;
unsigned int program; unsigned int program;

View File

@ -18,7 +18,8 @@ int main(int argc, char **argv) {
po::options_description desc("Available options"); po::options_description desc("Available options");
desc.add_options()("help,h", "show help")("verbose,v", "more verbose output")( desc.add_options()("help,h", "show help")("verbose,v", "more verbose output")(
"vert", po::value<string>()->required(), "vertex shader file")( "vert", po::value<string>()->required(), "vertex shader file")(
"frag", po::value<string>()->required(), "fragment shader file")( "frag", po::value<string>()->required(),
"fragment shader file")("wire,w", "activate wireframe mode")(
"base,b", po::value<string>()->required(), "base program"); "base,b", po::value<string>()->required(), "base program");
/* unused for now */ /* unused for now */
@ -104,7 +105,7 @@ int main(int argc, char **argv) {
BOOST_LOG_TRIVIAL(trace) << "Creating ShaderProgram"; BOOST_LOG_TRIVIAL(trace) << "Creating ShaderProgram";
// TODO: determinde ShaderProgram::Base with a CLI arg // TODO: determinde ShaderProgram::Base with a CLI arg
ShaderProgram shaderProgram(base, vert, frag); ShaderProgram shaderProgram(base, vert, frag, !vm["wire"].empty());
/* Run our main program */ /* Run our main program */
BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow"; BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";
@ -117,7 +118,8 @@ int main(int argc, char **argv) {
void help(char *prog, po::options_description desc) { void help(char *prog, po::options_description desc) {
cout << "Usage:" << endl cout << "Usage:" << endl
<< prog << prog
<< " [-vh] [--vert] shaders/vertex.glsl [--frag] shaders/fragment.glsl" << " [-vwh] \\\n\t--vert shaders/vertex.glsl \\\n\t--frag "
"shaders/fragment.glsl \\\n\t--base BASE_PROGRAM"
<< endl << endl
<< endl << endl
<< "return values:" << endl << "return values:" << endl
@ -132,6 +134,7 @@ void help(char *prog, po::options_description desc) {
<< "base programs:" << endl << "base programs:" << endl
<< "Empty\t\t-\tEmpty window with colored background" << endl << "Empty\t\t-\tEmpty window with colored background" << endl
<< "Triangle\t-\tA static Triangle" << endl << "Triangle\t-\tA static Triangle" << endl
<< "Polygons\t-\tA small amount of polygons" << endl
<< endl << endl
<< endl << endl
<< desc << endl; << desc << endl;