virtual void initializeGL() { // Compile and link GLSL programs. The last three include geometry shader cout << "Creating Gouraud program..." << endl; pgmGouraud = Program::createProgram( ShaderFile(Vert,"demo2-shaders/Gouraud/vtxGouraud.glsl"), ShaderFile(Frag,"demo2-shaders/Gouraud/frgGouraud.glsl") ); cout << "Creating Phong program..." << endl; pgmPhong = Program::createProgram( ShaderFile(Vert,"demo2-shaders/Phong/vtxPhong.glsl"), ShaderFile(Frag,"demo2-shaders/Phong/frgPhong.glsl") ); cout << "Creating Flat program..." << endl; pgmFlat = Program::createProgram( ShaderFile(Vert,"demo2-shaders/Flat/vtxFlat.glsl"), ShaderFile(Geom,"demo2-shaders/Flat/geoFlat.glsl"), ShaderFile(Frag,"demo2-shaders/Flat/frgFlat.glsl") ); cout << "Creating WFlat program..." << endl; pgmWFlat = Program::createProgram( ShaderFile(Vert,"demo2-shaders/Wire/vtxWire.glsl"), ShaderFile(Geom,"demo2-shaders/Wire/geoWire.glsl"), ShaderFile(Frag,"demo2-shaders/Wire/frgWire.glsl") ); cout << "Creating Double program..." << endl; pgmDouble = Program::createProgram( ShaderFile(Vert,"demo2-shaders/Double/vtxDouble.glsl"), ShaderFile(Geom,"demo2-shaders/Double/geoDouble.glsl"), ShaderFile(Frag,"demo2-shaders/Double/frgDouble.glsl") ); // Mesh is a helper class for dealing with triangle meshes. // Construct a mesh object; this reads the mesh from the file (the argument) // getArgv() (inherited from EventHandlerBase) returns the command line arguments Mesh M(getArgv()[1]); // Get information about the mesh. Methods used here: // getCenter() returns center of the bounding box // get*Corner() returns corners of the bounding box; Upper = (max x coord, max y coord, max z coord), // Lower = (min x coord, min y coord, min z coord) // getMaxDim(): maximum dimension of the bounding box, or largest value of // max coord-min coord over all coordinates; rough estimate of size of the mesh // getTriangleCount(), getVertexCount() - return what they say // getVertexTable(): returns vertex table (array of type vec3* with num of entries = #vertices) // getVertexNormals(): returns area weighted vertex normals (array of type vec3*) // getTriangleTable(): returns triangle table, array of type uvec3* with #entries = #triangles // entries are unsigned so it can be readily used as data for the index buffer // CAUTION: DON'T delete any arrays returned by the methods of the mesh class! Or you'll corrupt // your mesh object center = M.getCenter(); mx = M.getUpperCorner(); mn = M.getLowerCorner(); maxdim = M.getMaxDim(); ts = M.getTriangleCount(); // Buffer class represents OpenGL Vertex Buffer Objects (VBOs), basically arrays residing // in the GPU memory. // The constructor creates a buffer object and sends data to it. // Versions of the constructor used here take #entries of an array as the first argument and // the pointer to the array as the second argument (that pointer can be of type // ivec[234]*, uvec[234]* or vec[234]*). vloc = new Buffer(M.getVertexCount(),M.getVertexTable()); vnormal = new Buffer(M.getVertexCount(),M.getVertexNormals()); // IndexBuffer class represent index buffer objects; Note that they are required to have // entries of an unsigned integer type. // create an index buffer; first entry: size of array, second: array (types allowed: // GLubyte*, GLuint*, GLushort*, uvec2*, uvec3*) ix = new IndexBuffer(M.getTriangleCount(),M.getTriangleTable()); // Build a vertex array object (VAO; they are represented by VertexArray class). // Basically, a VAO tells the system where to find attributes for a vertex. // Here, (after creating an empty VAO) we tell it to look up attribute #0 from the // buffer vloc and attribute #1 - from the buffer vnormal. These have to match the location // layout qualifier in the vertex shader. In our case, we have these lines in the vertex shaders: // layout(location=0) in vec3 coord; [for vertex i, use vloc[i] as value] // layout(location=1) in vec3 normal; [ ... use vnormal[i] as value] vaGouraudPhong = new VertexArray; vaGouraudPhong->attachAttribute(0,vloc); vaGouraudPhong->attachAttribute(1,vnormal); // enable culling and depth test; use white when clearing the color buffer glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glClearColor(1.0, 1.0, 1.0, 1.0); glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE); // Build menu; the code is set up so that it is automatically attached to the right mouse button // It should clear what happens here (try to run the code and press the right mouse button // over the window). If I got everything right, you should get an error if the menus/submenus // are not properly nested. The second argument of addMenuEntry is a name of the function // that is called when that entry is selected. Because of the requirements of the freeglut // library this is based on, this has to be a function that has a well-defined address // (e.g. a static method). See one of the comments above. beginMenu(); beginSubMenu("Mode"); addMenuEntry("Flat",do_Flat); addMenuEntry("Wireframe-like flat",do_WFlat); addMenuEntry("Double",do_Double); addMenuEntry("Gouraud",do_Gouraud); addMenuEntry("Phong",do_Phong); endSubMenu(); addMenuEntry("Reorient",reorient); endMenu(); }
void initializeGL() { vec2 qbuf[4]; qbuf[0] = vec2(-1,-1); qbuf[1] = vec2(1,-1); qbuf[2] = vec2(-1,1); qbuf[3] = vec2(1,1); Buffer *q = new Buffer(4,qbuf); vaQuad = new VertexArray; vaQuad->attachAttribute(0,q); pgmPhong = Program::createProgram(ShaderFile(Vert,"dpeel-shaders/vtxPhong.glsl"), ShaderFile(Frag,"dpeel-shaders/frgPhong.glsl")); pgmQuad = Program::createProgram(ShaderFile(Vert,"dpeel-shaders/vtxQuad.glsl"), ShaderFile(Frag,"dpeel-shaders/frgQuad.glsl")); Mesh M(getArgv()[1]); center = M.getCenter(); mx = M.getUpperCorner(); mn = M.getLowerCorner(); maxdim = M.getMaxDim(); ts = M.getTriangleCount(); vloc = new Buffer(M.getVertexCount(),M.getVertexTable()); vnormal = new Buffer(M.getVertexCount(),M.getVertexNormals()); ix = new IndexBuffer(M.getTriangleCount(),M.getTriangleTable()); vaGouraudPhong = new VertexArray(); vaGouraudPhong->attachAttribute(0,vloc); vaGouraudPhong->attachAttribute(1,vnormal); glDisable(GL_CULL_FACE); glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(0.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); texsize = 800; fb = new Framebuffer; tex_depth[0] = new Texture(Depth,texsize,texsize); // clear tex_depth[1] = new Texture(Depth,texsize,texsize); tex_color = new Texture(RGBA,texsize,texsize); fb->attachDepth(tex_depth[0]); glClearDepth(1.0); fb->attachColor(tex_color); tex_color->bind(1); tex_depth[0]->nearest(); tex_depth[1]->nearest(); tex_color->nearest(); beginMenu(); addMenuEntry("Peeling",do_peel); addMenuEntry("Naive",do_naive); endMenu(); glutAttachMenu(GLUT_RIGHT_BUTTON); glGenQueries(1,&query); }
void initializeGL() { // build a vertex array containing a 2D square extending from -1 to 1. // we'll be rendering textures as a square with color pattern looked up from // the texture applied to it vec2 qv[4] = { vec2(-1,-1), vec2(1,-1), vec2(-1,1), vec2(1,1) }; qbuf = new Buffer(4,qv); vaSquare = new VertexArray; vaSquare->attachAttribute(0,qbuf); // we'll use the familiar Phong program and a program to apply texture to the square pgmPhong = createProgram(ShaderFile(Vert,"shaders/vtxPhong.glsl"), ShaderFile(Frag,"shaders/frgPhong.glsl")); pgmSquare = createProgram(ShaderFile(Vert,"shaders/vtxSquare.glsl"), ShaderFile(Frag,"shaders/frgSquare.glsl")); // this should look familiar: use the Mesh class to read mesh and get // vertex locations, normals and triangle table (to be used as the index buffer) Mesh M(getArgv()[1]); center = M.getCenter(); maxdim = M.getMaxDim(); ts = M.getTriangleCount(); vloc = new Buffer(M.getVertexCount(),M.getVertexTable()); vnormal = new Buffer(M.getVertexCount(),M.getVertexNormals()); ix = new IndexBuffer(M.getTriangleCount(),M.getTriangleTable()); // build the VAO for the standard Phong program vaPhong = new VertexArray(); vaPhong->attachAttribute(0,vloc); vaPhong->attachAttribute(1,vnormal); // want to use culling, depth test and white background glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glClearColor(0.0, 0.0, 0.0, 1.0); glEnable(GL_DEPTH_TEST); // build the framebuffer make_fb(); // ... and menu beginMenu(); addMenuEntry("Reorient",reorient); addMenuEntry("Toggle texture",tog_texture); addMenuEntry("Toggle light movement", tog_anim); addMenuEntry("Toggle interpolation", tog_interpolation); beginSubMenu("Texture size"); addMenuEntry("2048",res_2048); addMenuEntry("1024",res_1024); addMenuEntry("512",res_512); addMenuEntry("256",res_256); addMenuEntry("128",res_128); addMenuEntry("64",res_64); endSubMenu(); endMenu(); // register the timer callback glutTimerFunc(30,timerFunc,0); // make timerFunc(0) call in 30 ms }