int main(int argc, char ** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA |GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowPosition(0,0); glutInitWindowSize(g_winSz[0], g_winSz[1]); glutCreateWindow("OpenGL Test"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); glutSpecialFunc(specialkeys); glutMotionFunc(motion); #if defined(USESVCUI) || defined(USEANTTWEAKBAR) initUIBase(); #endif glewInit(); LOGOK("initialized Glew\n"); initGL(argc, argv); glutIdleFunc(idle); glutMainLoop(); shutdown(); main::shutdownBase(); }
/* Allocate camera */ dlCamera* dlNewCamera( void ) { TRACE(); /* Allocate camera */ dlSetAlloc( ALLOC_CAMERA ); dlCamera *object = (dlCamera*)dlCalloc( 1, sizeof(dlCamera) ); if(!object) { RET("%p", NULL); return( NULL ); } /* Defaults */ object->zNear = 0.1f; object->zFar = 100.0f; object->fov = 35.0f; /* Equals to default blender fov */ /* Reset */ dlCameraReset( object ); LOGOK("NEW"); /* Increase ref counter */ object->refCounter++; /* Return the instance */ RET("%p", object); return( object ); }
/* Allocate material object */ dlMaterial* dlNewMaterial( void ) { TRACE(); /* Allocate material object */ dlSetAlloc( ALLOC_MATERIAL ); dlMaterial* object = (dlMaterial*)dlCalloc( 1, sizeof(dlMaterial) ); if(!object) { RET("%p", NULL); return( NULL ); } /* Nullify pointers */ object->texture = NULL; /* default blend modes */ object->blend1 = GL_SRC_ALPHA; object->blend2 = GL_ONE_MINUS_SRC_ALPHA; LOGOK("NEW"); /* Increase ref counter */ object->refCounter++; /* Return the instance */ RET("%p", object); return( object ); }
/* Allocate IBO object */ dlIBO* dlNewIBO( void ) { #if USE_BUFFERS unsigned int i; #endif TRACE(); /* Allocate IBO object */ dlSetAlloc( ALLOC_IBO ); dlIBO *ibo = (dlIBO*)dlCalloc( 1, sizeof(dlIBO) ); if(!ibo) { RET("%p", NULL); return( NULL ); } /* Default hint */ ibo->hint = GL_STATIC_DRAW; #if USE_BUFFERS i = 0; for(;i != DL_MAX_BUFFERS; ++i) ibo->indices[i] = NULL; #else ibo->indices = NULL; #endif LOGOK("NEW"); /* Increase ref counter */ ibo->refCounter++; /* Return IBO object */ RET("%p", ibo); return( ibo ); }
/* Allocate VBO object */ dlVBO* dlNewVBO( void ) { unsigned int i; /* Allocate VBO object */ dlSetAlloc( ALLOC_VBO ); dlVBO *vbo = (dlVBO*)dlCalloc( 1, sizeof(dlVBO) ); if(!vbo) { RET("%p", NULL); return( NULL ); } /* Default hint */ vbo->hint = GL_STATIC_DRAW; /* Allocate uvws */ vbo->uvw = dlCalloc( _dlCore.info.maxTextureUnits, sizeof(dlUVW) ); if(!vbo->uvw) { dlFree(vbo, sizeof(dlVBO)); RET("%p", NULL); return( NULL ); } /* Nullify pointers */ i = 0; for(; i != _dlCore.info.maxTextureUnits; ++i) vbo->uvw[i].coords = NULL; vbo->tstance = NULL; vbo->vertices = NULL; vbo->normals = NULL; #if VERTEX_COLOR vbo->colors = NULL; #endif LOGOK("NEW"); /* Increase ref counter */ vbo->refCounter++; /* Return VBO object */ RET("%p", vbo); return( vbo ); }
/* allocate new atlas */ dlAtlas* dlNewAtlas(void) { dlAtlas *atlas; TRACE(); dlSetAlloc( ALLOC_ATLAS ); atlas = dlCalloc( 1, sizeof(dlAtlas) ); if(!atlas) { RET("%p", NULL); return( NULL ); } /* nullify */ atlas->rect = NULL; atlas->texture = NULL; LOGOK("NEW"); atlas->refCounter++; RET("%p", atlas); return( atlas ); }
/* Allocate scene object */ dlObject* dlNewObject( void ) { TRACE(); /* Allocate scene object */ dlSetAlloc( ALLOC_SCENEOBJECT ); dlObject *object = (dlObject*)dlCalloc( 1, sizeof(dlObject) ); if(!object) { RET("%p", NULL); return( NULL ); } /* Nullify pointers */ object->material = NULL; object->vbo = NULL; object->ibo = NULL; object->animator = NULL; object->child = NULL; /* Default primitive type */ object->primitive_type = GL_TRIANGLE_STRIP; /* Default transformation */ object->scale.x = 100; object->scale.y = 100; object->scale.z = 100; /* Update matrix on start */ object->transform_changed = 1; LOGOK("NEW"); /* Increase ref counter */ object->refCounter++; /* Return the instance */ RET("%p", object); return( object ); }
bool initGLBase() { GLuint PixelFormat; PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 24; pfd.cStencilBits = 8; g_hDC = GetDC( g_hWnd ); PixelFormat = ChoosePixelFormat( g_hDC, &pfd ); SetPixelFormat( g_hDC, PixelFormat, &pfd); g_hRC = wglCreateContext( g_hDC ); wglMakeCurrent( g_hDC, g_hRC ); // calling glewinit NOW because the inside glew, there is mistake to fix... // This is the joy of using Core. The query glGetString(GL_EXTENSIONS) is deprecated from the Core profile. // You need to use glGetStringi(GL_EXTENSIONS, <index>) instead. Sounds like a "bug" in GLEW. glewInit(); //wglSwapInterval(0); #if 1 #define GLCOMPAT if(!wglCreateContextAttribsARB) wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB"); if(wglCreateContextAttribsARB) { HGLRC hRC = NULL; int attribList[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 3, #ifdef GLCOMPAT WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, #else WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, #endif WGL_CONTEXT_FLAGS_ARB, //WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB| #ifndef GLCOMPAT //WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB| // use it if you want errors when compat options still used #endif #ifdef _DEBUG WGL_CONTEXT_DEBUG_BIT_ARB #else 0 #endif ,0, 0 }; if (!(hRC = wglCreateContextAttribsARB(g_hDC, 0, attribList))) { LOGE("wglCreateContextAttribsARB() failed for OpenGL context.\n"); return false; //attribList[3] = 0; //if (!(hRC = wglCreateContextAttribsARB(g_hDC, 0, attribList))) // LOGE("wglCreateContextAttribsARB() failed for OpenGL context.\n"); } if (!wglMakeCurrent(g_hDC, hRC)) { LOGE("wglMakeCurrent() failed for OpenGL context.\n"); } else { wglDeleteContext( g_hRC ); g_hRC = hRC; #ifdef _DEBUG if(!glDebugMessageCallbackARB) { glDebugMessageCallbackARB = (PFNGLDEBUGMESSAGECALLBACKARBPROC)wglGetProcAddress("glDebugMessageCallbackARB"); glDebugMessageControlARB = (PFNGLDEBUGMESSAGECONTROLARBPROC)wglGetProcAddress("glDebugMessageControlARB"); } if(glDebugMessageCallbackARB) { glDebugMessageCallbackARB(myOpenGLCallback, NULL); glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_HIGH_ARB, 0, NULL, GL_TRUE); } #endif } } #endif glewInit(); LOGOK("Loaded Glew\n"); LOGOK("initialized OpenGL basis\n"); return true; }
//----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- bool loadMaterialEffect() { LOGI("Creating Effect ("EFFECT")\n"); fx_Effect = nvFX::IContainer::create(EFFECT); bool bRes = nvFX::loadEffectFromFile(fx_Effect, EFFECT); if(!bRes) bRes = nvFX::loadEffectFromFile(fx_Effect, SOURCE_DIR "/"EFFECT); if(!bRes) { LOGE("Failed\n"); } else { LOGOK("Loaded\n"); // // Let's keep track in interface pointers everything, for purpose of clarity // #ifdef USESVCUI if(g_pComboTech) g_pComboTech->RemoveAllItems(); #endif for(int t=0; fx_Tech = fx_Effect->findTechnique(t); t++) { fx_Tech->validate(); #ifdef USESVCUI // avoid the tech #0 and #1: only some init for states... if(t>=TECH_DEFAULT) if(g_pComboTech) g_pComboTech->AddItem(fx_Tech->getName(), t); #endif } #ifdef USESVCUI if(g_pComboTech) g_pComboTech->SetSelectedByIndex(0); #endif // set the material for the model fx_Tech = fx_Effect->findTechnique(TECH_DEFAULT); fx_TechFloor = fx_Effect->findTechnique(TECH_WIREFLOOR); // execute the pass as a way to initialize some basic GL states nvFX::ITechnique* fx_TechInit = fx_Effect->findTechnique(TECH_INIT); fx_TechInit->getPass(0)->execute(); fx_transfBlock1 = fx_Effect->findCstBuffer("transfBlock1"); fx_transfBlock1->buildGLBuffer(); // ask nvFx to take care of the UBO creation fx_transfBlock2 = fx_Effect->findCstBuffer("transfBlock2"); fx_transfBlock2->buildGLBuffer(); // ask nvFx to take care of the UBO creation #ifdef USECSTBUFUNIFORMS // example of other way to update: let's track the uniforms inside this buffer. { fx_m4_World = fx_transfBlock2->findUniform("m4_World"); fx_m4_WorldView = fx_transfBlock2->findUniform("m4_WorldView"); fx_m4_WorldViewProj = fx_transfBlock2->findUniform("m4_WorldViewProj"); fx_m4_WorldIT = fx_transfBlock2->findUniform("m4_WorldIT"); } #endif fx_materialBlock = fx_Effect->findCstBuffer("materialBlock"); fx_materialBlock->buildGLBuffer(); // ask nvFx to take care of the UBO creation fx_scaleBias = fx_Effect->findUniform("scaleBias"); if(fx_scaleBias) fx_scaleBias->setValue4f(0, 0, 0, g_scale); // // Now let's load the default resources that the effect might need // nvFX::IResource* pRes = NULL; for(int i=0; pRes=fx_Effect->findResource(i); i++) { const char* name = pRes->annotations()->getAnnotationString("defaultFile"); if(!name) continue; char fname[200]; bool bRes = g_ddsImage.load(name, false); if(!bRes) { sprintf(fname, RESOURCE_DIR "/%s", name); bRes = g_ddsImage.load(fname, false); } if(bRes) { LOGI("Found %s\n", name); bool bRes = false; if(g_ddsImage.get_type() == nv_dds::TextureFlat) { GLuint texID; // Use this free slot (make available for this purpose) glGenTextures(1, (GLuint*)&texID); glBindTexture(GL_TEXTURE_2D, texID); bRes = g_ddsImage.upload_texture2D(); // tell nvFx about this resource texture // This is not the only way to do but it is clean pRes->setGLTexture(texID); fx_resources.push_back(pRes); // keep track of it... could be useful... } glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); if(!bRes) { LOGI("Failed loading %s\n", fname); bRes = false; } glBindTexture(nvFX::RESTEX_2D, 0); } else { LOGI("Failed to find %s\n", fname); } } } return true; }
//------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ bool RendererStandard::initGraphics(int w, int h, int MSAA) { // // some offscreen buffer // fboInitialize(w, h, MSAA); // // Shader compilation // if(!s_shaderGrid.addVertexShaderFromString(g_glslv_grid)) return false; if(!s_shaderGrid.addFragmentShaderFromString(g_glslf_grid)) return false; if (!s_shaderGrid.link()) return false; if(!s_shaderMesh.addVertexShaderFromString(s_glslv_mesh)) return false; if(!s_shaderMesh.addFragmentShaderFromString(s_glslf_mesh)) return false; if(!s_shaderMesh.link()) return false; if(!s_shaderMeshLine.addVertexShaderFromString(s_glslv_mesh)) return false; if(!s_shaderMeshLine.addFragmentShaderFromString(s_glslf_mesh_line)) return false; if(!s_shaderMeshLine.link()) return false; // // Create some UBO for later share their 64 bits // glGenBuffers(1, &g_uboMatrix.Id); g_uboMatrix.Sz = sizeof(MatrixBufferGlobal); glNamedBufferDataEXT(g_uboMatrix.Id, g_uboMatrix.Sz, &g_globalMatrices, GL_STREAM_DRAW); //glBindBufferBase(GL_UNIFORM_BUFFER,UBO_MATRIX, g_uboMatrix.Id); // // Trivial Light info... // glGenBuffers(1, &g_uboLight.Id); g_uboLight.Sz = sizeof(LightBuffer); glNamedBufferDataEXT(g_uboLight.Id, g_uboLight.Sz, &s_light, GL_STATIC_DRAW); //glBindBufferBase(GL_UNIFORM_BUFFER,UBO_LIGHT, g_uboLight.Id); // // Misc OGL setup // glClearColor(0.0f, 0.1f, 0.15f, 1.0f); glGenVertexArrays(1, &s_vao); glBindVertexArray(s_vao); // // Grid // if(!initResourcesGrid()) return false; // // OpenGL timers // m_gltimers.init(10); LOGOK("Initialized renderer %s\n", getName()); m_bValid = true; return true; }
dlAnimTick* dlNewAnimTick( dlAnim *anim ) { dlNodeAnim *node; dlAnimTickOldNode **oldNode; dlVectorKey *vkey; dlQuatKey *qkey; DL_NODE_TYPE count; CALL("%p", anim); if(!anim) { RET("%p", NULL); return( NULL ); } /* Allocate animation handler object */ dlSetAlloc( ALLOC_EVALUATOR ); dlAnimTick *animTick = (dlAnimTick*)dlCalloc( 1, sizeof(dlAnimTick) ); if(!animTick) { RET("%p", NULL); return( NULL ); } /* assign animation */ animTick->anim = anim; animTick->oldTime = 0.0f; /* null */ animTick->oldNode = NULL; node = anim->node; oldNode = &animTick->oldNode; for(; node; node = node->next) { *oldNode = dlCalloc( 1, sizeof(dlAnimTickOldNode) ); if(!*oldNode) { dlFreeAnimTick( animTick ); RET("%p", NULL); return( NULL ); } /* store translations to pointer array */ if(node->translation) { (*oldNode)->translation = dlCalloc( node->num_translation, sizeof(dlVectorKey*) ); if(!(*oldNode)->translation) { dlFreeAnimTick( animTick ); RET("%p", NULL); return( NULL ); } vkey = node->translation; count = 0; for(; vkey; vkey = vkey->next) (*oldNode)->translation[count++] = vkey; } /* store rortations to pointer array */ if(node->rotation) { (*oldNode)->rotation = dlCalloc( node->num_rotation, sizeof(dlQuatKey*) ); if(!(*oldNode)->rotation) { dlFreeAnimTick( animTick ); RET("%p", NULL); return( NULL ); } qkey = node->rotation; count = 0; for(; qkey; qkey = qkey->next) (*oldNode)->rotation[count++] = qkey; } /* store scalings to pointer array */ if(node->scaling) { (*oldNode)->scaling = dlCalloc( node->num_scaling, sizeof(dlVectorKey*) ); if(!(*oldNode)->scaling) { dlFreeAnimTick( animTick ); RET("%p", NULL); return( NULL ); } vkey = node->scaling; count = 0; for(; vkey; vkey = vkey->next) (*oldNode)->scaling[count++] = vkey; } oldNode = &(*oldNode)->next; } /* no animations, pointless */ if(!anim->node) { dlFreeAnimTick( animTick ); RET("%p", NULL); return( NULL ); } LOGOK("NEW"); /* return */ RET("%p", animTick); return( animTick ); }
//----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- bool loadMaterialEffect() { LOGI("Creating Effect (OpenGLUniformUpdateExample)\n"); fx_Effect = nvFX::IContainer::create("OpenGLUniformUpdateExample"); bool bRes = nvFX::loadEffectFromFile(fx_Effect, "OpenGLUniformUpdateExample.glslfx"); if(!bRes) bRes = nvFX::loadEffectFromFile(fx_Effect, SOURCE_DIR "/OpenGLUniformUpdateExample.glslfx"); if(!bRes) { LOGE("Failed\n"); } else { LOGOK("Loaded\n"); // // Let's keep track in interface pointers everything, for purpose of clarity // #ifdef USESVCUI //if(g_pComboTech) g_pComboTech->RemoveAllItems(); #endif for(int t=0; fx_Tech = fx_Effect->findTechnique(t); t++) { fx_Tech->validate(); #ifdef USESVCUI // avoid the tech #0 and #1: only some init for states... //if(t>=TECH_DEFAULT) if(g_pComboTech) g_pComboTech->AddItem(fx_Tech->getName(), t); #endif } #ifdef USESVCUI //if(g_pComboTech) g_pComboTech->SetSelectedByIndex(1); #endif // set the material for the model fx_Tech = fx_Effect->findTechnique("uniformBlock"); fx_TechFloor = fx_Effect->findTechnique(TECH_WIREFLOOR); // execute the pass as a way to initialize some basic GL states nvFX::ITechnique* fx_TechInit = fx_Effect->findTechnique(TECH_INIT); fx_TechInit->getPass(0)->execute(); fx_transfBlock1 = fx_Effect->findCstBuffer("transfBlock1"); fx_transfBlock1->buildGLBuffer(nvFX::ICstBuffer::STATIC_DRAW); // ask nvFx to take care of the UBO creation fx_transfBlock2 = fx_Effect->findCstBuffer("transfBlock2"); fx_transfBlock2->buildGLBuffer(nvFX::ICstBuffer::STATIC_DRAW); // ask nvFx to take care of the UBO creation // example of other way to update: let's track the uniforms inside this buffer. { fx_m4_World = fx_transfBlock2->findUniform("m4_World"); fx_m4_WorldView = fx_transfBlock2->findUniform("m4_WorldView"); fx_m4_WorldViewProj = fx_transfBlock2->findUniform("m4_WorldViewProj"); fx_m4_WorldIT = fx_transfBlock2->findUniform("m4_WorldIT"); } // gathering uniforms fx_u_m4_World = fx_Effect->findUniform("u_m4_World"); fx_u_m4_WorldView = fx_Effect->findUniform("u_m4_WorldView"); fx_u_m4_WorldViewProj = fx_Effect->findUniform("u_m4_WorldViewProj"); fx_u_m4_WorldIT = fx_Effect->findUniform("u_m4_WorldIT"); fx_materialBlock = fx_Effect->findCstBuffer("materialBlock"); fx_materialBlock->buildGLBuffer(nvFX::ICstBuffer::STREAM_DRAW); // ask nvFx to take care of the UBO creation } return true; }