void PezInitialize() { PezConfig cfg = PezGetConfig(); float fovy = 170 * TwoPi / 180; float aspect = (float) cfg.Width / cfg.Height; float zNear = 65, zFar = 90; Globals.Projection = M4MakePerspective(fovy, aspect, zNear, zFar); Globals.Theta = 0; const float MajorRadius = 11.0f, MinorRadius = 1.5f; const float Radius = 5.0f; const int Slices = 60, Stacks = 30; Globals.LavaProgram = LoadProgram("TheGameMaker.VS", 0, "TheGameMaker.FS"); Globals.TorusVao = CreateTorus(MajorRadius, MinorRadius, Slices, Stacks); Globals.ReflectionProgram = LoadProgram("Reflection.VS", 0, "Reflection.FS"); Globals.SphereVao = CreateSphere(Radius, Slices, Stacks); // Load textures Globals.CloudTexture = LoadTexture("cloud.png"); Globals.LavaTexture = LoadTexture("lavatile.png"); glClearColor(0.1, 0.1, 0.1, 0); glBlendFunc(GL_ONE, GL_ONE); }
ezResult ezRendererTestBasics::InitializeSubTest(ezInt32 iIdentifier) { m_iFrame = -1; if (ezGraphicsTest::InitializeSubTest(iIdentifier).Failed()) return EZ_FAILURE; if (iIdentifier == SubTests::ST_ClearScreen) { return SetupRenderer(320, 240); } if (SetupRenderer().Failed()) return EZ_FAILURE; m_hSphere = CreateSphere(3, 1.0f); m_hSphere2 = CreateSphere(1, 0.75f); m_hTorus = CreateTorus(16, 0.5f, 0.75f); m_hLongBox = CreateBox(0.4f, 0.2f, 2.0f); m_hLineBox = CreateLineBox(0.4f, 0.2f, 2.0f); return EZ_SUCCESS; }
void CMassView::AttachedToWindow() // called when added to window { GLfloat light_position[] = {0.0, 3.0, 4.0, 0.0}; // position of light source (at infinity) GLfloat light_colour[] = {1.0, 1.0, 1.0, 0.0}; // colour of light source GLfloat torus_colour[] = {1.0, 1.0, 1.0, 0.0}; // set the torus to green // GLfloat torus_specular_colour[] = {1.0, 1.0, 1.0, 0.0}; // and set shininess up a little bit BGLView::AttachedToWindow(); // call inherited attachment function LockGL(); // make sure this is thread-safe glMatrixMode(GL_PROJECTION); // first set up matrix for the viewing volume glOrtho(-240.0, 240.0, -200.0, 200.0, -1000.0, 1000.0); // set the perspective glMatrixMode(GL_MODELVIEW); // switch to matrix for basis vectors at eye glLoadIdentity(); // load the identity matrix glTranslatef(0, 0, -600.0); // translate the model matrix // glRotatef(30.0, 0.0, 1.0, 0.0); // rotate the torus torusPickListID = glGenLists(2); // set up a total of 1 display list glNewList(torusPickListID, GL_COMPILE); // tell it to save the following calls CreateTorus(100.0, 50.0, GL_SELECT); // set up a list for rendering glEndList(); // close the list glShadeModel(GL_SMOOTH); // use smooth lighting glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, torus_colour); // set the colour of the torus glLightfv(GL_LIGHT0, GL_POSITION, light_position); // specify the position of the light glLightfv(GL_LIGHT0, GL_DIFFUSE, light_colour); // and the colour glEnable(GL_LIGHTING); // switch lighting on glEnable(GL_LIGHT0); // switch light #0 on glEnable(GL_DEPTH_TEST); // make sure depth buffer is switched on glEnable(GL_TEXTURE_2D); // enable 2D textures glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // set the pixel alignment glGenTextures(1, &texName); // create the GL texture "name" glBindTexture(GL_TEXTURE_2D, texName); // mark it as 2D torusDisplayListID = torusPickListID + 1; // we know this name is available glNewList(torusDisplayListID, GL_COMPILE); // tell it to save the following calls CreateTorus(100.0, 50.0, GL_RENDER); // set up a list for rendering glEndList(); // close the list UnlockGL(); // unlock GL FrameResized(Bounds().Width(), Bounds().Height()); } // end of AttachedToWindow()
// creates a set of toroids and loads them into GPU memory //------------------------------------------------------------------------------ void InitTorus (void* dataRef) { int i = 0, j = 0; NPtorusPtr torus = NULL; torus = (NPtorusPtr) malloc (sizeof(NPtorus)); if (torus == NULL) { printf ("\n 4220 error malloc failed cannot write file \n"); return; } // 5x5, 7x5, 12x6, 16x6, 24x8, 32x10, 48x12, 64x16, 96x24, 128x32, 196x48 for ( i = 0; i < kTorusScreenSizeCount; i++) for ( j = 0; j < kTorusThicknessCount; j++) { switch (i) { case 0 : torus->slices = 5; torus->segments = 5; break; case 1 : torus->slices = 5; torus->segments = 7; break; case 2 : torus->slices = 6; torus->segments = 12; break; case 3 : torus->slices = 6; torus->segments = 16; break; case 4 : torus->slices = 8; torus->segments = 24; break; case 5 : torus->slices = 10; torus->segments = 32; break; case 6 : torus->slices = 12; torus->segments = 48; break; case 7 : torus->slices = 16; torus->segments = 64; break; case 8 : torus->slices = 24; torus->segments = 96; break; case 9 : torus->slices = 32; torus->segments = 128; break; case 10 : torus->slices = 48; torus->segments = 196; break; case 11 : torus->slices = 64; torus->segments = 256; break; } torus->thickness = (GLfloat) j * 100.0f / (GLfloat) (kTorusThicknessCount - 1); CreateTorus (torus); // load torus to GPU and deallocate memory // we could save time by allocating enough memory for all torus's at once // or enough for largest torus then de-allocating all at once } }
MyApp::MyApp() { sImage *img; Painter = new sPainter; // perlin texture sInt xs = 256; sInt ys = 256; img = new sImage; img->Init(xs,ys); for(sInt y=0;y<ys;y++) { for(sInt x=0;x<xs;x++) { sF32 p = sPerlin2D(x*(0x100000/xs),y*(0x100000/ys),3,0)*2.0f + sPerlin2D(x*(0x200000/xs),y*(0x200000/ys),7,0)*1.5f + sPerlin2D(x*(0x400000/xs),y*(0x400000/ys),15,0)*1.0f ; sInt c = sInt(128+127*sClamp(p,-1.0f,1.0f)); img->Data[y*xs+x] = c|(c<<8)|(c<<16)|0xff000000; } } Tex = sLoadTexture2D(img,sTEX_ARGB8888); delete img; CreateTorus(); // materials TorusMtrl = new TorusShader; TorusMtrl->Texture[0] = Tex; TorusMtrl->Texture[1] = Tex; TorusMtrl->TBind[1] = sMTB_VS|0; TorusMtrl->Flags = sMTRL_ZON|sMTRL_CULLOFF; TorusMtrl->Prepare(TorusFormat); }
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); }
// Main program int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd = NULL; MSG msg = {0}; WNDCLASSEX wndclassex = {0}; // Init fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.lpszClassName = kClassName; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); RegisterClassEx(&wndclassex); // Register the WNDCLASSEX RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect DWORD winStyleEx = WS_EX_CLIENTEDGE; DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME; // Adjust window rect so it gives us our desired client rect when we // create the window AdjustWindowRectEx(&rect, winStyle, false, winStyleEx); // Create the window hwnd = CreateWindowEx(winStyleEx, // Window extended style kClassName, "www.GameTutorials.com -- DirectX Mesh", winStyle, // Window style CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hinstance, NULL); // Get the client rect and make sure our client is the size we want GetClientRect(hwnd, &rect); assert(rect.right == kWinWid && rect.bottom == kWinHgt); // Init our global 3D object if(g3D->init(hwnd) == false) return EXIT_FAILURE; // There's been an error, lets get out of this joint // Create the sphere if(!CreateSphere(0.5f, D3DCOLOR_XRGB(0,25,225), &gSphere)) return false; // Couldn't create the sphere... Something very bad happened... // Create the torus if(!CreateTorus(0.5f, 1.0f, D3DCOLOR_XRGB(225,25,25), &gTorus)) return false; // Couldn't create the sphere... Something very bad happened... // Set up our projection matrix once because it will not change g3D->setProjMatrix(DEG2RAD(60), (float)kWinWid / (float)kWinHgt, 1.0f, 8192.0f); // We set up our view matrix once because it will never change g3D->setViewMatrix(CPos(0.0f, 0.0f, -5.0f), CPos(0.0f, 0.0f, 0.0f)); ShowWindow(hwnd, ishow); UpdateWindow(hwnd); // While the app is running... while(1) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // If the OS has a message for us { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate()) // Else, if it's time to draw { g3D->begin(); // Begin the frame g3D->clear(); // Clear the back buffer static float angle = 0; D3DXMATRIX wMat; // Set "wMat" to the identity matrix, then set the world matrix // to "wMat" so the sphere is centered in the world D3DXMatrixIdentity(&wMat); g3D->setWorldMatrix(&wMat); g3D->render(gSphere); // Draw the sphere // Set "wMat" to a rotation around the X, Y, and Z axis, then // set the world matrix to "wMat" so the torus rotates around all // three axises D3DXMatrixRotationYawPitchRoll(&wMat, DEG2RAD(angle), DEG2RAD(angle), DEG2RAD(angle)); g3D->setWorldMatrix(&wMat); g3D->render(gTorus); // Draw the torus ++angle; // Update the amount to rotate the torus by g3D->end(); // End the frame } else Sleep(1); // Otherwise, give the OS some time to process other programs } // end of while(1) gSphere->Release(); // Free up the sphere gTorus->Release(); // Free up the torus g3D->deinit(); // Free up the D3D object UnregisterClass(kClassName,hinstance); // Free up WNDCLASSEX return EXIT_SUCCESS; // Application was a success }