Compare commits

..

No commits in common. "devel" and "master" have entirely different histories.

3 changed files with 17 additions and 117 deletions

View File

@ -22,9 +22,6 @@ 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);
@ -43,8 +40,6 @@ 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");
@ -55,14 +50,13 @@ 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,
bool wireframe /* = false */) {
ShaderProgram::ShaderProgram(Base base, string vertexSource,
string fragSource) {
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
@ -155,9 +149,8 @@ int ShaderProgram::run(GLFWwindow *window) {
// VBOs) when it's not directly necessary.
glBindVertexArray(0);
if (this->wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
// uncomment this call to draw in wireframe polygons.
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
while (!glfwWindowShouldClose(window)) {
// input
// -----
@ -170,10 +163,9 @@ 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
@ -189,90 +181,6 @@ 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();
@ -281,9 +189,6 @@ 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,21 +7,19 @@
class ShaderProgram {
public:
enum Base { Empty, Triangle, Polygons };
enum Base { Empty, Triangle };
std::string baseName();
static Base baseFromName(std::string name);
ShaderProgram(Base base, std::string vertexSource, std::string fragSource,
bool wireframe = false);
int run(GLFWwindow *window);
ShaderProgram(Base base, std::string vertexSource, std::string fragSource);
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,8 +18,7 @@ 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")("wire,w", "activate wireframe mode")(
"frag", po::value<string>()->required(), "fragment shader file")(
"base,b", po::value<string>()->required(), "base program");
/* unused for now */
@ -83,11 +82,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");
}
@ -105,7 +104,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, !vm["wire"].empty());
ShaderProgram shaderProgram(base, vert, frag);
/* Run our main program */
BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";
@ -118,8 +117,7 @@ int main(int argc, char **argv) {
void help(char *prog, po::options_description desc) {
cout << "Usage:" << endl
<< prog
<< " [-vwh] \\\n\t--vert shaders/vertex.glsl \\\n\t--frag "
"shaders/fragment.glsl \\\n\t--base BASE_PROGRAM"
<< " [-vh] [--vert] shaders/vertex.glsl [--frag] shaders/fragment.glsl"
<< endl
<< endl
<< "return values:" << endl
@ -134,7 +132,6 @@ 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;