Scene * newScene(GLboolean vsync) { Scene * scene = (Scene *) malloc(sizeof(Scene)); scene->context = (ContextSize *) malloc(sizeof(ContextSize)); scene->context->w = CONTEXT_WIDTH_INITIAL; scene->context->h = CONTEXT_HEIGHT_INITIAL; setupOpenGLContext(scene->context); checkOpenGLVersion(); setupOpenGLState(scene->context, vsync); scene->camera = newCamera(scene->context->w, scene->context->h); scene->world = getWorld("world.txt"); scene->water = getWater(); setupWater(scene->water->drawSP, scene->world); setupWorldUniforms(scene->world->sp, scene->water); return scene; }
void init (void) { //Warn students about OpenGL version before 0xC0000005 error checkOpenGLVersion(); printHelp(); /* select clearing color */ glClearColor (0.0, 0.0, 0.0, 0.0); /* initialize viewing values */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Think about this. Why is the up vector not normalized? glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; gluLookAt(0,-eyeloc,eyeloc,0,0,0,0,1,1) ; // Initialize the shaders // vertexshader = initshaders(GL_VERTEX_SHADER, "shaders/tex.vert") ; // fragmentshader = initshaders(GL_FRAGMENT_SHADER, "shaders/tex.frag") ; vertexshader = initshaders(GL_VERTEX_SHADER, "shaders/light.vert.glsl") ; fragmentshader = initshaders(GL_FRAGMENT_SHADER, "shaders/light.frag.glsl") ; GLuint program = glCreateProgram() ; shaderprogram = initprogram(vertexshader, fragmentshader) ; GLint linked; glGetProgramiv(shaderprogram, GL_LINK_STATUS, &linked) ; // * NEW * Set up the shader parameter mappings properly for lighting. islight = glGetUniformLocation(shaderprogram,"islight") ; light0dirn = glGetUniformLocation(shaderprogram,"light0dirn") ; light0color = glGetUniformLocation(shaderprogram,"light0color") ; light1posn = glGetUniformLocation(shaderprogram,"light1posn") ; light1color = glGetUniformLocation(shaderprogram,"light1color") ; ambient = glGetUniformLocation(shaderprogram,"ambient") ; diffuse = glGetUniformLocation(shaderprogram,"diffuse") ; specular = glGetUniformLocation(shaderprogram,"specular") ; shininess = glGetUniformLocation(shaderprogram,"shininess") ; // Set up the Geometry for the scene. // From OpenGL book pages 103-109 glGenBuffers(numperobj*numobjects+ncolors+1, buffers) ; // 1 for texcoords initcolorscube() ; // Initialize texture // inittexture("wood.ppm", fragmentprogram) ; inittexture("wood.ppm", shaderprogram) ; // Initialize objects initobject(FLOOR, (GLfloat *) floorverts, sizeof(floorverts), (GLfloat *) floorcol, sizeof (floorcol), (GLubyte *) floorinds, sizeof (floorinds), GL_POLYGON) ; // initobject(CUBE, (GLfloat *) cubeverts, sizeof(cubeverts), (GLfloat *) cubecol, sizeof (cubecol), (GLubyte *) cubeinds, sizeof (cubeinds), GL_QUADS) ; initobjectnocol(CUBE, (GLfloat *) cubeverts, sizeof(cubeverts), (GLubyte *) cubeinds, sizeof (cubeinds), GL_QUADS) ; // Enable the depth test glEnable(GL_DEPTH_TEST) ; glDepthFunc (GL_LESS) ; // The default option }
int Application::run(Application* self) { m_self = self; if (!framework::Utils::exists("data/gui")) { Logger::toLog("Error: could not find gui directory. Probably working directory has not been set correctly (especially if you are running from IDE).\n"); return EXIT_FAILURE; } init(); m_isRunning = true; glfwSetErrorCallback(&Application::errorCallback); if (!glfwInit()) { Logger::toLog("Error: glfwInit failed"); return EXIT_FAILURE; } glfwWindowHint(GLFW_VISIBLE, GL_TRUE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); glfwWindowHint(GLFW_RED_BITS, 8); glfwWindowHint(GLFW_GREEN_BITS, 8); glfwWindowHint(GLFW_BLUE_BITS, 8); glfwWindowHint(GLFW_ALPHA_BITS, 0); glfwWindowHint(GLFW_DEPTH_BITS, 32); glfwWindowHint(GLFW_STENCIL_BITS, 0); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, m_info.majorVersion); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, m_info.minorVersion); #ifdef _DEBUG glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); #endif glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_SAMPLES, m_info.samples); glfwWindowHint(GLFW_STEREO, m_info.flags.stereo ? GL_TRUE : GL_FALSE); // create window m_window = glfwCreateWindow(m_info.windowWidth, m_info.windowHeight, m_info.title.c_str(), m_info.flags.fullscreen ? glfwGetPrimaryMonitor() : NULL, NULL); if (!m_window) { glfwTerminate(); return EXIT_FAILURE; } glfwSetWindowSizeCallback(m_window, &Application::_setWindowSize); glfwSetKeyCallback(m_window, &Application::_onKey); glfwSetCharCallback(m_window, &Application::_onChar); glfwSetMouseButtonCallback(m_window, &Application::_onMouse); glfwSetCursorPosCallback(m_window, &Application::_onCursor); glfwSetScrollCallback(m_window, &Application::_onScroll); glfwSetInputMode(m_window, GLFW_CURSOR, m_info.flags.cursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN); // center position GLFWmonitor* primary = glfwGetPrimaryMonitor(); const GLFWvidmode* mode = glfwGetVideoMode(primary); int posx = (mode->width - m_info.windowWidth) >> 1; int posy = (mode->height - m_info.windowHeight) >> 1; glfwSetWindowPos(m_window, posx, posy); // set vsync glfwMakeContextCurrent(m_window); glfwSwapInterval((int)m_info.flags.vsync); // init GL3w gl3wInit(); std::vector<int> multisamplingLevels; if (!checkOpenGLVersion() || !checkDeviceCapabilities(multisamplingLevels)) { glfwDestroyWindow(m_window); glfwTerminate(); return EXIT_FAILURE; } #ifdef _DEBUG Logger::toLogWithFormat("Video adapter: %s - %s, OpenGL: %s\n", (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER), (const char*)glGetString(GL_VERSION)); #endif if (m_info.flags.debug) { if (gl3wIsSupported(4, 3)) { glDebugMessageCallback(debugCallback, this); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); } else if (IsExtensionSupported("GL_ARB_debug_output")) { glDebugMessageCallbackARB(debugCallback, this); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB); } } initGui(); if (!StandardGpuPrograms::init()) { glfwDestroyWindow(m_window); glfwTerminate(); return EXIT_FAILURE; } initAxes(); startup(m_rootWindow); do { glfwMakeContextCurrent(m_window); Texture::beginFrame(); if (fabs(m_lastTime) < 1e-7) { render(0); Texture::endFrame(); renderGui(0); m_lastTime = glfwGetTime(); } else { double curTime = glfwGetTime(); double delta = curTime - m_lastTime; // fps counter measureFps(delta); // rendering render(delta); Texture::endFrame(); renderGui(delta); m_lastTime = curTime; } glfwSwapBuffers(m_window); glfwPollEvents(); if (glfwWindowShouldClose(m_window)) { m_isRunning = GL_FALSE; } m_isRunning &= (glfwGetKey(m_window, GLFW_KEY_ESCAPE) == GLFW_RELEASE); } while(m_isRunning); shutdown(); destroyAllDestroyable(); destroyGui(); glfwDestroyWindow(m_window); glfwTerminate(); return EXIT_SUCCESS; }