void starttest() { glswInit(); #ifdef __MAC_NA glswSetPath("../../test/", ".glsl"); #else glswSetPath("../test/", ".glsl"); #endif glswAddDirectiveToken("GL2", "#version 120"); glswAddDirectiveToken("GL3", "#version 130"); glswAddDirectiveToken("GL4", "#version 150"); }
void Initialize() { RenderContext& rc = GlobalRenderContext; glswInit(); glswSetPath("../", ".glsl"); glswAddDirectiveToken("GL3", "#version 130"); rc.VertexBuffer = CreateVertexBuffer(); rc.IndexBuffer = CreateIndexBuffer(); #if defined(__APPLE__) rc.ToonHandle = BuildProgram("Toon.Vertex.GL2", "Toon.Fragment.GL2"); #else rc.ToonHandle = BuildProgram("Toon.Vertex.GL3", "Toon.Fragment.GL3"); #endif rc.ToonUniforms.Projection = glGetUniformLocation(rc.ToonHandle, "Projection"); rc.ToonUniforms.Modelview = glGetUniformLocation(rc.ToonHandle, "Modelview"); rc.ToonUniforms.NormalMatrix = glGetUniformLocation(rc.ToonHandle, "NormalMatrix"); rc.ToonUniforms.LightPosition = glGetUniformLocation(rc.ToonHandle, "LightPosition"); rc.ToonUniforms.AmbientMaterial = glGetUniformLocation(rc.ToonHandle, "AmbientMaterial"); rc.ToonUniforms.DiffuseMaterial = glGetUniformLocation(rc.ToonHandle, "DiffuseMaterial"); rc.ToonUniforms.SpecularMaterial = glGetUniformLocation(rc.ToonHandle, "SpecularMaterial"); rc.ToonUniforms.Shininess = glGetUniformLocation(rc.ToonHandle, "Shininess"); glEnable(GL_DEPTH_TEST); const float S = 0.46f; const float H = S * ViewportHeight / ViewportWidth; rc.Projection = mat4::Frustum(-S, S, -H, H, 4, 10); }
void LoadEffect() { RenderContext& rc = GlobalRenderContext; glswInit(); glswSetPath("../demo/", ".glsl"); glswAddDirectiveToken("GL3", "#version 150"); const char* vsKey = "PixelLighting.Vertex.GL3"; const char* fsKey = "PixelLighting.Fragment.GL3"; rc.EffectHandle = BuildProgram(vsKey, fsKey); rc.EffectUniforms.Projection = glGetUniformLocation(rc.EffectHandle, "Projection"); rc.EffectUniforms.Modelview = glGetUniformLocation(rc.EffectHandle, "Modelview"); rc.EffectUniforms.NormalMatrix = glGetUniformLocation(rc.EffectHandle, "NormalMatrix"); rc.Theta = 0; glUseProgram(rc.EffectHandle); GLuint LightPosition = glGetUniformLocation(rc.EffectHandle, "LightPosition"); GLuint AmbientMaterial = glGetUniformLocation(rc.EffectHandle, "AmbientMaterial"); GLuint DiffuseMaterial = glGetUniformLocation(rc.EffectHandle, "DiffuseMaterial"); GLuint SpecularMaterial = glGetUniformLocation(rc.EffectHandle, "SpecularMaterial"); GLuint Shininess = glGetUniformLocation(rc.EffectHandle, "Shininess"); glUniform3f(DiffuseMaterial, 0.75, 0.75, 0.5); glUniform3f(AmbientMaterial, 0.04f, 0.04f, 0.04f); glUniform3f(SpecularMaterial, 0.5, 0.5, 0.5); glUniform1f(Shininess, 50); glUniform3f(LightPosition, 0.25, 0.25, 1); }
void Setup(CPlatform * const pPlatform) { unsigned int got = 0; const char *pVertStr[2] = {0,0}, *pFragStr[2] = {0,0}; float *pFrustumVertices = 0; float colour[] = {1.0f,0.0f,0.0f, 0.0f,1.0f,0.0f, 0.0f,0.0f,1.0f}; int nFacets = 0; glswInit(); glswSetPath( "../resources/", ".glsl" ); WRender::SetClearColour(0,0,0,0); WRender::EnableCulling(true); // - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - glswGetShadersAlt( "shaders.Shared+shaders.Skymap.Vertex", pVertStr, 2); glswGetShadersAlt( "shaders.Shared+shaders.Skymap.Fragment", pFragStr, 2); CShader vertexShader(CShader::VERT, pVertStr, 2); CShader fragmentShader(CShader::FRAG, pFragStr, 2); //setup the shaders program_cube.Initialise(); program_cube.AddShader(&vertexShader); program_cube.AddShader(&fragmentShader); program_cube.Link(); program_cube.Start(); program_cube.SetTextureUnit("cube_map",0); // - - - - - - - - - - //set up shapes box = CreateShapeCube(); sphere = CreateShapeSphereNormals(); //sphere = CreateShapeSphere(); //sphere = CreateShapeCylinder(); //sphere = CreateShapeCone(); //sphere = CreateShapeTorus(); // - - - - - - - - - - // set up cubemap CreateCubeMapTextures(); //ubo for cameras etc ubo = WRender::CreateBuffer(WRender::UNIFORM, WRender::DYNAMIC, sizeof(Transforms), &transform); WRender::BindBufferToIndex(WRender::UNIFORM, ubo, 1); Transform::CreateProjectionMatrix(transforms.proj, -0.1f, 0.1f, -0.1f, 0.1f, 0.1f, 50.0f); }
static void LoadEffect() { const char* vsSource, * fsSource; GLuint vsHandle, fsHandle; GLint compileSuccess, linkSuccess; GLchar compilerSpew[256]; GLuint programHandle; glswInit(); glswSetPath("../demo/", ".glsl"); glswAddDirectiveToken("GL3", "#version 130"); vsSource = glswGetShader("Simple.Vertex.Textured." PEZ_GL_VERSION_TOKEN); fsSource = glswGetShader("Simple.Fragment.Textured." PEZ_GL_VERSION_TOKEN); PezCheckCondition(vsSource, "Can't find vertex shader.\n"); PezCheckCondition(fsSource, "Can't find fragment shader.\n"); vsHandle = glCreateShader(GL_VERTEX_SHADER); fsHandle = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(vsHandle, 1, &vsSource, 0); glCompileShader(vsHandle); glGetShaderiv(vsHandle, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(vsHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(compileSuccess, compilerSpew); glShaderSource(fsHandle, 1, &fsSource, 0); glCompileShader(fsHandle); glGetShaderiv(fsHandle, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(fsHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(compileSuccess, compilerSpew); programHandle = glCreateProgram(); glAttachShader(programHandle, vsHandle); glAttachShader(programHandle, fsHandle); glBindAttribLocation(programHandle, PositionSlot, "Position"); glBindAttribLocation(programHandle, TexCoordSlot, "InCoord"); glLinkProgram(programHandle); glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess); glGetProgramInfoLog(programHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(linkSuccess, compilerSpew); glUseProgram(programHandle); }
void Scene::initShader() { const char *kShadersPath = "data/shader/"; /// GLSW, shader file manager glswInit(); glswSetPath( kShadersPath, ".glsl"); glswAddDirectiveToken("*", "#version 330 core"); m_passthroughPS.generate(); m_passthroughPS.addShader( engine::VERTEX_SHADER, "PassThrough.Vertex"); m_passthroughPS.addShader( engine::FRAGMENT_SHADER, "PassThrough.Fragment"); m_passthroughPS.link(); m_screenmappingPS.generate(); m_screenmappingPS.addShader( engine::VERTEX_SHADER, "ScreenMapping.Vertex"); m_screenmappingPS.addShader( engine::FRAGMENT_SHADER, "ScreenMapping.Fragment"); m_screenmappingPS.link(); glswShutdown(); }
void Setup(CPlatform * const pPlatform) { //for the shapes unsigned int nTorusFloats; float *pTorusVertices = 0; float *pPlaneVertices = 0; float colour[] = {1.0f,0.0f,0.0f, 0.0f,1.0f,0.0f, 0.0f,0.0f,1.0f}; unsigned int torusSegments = 36, torusTubeSegments = 36; //for the shaders const char *pVertStr[3] = {0,0,0}, *pFragStr = 0; const char *pFragTexStr = 0, *pVertTexStr = 0; //fbo stuff WRender::Texture::SDescriptor descDepth = {WRender::Texture::TEX_2D, WRender::Texture::DEPTH_COMPONENT, TEX_DIMENSIONS, TEX_DIMENSIONS, 0, 0, WRender::Texture::DONT_GEN_MIPMAP}; WRender::Texture::SParam param[] ={ { WRender::Texture::MIN_FILTER, WRender::Texture::LINEAR}, { WRender::Texture::MAG_FILTER, WRender::Texture::LINEAR}, { WRender::Texture::WRAP_S, WRender::Texture::CLAMP_TO_EDGE}, { WRender::Texture::WRAP_T, WRender::Texture::CLAMP_TO_EDGE}, }; glswInit(); glswSetPath("../resources/", ".glsl"); WRender::SetClearColour(0,0,0,0); WRender::EnableCulling(true); // - - - - - - - - - - //setup the shaders // - - - - - - - - - - //normal shader glswGetShadersAlt("shaders.Version+shaders.Shared+shaders.SingleShadow.Vertex", pVertStr, 3); pFragStr = glswGetShaders("shaders.Version+shaders.SingleShadow.Fragment"); CShader vertexShader(CShader::VERT, pVertStr, 3); CShader fragmentShader(CShader::FRAG, &pFragStr, 1); program.Initialise(); program.AddShader(&vertexShader); program.AddShader(&fragmentShader); program.Link(); uShadowMtx = program.GetUniformLocation("shadowMtx"); program.Start(); program.SetTextureUnit("shadowMap", 0); program.Stop(); //debug shader for textures in screen space pVertTexStr = glswGetShaders("shaders.Version+shaders.Dbg.ScreenSpaceTexture.Vertex"); pFragTexStr = glswGetShaders("shaders.Version+shaders.Dbg.ScreenSpaceTexture.Fragment"); CShader vTexShader(CShader::VERT, &pVertTexStr, 1); CShader fTexShader(CShader::FRAG, &pFragTexStr, 1); textureShader.Initialise(); textureShader.AddShader(&vTexShader); textureShader.AddShader(&fTexShader); textureShader.Link(); textureShader.Start(); textureShader.SetTextureUnit("texture",0); textureShader.Stop(); // - - - - - - - - - - //set up shapes // - - - - - - - - - - //shared colours abColour = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(colour), colour); //Torus nTorusVertices = 2*torusTubeSegments*torusSegments*3; nTorusFloats = nTorusVertices*3; pTorusVertices = new float[nTorusFloats]; CreateTorus(pTorusVertices, torusSegments, 3.0f, torusTubeSegments, 1.0f); vaoTorus = WRender::CreateVertexArrayObject(); abTorus = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(float)*nTorusFloats, pTorusVertices); WRender::BindVertexArrayObject(vaoTorus); WRender::VertexAttribute vaTorus[2] = { {abTorus, 0, 3, WRender::FLOAT, 0, sizeof(float)*3, 0, 0}, {abColour, 1, 3, WRender::FLOAT, 0, sizeof(float)*9, sizeof(float)*6, 1}, }; WRender::SetAttributeFormat( vaTorus, 2, 0); delete[] pTorusVertices; //Plane pPlaneVertices = new float[4*3*3]; CreatePlane(pPlaneVertices, 20.0f, 20.0f); vaoPlane = WRender::CreateVertexArrayObject(); abPlane = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(float)*4*3*3, pPlaneVertices); WRender::BindVertexArrayObject(vaoPlane); WRender::VertexAttribute vaFrustum[2] = { {abPlane, 0, 3, WRender::FLOAT, 0, sizeof(float)*3, 0, 0}, {abColour, 1, 3, WRender::FLOAT, 0, sizeof(float)*9, sizeof(float)*3, 1}, }; WRender::SetAttributeFormat( vaFrustum, 2, 0); delete[] pPlaneVertices; //for screen aligned texture sqVao = WRender::CreateVertexArrayObject(); sqEab = WRender::CreateBuffer(WRender::ELEMENTS, WRender::STATIC, sizeof(sqIndices), sqIndices); sqAb = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(sqVertices), sqVertices); WRender::BindVertexArrayObject(sqVao); WRender::BindBuffer(WRender::ELEMENTS, sqEab); WRender::VertexAttribute sqVa[2] = { {sqAb, 0, 3, WRender::FLOAT, 0, sizeof(float)*5, 0, 0}, //vertices {sqAb, 1, 2, WRender::FLOAT, 0, sizeof(float)*5, sizeof(float)*3, 0}, //texture coordinates }; WRender::SetAttributeFormat( sqVa, 2, 0); WRender::UnbindVertexArrayObject(); // - - - - - - - - - - //ubo for cameras etc // - - - - - - - - - - ubo = WRender::CreateBuffer(WRender::UNIFORM, WRender::DYNAMIC, sizeof(Transforms), &transform); WRender::BindBufferToIndex(WRender::UNIFORM, ubo, 1); //create the texture and FBO for rendering to when drawing //during shadow stage WRender::CreateBaseTexture(depthTexture, descDepth); WRender::SetTextureParams(depthTexture,param,4); WRender::CreateFrameBuffer(fbo); WRender::AddTextureRenderBuffer(fbo, depthTexture, WRender::ATT_DEPTH, 0); WRender::BindFrameBuffer(WRender::FrameBuffer::DRAW, fbo); WRender::SetDrawBuffer(WRender::DB_NONE); WRender::CheckFrameBuffer(fbo); //set the projection matrix Transform::CreateProjectionMatrix(transforms.proj, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 50.0f); WRender::BindTexture(depthTexture,WRender::Texture::UNIT_0); }
static void SetupShaders(void){ int got = 0; SShader vtxTxShdr, frgTxShdr; SShader vtxClrShdr, frgClrShdr; SShader vtxLightShdr, frgLightShdr; const char *pDVertStr[3] = {0,0,0}, *pDFragStr[3] = {0,0,0}; glswInit(); glswSetPath("./id1/shaders/", ".glsl"); glswAddDirectiveToken("Shared", HASH_DEFINE_VALUE(TRANSFORM_UBO_BINDING)); glswAddDirectiveToken("Shared", HASH_DEFINE_VALUE(LIGHT_UBO_BINDING)); glswAddDirectiveToken("Shared", HASH_DEFINE_VALUE(MAX_DIRECTION_LIGHTS)); glswAddDirectiveToken("Shared", HASH_DEFINE_VALUE(MAX_POINT_LIGHTS)); glswAddDirectiveToken("Shared", HASH_DEFINE_VALUE(MAX_SPOT_LIGHTS)); glswAddDirectiveToken("Shared", HASH_DEFINE_VALUE(TEXT_TEX_UNIT)); glswAddDirectiveToken("Vertex", HASH_DEFINE_VALUE(POSITION_LOCATION)); glswAddDirectiveToken("Vertex", HASH_DEFINE_VALUE(COLOUR_LOCATION)); glswAddDirectiveToken("Vertex", HASH_DEFINE_VALUE(NORMAL_LOCATION)); glswAddDirectiveToken("Vertex", HASH_DEFINE_VALUE(JOINT_WEIGHT_LOCATION)); glswAddDirectiveToken("Vertex", HASH_DEFINE_VALUE(JOINT_INDEX_LOCATION)); glswAddDirectiveToken("Vertex", HASH_DEFINE_VALUE(UV_LOCATION0)); glswAddDirectiveToken("Vertex", HASH_DEFINE_VALUE(UV_LOCATION1)); glswAddDirectiveToken("Vertex", HASH_DEFINE_VALUE(TEXT_LOCATION)); //glswAddDirectiveToken("Shared", HASH_DEFINE_VALUE(SKINNING_TEXTURE_BINDING)); //glswAddDirectiveToken("Shared", HASH_DEFINE_VALUE(WEIGHTS_PER_VERTEX)); //glswAddDirectiveToken("Shared", HASH_DEFINE(TEXTURE_BUFFER_SKINNING)); //glswAddDirectiveToken("Shared", HASH_DEFINE_VALUE(RESERVED_JOINTS)); //shader (TEXTURED) got = glswGetShadersAlt("shaders.Header.Vertex+shaders.Shared+shaders.SimpleVertexTextured", pDVertStr, 3); CreateShader(&vtxTxShdr, VERT, pDVertStr, got); got = glswGetShadersAlt("shaders.Header.Fragment+shaders.Shared+shaders.SimpleFragmentTextured", pDFragStr, 3); CreateShader(&frgTxShdr, FRAG, pDFragStr, got); CreateShaderProgram(&texture_shader); AddShaderToProgram(&texture_shader,&vtxTxShdr); AddShaderToProgram(&texture_shader,&frgTxShdr); LinkShaderProgram(&texture_shader); //shader (COLOUR) got = glswGetShadersAlt("shaders.Header.Vertex+shaders.Shared+shaders.SimpleVertexColoured", pDVertStr, 3); CreateShader(&vtxClrShdr, VERT, pDVertStr, got); got = glswGetShadersAlt("shaders.Header.Fragment+shaders.Shared+shaders.SimpleFragmentColoured", pDFragStr, 3); CreateShader(&frgClrShdr, FRAG, pDFragStr, got); CreateShaderProgram(&colour_shader); AddShaderToProgram(&colour_shader,&vtxClrShdr); AddShaderToProgram(&colour_shader,&frgClrShdr); LinkShaderProgram(&colour_shader); //shader (LIGHTMAP) got = glswGetShadersAlt("shaders.Header.Vertex+shaders.Shared+shaders.SimpleVertexLightmap", pDVertStr, 3); CreateShader(&vtxLightShdr, VERT, pDVertStr, got); got = glswGetShadersAlt("shaders.Header.Fragment+shaders.Shared+shaders.SimpleFragmentLightmap", pDFragStr, 3); CreateShader(&frgLightShdr, FRAG, pDFragStr, got); CreateShaderProgram(&light_map_shader); AddShaderToProgram(&light_map_shader, &vtxLightShdr); AddShaderToProgram(&light_map_shader, &frgLightShdr); LinkShaderProgram(&light_map_shader); //this will delete them after we have deleted the program associated with them DeleteShader(&vtxTxShdr); DeleteShader(&frgTxShdr); DeleteShader(&vtxClrShdr); DeleteShader(&frgClrShdr); DeleteShader(&vtxLightShdr); DeleteShader(&frgLightShdr); }
void Setup(CPlatform * const pPlatform) { float vertices[3] = {0,0,0}; WRender::RenderBuffer::SDescriptor rboDesc = {WRender::RenderBuffer::RGBA8, WIDTH_HEIGHT, WIDTH_HEIGHT, 0}; const char* pV = 0; const char* pF = 0; glswInit(); glswSetPath("../resources/", ".glsl"); //setup the textures WRender::Texture::SDescriptor desc = {WRender::Texture::TEX_2D, WRender::Texture::RGB8, WIDTH_HEIGHT, WIDTH_HEIGHT, 0, 0, WRender::Texture::DONT_GEN_MIPMAP}; WRender::Texture::SDescriptor descDepth = {WRender::Texture::TEX_2D, WRender::Texture::DEPTH_COMPONENT, WIDTH_HEIGHT, WIDTH_HEIGHT, 0, 0, WRender::Texture::DONT_GEN_MIPMAP}; WRender::Texture::SParam param[] ={ { WRender::Texture::MIN_FILTER, WRender::Texture::LINEAR}, { WRender::Texture::MAG_FILTER, WRender::Texture::LINEAR}, { WRender::Texture::WRAP_S, WRender::Texture::REPEAT}, { WRender::Texture::WRAP_T, WRender::Texture::REPEAT}, }; WRender::CreateBaseTexture(texR, desc); WRender::CreateBaseTexture(texG, desc); WRender::CreateBaseTexture(texB, desc); WRender::CreateBaseTexture(texD, descDepth); WRender::SetTextureParams(texR,param,4); WRender::SetTextureParams(texG,param,4); WRender::SetTextureParams(texB,param,4); WRender::SetTextureParams(texD,param,4); //Setup Render Buffer WRender::CreateRenderBuffer(rbo,rboDesc); //setup Frame Buffer WRender::CreateFrameBuffer(fbo); WRender::AddTextureRenderBuffer(fbo, texR, WRender::ATT_CLR0, 0); //WRender::AddTextureRenderBuffer(fbo, texG, WRender::ATT_CLR1, 0); WRender::AddTextureRenderBuffer(fbo, texB, WRender::ATT_CLR2, 0); WRender::AddRenderBuffer(fbo, rbo, WRender::ATT_CLR3); WRender::AddTextureRenderBuffer(fbo, texD, WRender::ATT_DEPTH, 0); //FOR DEPTH INDEX DOESN'T MATTER WRender::CheckFrameBuffer(fbo); //setup blitting FBO WRender::CreateFrameBuffer(fboForBlitting); WRender::AddTextureRenderBuffer(fboForBlitting, texG, WRender::ATT_CLR0, 0); WRender::CheckFrameBuffer(fboForBlitting); //setup shaders pV = glswGetShaders("shaders.Version+shaders.MRT.Stage1.Vertex"); pF = glswGetShaders("shaders.Version+shaders.MRT_2.Stage1.Fragment"); CShader vertexShader(CShader::VERT, &pV, 1); CShader fragmentShader(CShader::FRAG, &pF, 1); pV = glswGetShaders("shaders.Version+shaders.MRT.Stage2.Vertex"); pF = glswGetShaders("shaders.Version+shaders.MRT.Stage2.Fragment"); CShader vertexShaderStage2(CShader::VERT, &pV, 1); CShader fragmentShaderStage2(CShader::FRAG, &pF, 1); program[0].Initialise(); program[0].AddShader(&vertexShader); program[0].AddShader(&fragmentShader); program[0].Link(); program[1].Initialise(); program[1].AddShader(&vertexShaderStage2); program[1].AddShader(&fragmentShaderStage2); program[1].Link(); //quickly set the uniforms program[1].Start(); program[1].SetTextureUnit("mrt",0); program[1].Stop(); // setup vertex buffers etc //axis vao = WRender::CreateVertexArrayObject(); ab = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(vertices), vertices); WRender::BindVertexArrayObject(vao); WRender::VertexAttribute va[1] = { {ab, 0, 3, WRender::FLOAT, 0, sizeof(float)*3, 0, 0}, //vertices }; WRender::SetAttributeFormat( va, 1, 0); WRender::UnbindVertexArrayObject();//needed //square sqVao = WRender::CreateVertexArrayObject(); sqEab = WRender::CreateBuffer(WRender::ELEMENTS, WRender::STATIC, sizeof(sqIndices), sqIndices); sqAb = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(sqVertices), sqVertices); WRender::BindVertexArrayObject(sqVao); WRender::BindBuffer(WRender::ELEMENTS, sqEab); WRender::VertexAttribute sqVa[2] = { {sqAb, 0, 3, WRender::FLOAT, 0, sizeof(float)*5, 0, 0}, //vertices {sqAb, 1, 2, WRender::FLOAT, 0, sizeof(float)*5, sizeof(float)*3, 0}, //texture coordinates }; WRender::SetAttributeFormat( sqVa, 2, 0); WRender::UnbindVertexArrayObject(); //variables for camera float latitude = 0.0f, longitude = 0.0f; CVec3df cameraPosition(0, 0.0f, -3.0f); Transforms transforms; Transform::CreateProjectionMatrix(transforms.proj, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 10.0f); //for rendering first time aorund WRender::EnableDepthTest(true); }
int main( int argc, char *argv[] ) { printf("Game Data Dir: %s\n", getResourceDir().c_str() ); // Initialize GLSW glswInit(); glswSetPath( getResourceDir().c_str(), ".glsl" ); // Initialize SDL if (SDL_Init( SDL_INIT_NOPARACHUTE | SDL_INIT_VIDEO ) < 0 ) { printf("Unable to init SDL: %s\n", SDL_GetError() ); exit(1); } // cheezy check for fullscreen Uint32 mode_flags = SDL_OPENGL; for (int i=1; i < argc; i++) { if (!_stricmp( argv[i], "-fullscreen")) { mode_flags |= SDL_FULLSCREEN; } } // TODO: flag to control this SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 ); if (SDL_SetVideoMode( 800, 600, 32, mode_flags ) == 0 ) { printf( "Unable to set video mode: %s\n", SDL_GetError() ) ; exit(1); } SDL_WM_SetCaption( "LD{{number}} - {{Project}}", NULL ); TemplateGame *game = new TemplateGame(); game->init(); // init graphics // FIXME: screen size glViewport( 0, 0, 800, 600 ); //=====[ Main loop ]====== bool done = false; Uint32 ticks = SDL_GetTicks(), ticks_elapsed, sim_ticks = 0; while(!done) { SDL_Event event; while (SDL_PollEvent( &event ) ) { switch (event.type ) { case SDL_KEYDOWN: switch( event.key.keysym.sym ) { case SDLK_ESCAPE: done = true; break; default: game->keypress( event.key.keysym.sym ); break; } break; case SDL_MOUSEMOTION: game->mouseMotion( event.motion.xrel, event.motion.yrel ); break; case SDL_MOUSEBUTTONDOWN: game->mouseButton( event.button ); break; case SDL_MOUSEBUTTONUP: game->mouseButton( event.button ); break; case SDL_QUIT: done = true; break; default: break; } } // Do continuous keys Uint8 *keyState = SDL_GetKeyState( NULL ); // convert to btn mask Uint32 btnMask = 0; btnMask |= (keyState[SDLK_LEFT]||keyState[SDLK_a])?BTN_LEFT:0; btnMask |= (keyState[SDLK_RIGHT]||keyState[SDLK_d])?BTN_RIGHT:0; btnMask |= (keyState[SDLK_UP]||keyState[SDLK_w])?BTN_UP:0; btnMask |= (keyState[SDLK_DOWN]||keyState[SDLK_s])?BTN_DOWN:0; btnMask |= (keyState[SDLK_z]||keyState[SDLK_COMMA])?BTN_A:0; btnMask |= (keyState[SDLK_x]||keyState[SDLK_PERIOD])?BTN_B:0; game->updateButtons( btnMask ); // Timing ticks_elapsed = SDL_GetTicks() - ticks; ticks += ticks_elapsed; // fixed sim update sim_ticks += ticks_elapsed; while (sim_ticks > STEPTIME) { sim_ticks -= STEPTIME; //printf("update sim_ticks %d ticks_elapsed %d\n", sim_ticks, ticks_elapsed ); game->updateSim( (float)STEPTIME / 1000.0f ); } // redraw as fast as possible float dtRaw = (float)(ticks_elapsed) / 1000.0f; game->updateFree( dtRaw ); game->redraw(); SDL_GL_SwapBuffers(); // Call this once a frame if using tweakables //ReloadChangedTweakableValues(); } // Shut down game game->shutdown(); delete game; // Restore SDL stuff SDL_ShowCursor( true ); SDL_WM_GrabInput( SDL_GRAB_OFF ); // Shutdown glsw glswShutdown(); return 0; }
void setup() { // 3. Define and compile vertex and fragment shaders GLuint vs; GLuint fs; GLint compileSuccess, linkSuccess; GLchar compilerSpew[256]; glswInit(); glswSetPath("../shader/", ".glsl"); glswAddDirectiveToken("GL3", "#version 150"); const char *vss = glswGetShader("test.Vertex.GL3"); const char *fss = glswGetShader("test.Fragment.GL3"); PezCheckCondition((void *)vss, "Can't find vertex shader.\n"); PezCheckCondition((void *)fss, "Can't find fragment shader.\n"); vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, &vss, NULL); glCompileShader(vs); glGetShaderiv(vs, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(vs, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition((void*)compileSuccess, compilerSpew); fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, &fss, NULL); glCompileShader(fs); glGetShaderiv(fs, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(fs, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition((void*)compileSuccess, compilerSpew); shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vs); glAttachShader(shaderProgram, fs); glBindFragDataLocation(shaderProgram, 0, "fragColour"); glLinkProgram(shaderProgram); glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkSuccess); glGetProgramInfoLog(shaderProgram, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition((void*)linkSuccess, compilerSpew); positionUniform = glGetUniformLocation(shaderProgram, "p"); colourAttribute = glGetAttribLocation(shaderProgram, "colour"); positionAttribute = glGetAttribLocation(shaderProgram, "position"); glDeleteShader(vs); glDeleteShader(fs); GLfloat vertexData[]= { -0.5,-0.5,0.0,1.0, 1.0,0.0,0.0,1.0, -0.5, 0.5,0.0,1.0, 0.0,1.0,0.0,1.0, 0.5, 0.5,0.0,1.0, 0.0,0.0,1.0,1.0, 0.5,-0.5,0.0,1.0, 1.0,1.0,1.0,1.0}; glGenVertexArrays(1, &vertexArrayObject); glBindVertexArray(vertexArrayObject); glGenBuffers(1, &vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, 4*8*sizeof(GLfloat), vertexData, GL_STATIC_DRAW); glEnableVertexAttribArray((GLuint)positionAttribute); glEnableVertexAttribArray((GLuint)colourAttribute ); glVertexAttribPointer((GLuint)positionAttribute, 4, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), 0); glVertexAttribPointer((GLuint)colourAttribute , 4, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (char*)0+4*sizeof(GLfloat)); }
void Setup(CPlatform * const pPlatform) { unsigned int got = 0; const char *pVertStr[3] = {0,0,0}, *pFragStr = 0; unsigned int nSphereFloats; float *pSphereVertices = 0; unsigned int iterations = 7; int nFacets = 0; glswInit(); glswSetPath("../resources/", ".glsl"); WRender::SetClearColour(0,0,0,0); WRender::EnableCulling(true); got = glswGetShadersAlt("shaders.Version+shaders.Shared+shaders.FlatShading.Vertex", pVertStr, 3); pFragStr = glswGetShaders("shaders.Version+shaders.FlatShading.Fragment"); CShader vertexShader(CShader::VERT, pVertStr, got); CShader fragmentShader(CShader::FRAG, &pFragStr, 1); pVertStr[0] = glswGetShaders("shaders.Version+shaders.Gamma.Vertex"); pFragStr = glswGetShaders("shaders.Version+shaders.Gamma.Fragment"); CShader vertexShaderStage2(CShader::VERT, &pVertStr[0], 1); CShader fragmentShaderStage2(CShader::FRAG, &pFragStr, 1); // - - - - - - - - - - //setup the shaders program[0].Initialise(); program[0].AddShader(&vertexShader); program[0].AddShader(&fragmentShader); program[0].Link(); program[1].Initialise(); program[1].AddShader(&vertexShaderStage2); program[1].AddShader(&fragmentShaderStage2); program[1].Link(); program[1].Start(); program[1].SetTextureUnit("src_image",0); program[1].Stop(); // - - - - - - - - - - //setup the textures WRender::Texture::SDescriptor desc = {WRender::Texture::TEX_2D, WRender::Texture::RGB8, WIDTH_HEIGHT, WIDTH_HEIGHT, 0, 0, WRender::Texture::DONT_GEN_MIPMAP}; WRender::Texture::SParam param[] ={ { WRender::Texture::MIN_FILTER, WRender::Texture::LINEAR}, { WRender::Texture::MAG_FILTER, WRender::Texture::LINEAR}, { WRender::Texture::WRAP_S, WRender::Texture::REPEAT}, { WRender::Texture::WRAP_T, WRender::Texture::REPEAT}, }; WRender::CreateBaseTexture(texR, desc); WRender::SetTextureParams(texR,param,4); //setup Frame Buffer WRender::CreateFrameBuffer(fbo); WRender::AddTextureRenderBuffer(fbo, texR, WRender::ATT_CLR0, 0); WRender::CheckFrameBuffer(fbo); // - - - - - - - - - - //set up shapes //SPHERE nSphereVertices = unsigned int(powl(4.0,long double(iterations)))*8*3; nSphereFloats = nSphereVertices*3; pSphereVertices = new float[nSphereFloats]; nFacets = CreateNSphere(pSphereVertices, iterations); vaoSphere = WRender::CreateVertexArrayObject(); abSphere = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(float)*nSphereFloats, pSphereVertices); WRender::BindVertexArrayObject(vaoSphere); WRender::VertexAttribute vaSphere[1] = { {abSphere, 0, 3, WRender::FLOAT, 0, sizeof(float)*3, 0, 0} }; WRender::SetAttributeFormat( vaSphere, 1, 0); delete[] pSphereVertices; //square sqVao = WRender::CreateVertexArrayObject(); sqEab = WRender::CreateBuffer(WRender::ELEMENTS, WRender::STATIC, sizeof(sqIndices), sqIndices); sqAb = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(sqVertices), sqVertices); WRender::BindVertexArrayObject(sqVao); WRender::BindBuffer(WRender::ELEMENTS, sqEab); WRender::VertexAttribute sqVa[2] = { {sqAb, 0, 3, WRender::FLOAT, 0, sizeof(float)*5, 0, 0}, //vertices {sqAb, 1, 2, WRender::FLOAT, 0, sizeof(float)*5, sizeof(float)*3, 0}, //texture coordinates }; WRender::SetAttributeFormat( sqVa, 2, 0); WRender::UnbindVertexArrayObject(); //ubo for cameras etc ubo = WRender::CreateBuffer(WRender::UNIFORM, WRender::DYNAMIC, sizeof(Transforms), &transform); WRender::BindBufferToIndex(WRender::UNIFORM, ubo, 1); Transform::CreateProjectionMatrix(transforms.proj, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 50.0f); }