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") {
return Triangle;
}
if (lname == "polygons") {
return Polygons;
} else {
BOOST_LOG_TRIVIAL(fatal) << "unknown base program \"" << name << "\"";
exit(EXIT_USAGE);
@ -40,6 +43,8 @@ string ShaderProgram::baseName() {
return "Empty";
case ShaderProgram::Base::Triangle:
return "Triangle";
case ShaderProgram::Base::Polygons:
return "Polygons";
default:
// this should never occur, as the switch case should cover all variants.
throw std::runtime_error("Undefined base Program");
@ -50,13 +55,14 @@ string ShaderProgram::baseName() {
* @param vertexSource C style string with NULL termination
* @param fragSource C style string with NULL termination
*/
ShaderProgram::ShaderProgram(Base base, string vertexSource,
string fragSource) {
ShaderProgram::ShaderProgram(Base base, string vertexSource, string fragSource,
bool wireframe /* = false */) {
int success;
char infoLog[512];
const char *vertexSource_c = vertexSource.c_str();
const char *fragSource_c = fragSource.c_str();
this->base = base;
this->wireframe = wireframe;
BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\"";
// NOTE: char arrays and char pointers are not actually the same.
// 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.
glBindVertexArray(0);
// uncomment this call to draw in wireframe polygons.
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (this->wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
while (!glfwWindowShouldClose(window)) {
// input
// -----
@ -163,9 +170,10 @@ int ShaderProgram::run(GLFWwindow *window) {
// 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
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);
// glBindVertexArray(0); // no need to unbind it every time
@ -181,6 +189,90 @@ int ShaderProgram::run(GLFWwindow *window) {
glDeleteBuffers(1, &VBOTriangle);
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.
// ------------------------------------------------------------------
glfwTerminate();
@ -189,6 +281,9 @@ int ShaderProgram::run(GLFWwindow *window) {
{
// use the compiled shader
glUseProgram(this->program);
if (this->wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
while (!glfwWindowShouldClose(window)) {
processInput(window);

View File

@ -7,19 +7,21 @@
class ShaderProgram {
public:
enum Base { Empty, Triangle };
enum Base { Empty, Triangle, Polygons };
std::string baseName();
static Base baseFromName(std::string name);
ShaderProgram(Base base, std::string vertexSource, std::string fragSource);
int run(GLFWwindow* window);
ShaderProgram(Base base, std::string vertexSource, std::string fragSource,
bool wireframe = false);
int run(GLFWwindow *window);
protected:
unsigned int vertex;
bool wireframe;
unsigned int frag;
Base base;
unsigned int program;
};
int mainWindow(ShaderProgram* shaderProgramm, GLFWwindow* window);
GLFWwindow* initGl();
int mainWindow(ShaderProgram *shaderProgramm, GLFWwindow *window);
GLFWwindow *initGl();
void processInput(GLFWwindow *window);
#endif

View File

@ -18,7 +18,8 @@ int main(int argc, char **argv) {
po::options_description desc("Available options");
desc.add_options()("help,h", "show help")("verbose,v", "more verbose output")(
"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");
/* unused for now */
@ -82,11 +83,11 @@ int main(int argc, char **argv) {
string buf;
string vert;
string frag;
while(!fvert.eof()) {
while (!fvert.eof()) {
getline(fvert, buf);
vert.append(buf + "\n");
}
while(!ffrag.eof()) {
while (!ffrag.eof()) {
getline(ffrag, buf);
frag.append(buf + "\n");
}
@ -104,7 +105,7 @@ int main(int argc, char **argv) {
BOOST_LOG_TRIVIAL(trace) << "Creating ShaderProgram";
// 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 */
BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";
@ -117,7 +118,8 @@ int main(int argc, char **argv) {
void help(char *prog, po::options_description desc) {
cout << "Usage:" << endl
<< 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
<< "return values:" << endl
@ -132,6 +134,7 @@ void help(char *prog, po::options_description desc) {
<< "base programs:" << endl
<< "Empty\t\t-\tEmpty window with colored background" << endl
<< "Triangle\t-\tA static Triangle" << endl
<< "Polygons\t-\tA small amount of polygons" << endl
<< endl
<< endl
<< desc << endl;