int graphics3d_init(int sw,int sh,int fullscreen,const char *project,Uint32 frameDelay) { const unsigned char *version; GLenum glew_status; if (SDL_Init(SDL_INIT_VIDEO) < 0) { slog("failed to initialize SDL!"); return -1; } atexit(SDL_Quit); __graphics3d_frame_delay = frameDelay; __graphics3d_window = SDL_CreateWindow(project?project:"gametest3d", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sw, sh, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); __graphics3d_gl_context = SDL_GL_CreateContext(__graphics3d_window); if (__graphics3d_gl_context == NULL) { slog("There was an error creating the OpenGL context!\n"); return -1; } version = glGetString(GL_VERSION); if (version == NULL) { slog("There was an error creating the OpenGL context!\n"); return -1; } SDL_GL_MakeCurrent(__graphics3d_window, __graphics3d_gl_context); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); //MUST make a context AND make it current BEFORE glewInit()! glewExperimental = GL_TRUE; glew_status = glewInit(); if (glew_status != 0) { slog("Error: %s", glewGetErrorString(glew_status)); return -1; } __graphics3d_shader_program = BuildShaderProgram("shaders/vs1.glsl", "shaders/fs1.glsl"); if (__graphics3d_shader_program == -1) { return -1; } glViewport(0,0,sw, sh); glMatrixMode(GL_PROJECTION); glLoadIdentity(); /*view angle, aspect ratio, near clip distance, far clip distance*/ /*TODO: put near/far clip in graphics view config*/ gluPerspective( 90, (float)sw / (float)sh, .01, 2000.0f); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glClearColor(0.0,0.0,0.0,0.0); glClear( 1 ); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); /*Enables alpha testing*/ /* glAlphaFunc(GL_GREATER,0); glEnable(GL_ALPHA_TEST);*/ graphics3d_setup_default_light(); atexit(graphics3d_close); return 0; }
void MyScene::initialize() { checkOpenGLErrors("MyScene::initialize0"); // Initialize OpenGL glEnable(GL_DEPTH_TEST); // Turn depth testing glClearColor(0.5f, 0.5f, 0.5f, 1.0f); // Set the window clear color // Build shader proram ShaderInfo shaders[] = { { GL_VERTEX_SHADER, "vertexShaderPerPixel.vs.glsl" }, { GL_FRAGMENT_SHADER, "fragmentShaderPerPixel.fs.glsl" }, { GL_NONE, NULL } // signals that there are no more shaders }; perPixelShaderProgram = BuildShaderProgram(shaders); checkOpenGLErrors("MyScene::initialize1"); // Set up the uniform blocks for this shader SharedProjectionAndViewing::setUniformBlockForShader(perPixelShaderProgram); SharedMaterialProperties::setUniformBlockForShader(perPixelShaderProgram); SharedGeneralLighting::setUniformBlockForShader(perPixelShaderProgram); // Build shader proram ShaderInfo shadersPointFive[] = { { GL_VERTEX_SHADER, "vertexShaderPerPixel.vs.glsl" }, { GL_FRAGMENT_SHADER, "fragmentShaderPerPixelMultiTexture.fs.glsl" }, { GL_NONE, NULL } // signals that there are no more shaders }; GLuint modelShaderProgram = BuildShaderProgram(shadersPointFive); checkOpenGLErrors("MyScene::initialize1"); // Set up the uniform blocks for this shader SharedProjectionAndViewing::setUniformBlockForShader(modelShaderProgram); SharedMaterialProperties::setUniformBlockForShader(modelShaderProgram); SharedGeneralLighting::setUniformBlockForShader(modelShaderProgram); // Build shader proram ShaderInfo shaders2[] = { { GL_VERTEX_SHADER, "vertexShader.vs.glsl" }, { GL_FRAGMENT_SHADER, "fragmentShader.fs.glsl" }, { GL_NONE, NULL } // signals that there are no more shaders }; shaderProgram = BuildShaderProgram(shaders2); checkOpenGLErrors("MyScene::initialize2"); // Set up the uniform blocks for this shader SharedProjectionAndViewing::setUniformBlockForShader(shaderProgram); SharedMaterialProperties::setUniformBlockForShader(shaderProgram); SharedGeneralLighting::setUniformBlockForShader(shaderProgram); Cube* cube = new Cube(); //cube->initialize(); cube->material.setAmbientAndDiffuseMat(glm::vec4(0.1f, 0.1f, 1.0f, 1.0f)); cube->material.setupTexture("Brick.bmp", DECAL); //cube->addBehavior(new Behavior()); vector<glm::vec3> p; p.push_back(glm::vec3(0.0f, 0.0f, 0.0f)); p.push_back(glm::vec3(0.0f, 0.0f, 20.0f)); p.push_back(glm::vec3(0.0f, 10.0f, 20.0f)); p.push_back(glm::vec3(0.0f, 10.0f, -20.0f)); //points.push_back(glm::vec3(0.0f, 0.0f, 0.0f)); //points.push_back(glm::vec3(0.0f, 0.0f, 5.0f)); cube->addBehavior(new WaypointBehavior(p, 1)); addChild(cube); Sphere* sun = new Sphere(1, 64, 64); //sphere->initialize(); sun->material.setAmbientAndDiffuseMat(glm::vec4(1.0f, 1.0f, 0.1f, 1.0f)); sun->material.setupTexture("preview_sun.jpg", REPLACE_AMBIENT_DIFFUSE); sun->addBehavior(new Behavior()); sun->addBehavior(new OrbitBehavior(glm::vec3(0.0f, 1.0f, 0.0f), 2.0f, 1.0f)); addChild(sun); Sphere* earth = new Sphere(0.5); //earth->initialize(); earth->material.setAmbientAndDiffuseMat(glm::vec4(0.0f, 0.5f, 0.0f, 1.0f)); earth->material.setupTexture("earth.bmp", REPLACE_AMBIENT_DIFFUSE); earth->addBehavior(new Behavior()); earth->addBehavior(new OrbitBehavior(glm::vec3(0.0f, 1.0f, 0.0f), 2.5f, 1.5f)); sun->addChild(earth); Sphere* moon = new Sphere(0.25); //moon->initialize(); moon->material.setupTexture("moon.bmp", REPLACE_AMBIENT_DIFFUSE); moon->addBehavior(new Behavior()); moon->addBehavior(new OrbitBehavior(glm::vec3(0.0f, 1.0f, 0.0f), 1.0f, 2.0f)); earth->addChild(moon); //Model =================================================== AssimpModel* model = new AssimpModel("model/nanosuit.obj"); //model->initialize(); model->setShader(modelShaderProgram); model->material.setTextureMapped(REPLACE_AMBIENT_DIFFUSE); //model->material.setSpecularExponentMat(16.0f); model->addBehavior(new Behavior()); model->scale = glm::scale(glm::vec3(0.2f, 0.2f, 0.2f)); model->addBehavior(new SpinBehavior(glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::radians(30.0f))); addChild(model); //========================================================= // Initialize the shader for all the obects selectShader(0); checkOpenGLErrors("MyScene::initialize3"); Camera* cam1 = new Camera(); cam1->setViewPort(0.5, 0, 0.5, 1); cam1->enable(true); cam1 = new Camera(); cam1->setViewPort(0, 0, 0.5, 1); cam1->enable(true); // Viewing transformation //glm::mat4 viewingTransformation = glm::lookAt(glm::vec3(0.0f, 0.0f, 30.0f), // glm::vec3(0.0f, 0.0f, 0.0f), // glm::vec3(0.0f, 1.0f, 0.0f)); // Set viewing transformation //SharedProjectionAndViewing::setViewMatrix(viewingTransformation); checkOpenGLErrors("MyScene::initialize4"); // Light 1 glm::vec4 light1AmbColor(0.15f, 0.15f, 0.15f, 1.0f); glm::vec4 light1DifColor(1.0f, 1.0f, 1.0f, 1.0f); glm::vec4 light1SpecColor(1.0f, 1.0f, 1.0f, 1.0f); glm::vec4 light1PositionOfDirection(5.0f, 5.0f, 5.0f, 1.0f); bool light1IsSpot = false; bool light1Enabled = true; SharedGeneralLighting::setAmbientColor(GL_LIGHT_ZERO, light1AmbColor); SharedGeneralLighting::setDiffuseColor(GL_LIGHT_ZERO, light1DifColor); SharedGeneralLighting::setSpecularColor(GL_LIGHT_ZERO, light1SpecColor); SharedGeneralLighting::setPositionOrDirection(GL_LIGHT_ZERO, light1PositionOfDirection); SharedGeneralLighting::setIsSpot(GL_LIGHT_ZERO, light1IsSpot); SharedGeneralLighting::setEnabled(GL_LIGHT_ZERO, light1Enabled); checkOpenGLErrors("MyScene::initialize5"); // Light 2 glm::vec4 light2AmbColor(0.15f, 0.15f, 0.15, 1.0f); glm::vec4 light2DifColor(0.75f, 0.75f, 0.75f, 1.0f); glm::vec4 light2SpecColor(1.0f, 1.0f, 1.0f, 1.0f); glm::vec4 light2PositionOfDirection(1.0f, 1.0f, 1.0f, 0.0f); bool light2IsSpot = false; bool light2Enabled = true; SharedGeneralLighting::setAmbientColor(GL_LIGHT_ONE, light2AmbColor); SharedGeneralLighting::setDiffuseColor(GL_LIGHT_ONE, light2DifColor); SharedGeneralLighting::setSpecularColor(GL_LIGHT_ONE, light2SpecColor); SharedGeneralLighting::setPositionOrDirection(GL_LIGHT_ONE, light2PositionOfDirection); SharedGeneralLighting::setIsSpot(GL_LIGHT_ONE, light2IsSpot); SharedGeneralLighting::setEnabled(GL_LIGHT_ONE, light2Enabled); checkOpenGLErrors("MyScene::initialize6"); // Light 3 glm::vec4 light3AmbColor(0.15f, 0.15f, 0.15, 1.0f); glm::vec4 light3DifColor(0.9f, 0.9f, 0.9f, 1.0f); glm::vec4 light3SpecColor(1.0f, 1.0f, 1.0f, 1.0f); glm::vec4 light3PositionOfDirection(0.0f, 0.0f, 12.0f, 1.0f); glm::vec3 light3SpotDirection(0.0f, 0.0f, -1.0f); bool light3IsSpot = true; GLfloat light3SpotCutOff = glm::cos(glm::radians(10.0f)); GLfloat light3SpotExponent = 0.9f; bool light3Enabled = true; SharedGeneralLighting::setAmbientColor(GL_LIGHT_TWO, light3AmbColor); SharedGeneralLighting::setDiffuseColor(GL_LIGHT_TWO, light3DifColor); SharedGeneralLighting::setSpecularColor(GL_LIGHT_TWO, light3SpecColor); SharedGeneralLighting::setPositionOrDirection(GL_LIGHT_TWO, light3PositionOfDirection); SharedGeneralLighting::setSpotDirection(GL_LIGHT_TWO, light3SpotDirection); SharedGeneralLighting::setIsSpot(GL_LIGHT_TWO, light3IsSpot); SharedGeneralLighting::setSpotCutoffCos(GL_LIGHT_TWO, light3SpotCutOff); SharedGeneralLighting::setSpotExponent(GL_LIGHT_TWO, light3SpotExponent); SharedGeneralLighting::setEnabled(GL_LIGHT_TWO, light3Enabled); checkOpenGLErrors("MyScene::initialize7"); VisibleObject::initialize(); // Initialize the shader for all the obects selectShader(0); }