# HG changeset patch # User Matti Hamalainen # Date 1572190075 -7200 # Node ID b45d8958e5a60b2a5aba9acc2787b7b663d424ab # Parent 4727156927ea223de44d65037339e00e381bbf48 Cosmetic indentation. diff -r 4727156927ea -r b45d8958e5a6 glxdragon.cpp --- a/glxdragon.cpp Sun Oct 27 17:19:47 2019 +0200 +++ b/glxdragon.cpp Sun Oct 27 17:27:55 2019 +0200 @@ -41,52 +41,52 @@ struct Mesh { - std::vector vertices; - std::vector faces; + std::vector vertices; + std::vector faces; } dragonMesh; void init(const int windowWidth, const int windowHeight, const std::string& windowTitle) { - std::stringstream ss; + std::stringstream ss; - if (SDL_Init(SDL_INIT_VIDEO) != 0) - { - ss << "Unable to initialize SDL: " << SDL_GetError() << '\n'; + if (SDL_Init(SDL_INIT_VIDEO) != 0) + { + ss << "Unable to initialize SDL: " << SDL_GetError() << '\n'; - throw std::runtime_error(ss.str()); - } + throw std::runtime_error(ss.str()); + } - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_WM_SetCaption(windowTitle.c_str(), 0); + SDL_WM_SetCaption(windowTitle.c_str(), 0); - if ((SDL_SetVideoMode(windowWidth, windowHeight, 0, SDL_OPENGL)) == NULL) - { - ss << "Couldn't set GL mode: " << SDL_GetError() << '\n'; + if ((SDL_SetVideoMode(windowWidth, windowHeight, 0, SDL_OPENGL)) == NULL) + { + ss << "Couldn't set GL mode: " << SDL_GetError() << '\n'; - throw std::runtime_error(ss.str()); - } + throw std::runtime_error(ss.str()); + } - std::cout << "GL_VENDOR : " << glGetString(GL_VENDOR) << std::endl; - std::cout << "GL_RENDERER : " << glGetString(GL_RENDERER) << std::endl; - std::cout << "GL_VERSION : " << glGetString(GL_VERSION) << std::endl; + std::cout << "GL_VENDOR : " << glGetString(GL_VENDOR) << std::endl; + std::cout << "GL_RENDERER : " << glGetString(GL_RENDERER) << std::endl; + std::cout << "GL_VERSION : " << glGetString(GL_VERSION) << std::endl; - std::cout << std::endl; + std::cout << std::endl; - // Setup the window and view port - glViewport(0, 0, windowWidth, windowHeight); + // Setup the window and view port + glViewport(0, 0, windowWidth, windowHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); @@ -97,8 +97,8 @@ glLoadIdentity(); - // Enable back face culling - glEnable(GL_CULL_FACE); + // Enable back face culling + glEnable(GL_CULL_FACE); // Enable smooth shading glShadeModel(GL_SMOOTH); @@ -135,14 +135,14 @@ glEnable(GL_LIGHT0); // Define the light components and position - GLfloat ambient [] = { 0.2f, 0.2f, 0.2f, 1.0f }; - GLfloat diffuse [] = { 0.8f, 0.8f, 0.8f, 1.0f }; - GLfloat specular[] = { 0.5f, 0.5f, 0.5f, 1.0f }; + GLfloat ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; + GLfloat diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; + GLfloat specular[] = { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat position[] = { 10.0f, 10.0f, 0.0f, 0.0f }; // Define the light components and position - glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); - glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); + glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, specular); glLightfv(GL_LIGHT0, GL_POSITION, position); @@ -153,7 +153,7 @@ void done() { - SDL_Quit(); + SDL_Quit(); } @@ -161,19 +161,19 @@ { int maxIndices; - glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); + glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); - glVertexPointer(3, GL_FLOAT, 24, &mesh.vertices[0]); - glNormalPointer( GL_FLOAT, 24, &mesh.vertices[3]); + glVertexPointer(3, GL_FLOAT, 24, &mesh.vertices[0]); + glNormalPointer(GL_FLOAT, 24, &mesh.vertices[3]); - for(size_t n = 0; n < mesh.faces.size() / 3; n += maxIndices) - { - const int count = std::min(maxIndices, int(mesh.faces.size() / 3 - n)); + for (size_t n = 0; n < mesh.faces.size() / 3; n += maxIndices) + { + const int count = std::min(maxIndices, int(mesh.faces.size() / 3 - n)); - glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &dragonMesh.faces[n * 3]); - } + glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &dragonMesh.faces[n * 3]); + } } @@ -182,40 +182,40 @@ glClear(GL_DEPTH_BUFFER_BIT); - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); - glOrtho(0.0, 1.0, 0.0, 1.0, -1, 1); + glOrtho(0.0, 1.0, 0.0, 1.0, -1, 1); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); // Draw the background gradient - glBegin(GL_QUADS); - { - glColor3ub(0x3B, 0x3B, 0x75); - glVertex2f(0.0f, 0.0f); - glVertex2f(1.0f, 0.0f); + glBegin(GL_QUADS); + { + glColor3ub(0x3B, 0x3B, 0x75); + glVertex2f(0.0f, 0.0f); + glVertex2f(1.0f, 0.0f); - glColor3ub(0x00, 0x00, 0x00); - glVertex2f(1.0f, 1.0f); - glVertex2f(0.0f, 1.0f); - } - glEnd(); + glColor3ub(0x00, 0x00, 0x00); + glVertex2f(1.0f, 1.0f); + glVertex2f(0.0f, 1.0f); + } + glEnd(); - // Restore the 3D projection - glMatrixMode(GL_PROJECTION); - glPopMatrix(); + // Restore the 3D projection + glMatrixMode(GL_PROJECTION); + glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); glEnable(GL_DEPTH_TEST); @@ -245,85 +245,82 @@ void runEventLoop() { - static std::clock_t startTime = std::clock(); + static std::clock_t startTime = std::clock(); - while (true) - { - SDL_Event event; + while (true) + { + SDL_Event event; - if (SDL_PollEvent(&event)) - { - if (event.type == SDL_QUIT) - break; - } - else - { - // Render the next frame and test if a full turn was completed - if (paintGL()) - { - // Get the time it took to render a full turn - double time = double(std::clock() - startTime) / CLOCKS_PER_SEC; + if (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + break; + } + else + { + // Render the next frame and test if a full turn was completed + if (paintGL()) + { + // Get the time it took to render a full turn + double time = double(std::clock() - startTime) / CLOCKS_PER_SEC; - // Print the current frames per second - std::printf("%.1lf seconds for 180 frames = %.1lf FPS\n", time, 180 / time); + // Print the current frames per second + std::printf("%.1lf seconds for 180 frames = %.1lf FPS\n", time, 180 / time); - // Restart the timer - startTime = std::clock(); - } - } - } + // Restart the timer + startTime = std::clock(); + } + } + } } void loadMesh(const std::string& fileName, Mesh& mesh) { - std::ifstream in(fileName.c_str(), std::ios::binary); - - - if (!in.is_open()) - { - std::stringstream ss; - - ss << "Unable to open file: " << fileName << '\n'; - - throw std::runtime_error(ss.str()); - - } + std::ifstream in(fileName.c_str(), std::ios::binary); - mesh.vertices.resize(100139 * 6); + if (!in.is_open()) + { + std::stringstream ss; - in.read(reinterpret_cast(&mesh.vertices[0]), 100139 * 6 * 4); + ss << "Unable to open file: " << fileName << '\n'; + + throw std::runtime_error(ss.str()); + + } - mesh.faces.resize(200198 * 3); + mesh.vertices.resize(100139 * 6); + in.read(reinterpret_cast(&mesh.vertices[0]), 100139 * 6 * 4); - for(unsigned i = 0; i < 200198; i++) - { - in.seekg(1, std::ios::cur); + mesh.faces.resize(200198 * 3); + for (unsigned i = 0; i < 200198; i++) + { + in.seekg(1, std::ios::cur); - in.read(reinterpret_cast(&mesh.faces[i*3]), 3 * 4); - } + in.read(reinterpret_cast(&mesh.faces[i * 3]), 3 * 4); + } } int main() { - try - { - loadMesh("dragon.mesh", dragonMesh); + try + { + loadMesh("dragon.mesh", dragonMesh); - init(640, 480, "glxdragon"); + init(640, 480, "glxdragon"); - initScene(); + initScene(); - runEventLoop(); - } - catch(std::runtime_error& e) - { - std::cerr << e.what(); - } + runEventLoop(); + } + catch(std::runtime_error & e) + { + std::cerr << e.what(); + } - done(); + done(); }