void entrypoint( void ) { // full screen if( ChangeDisplaySettings(&screenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) return; ShowCursor( 0 ); // create windows HDC hDC = GetDC( CreateWindow("edit", 0, WS_POPUP|WS_VISIBLE|WS_MAXIMIZE, 0,0,0,0,0,0,0,0) ); // init opengl SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd); wglMakeCurrent(hDC, wglCreateContext(hDC)); // create shader const int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); const int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(GL_FRAGMENT_SHADER); ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s, 1, &fragmentShader, 0); ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,s); ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(p); // run do { glColor3us((unsigned short)GetTickCount(),0,0); glRects(-1,-1,1,1); SwapBuffers(hDC); //wglSwapLayerBuffers( hDC, WGL_SWAP_MAIN_PLANE ); }while ( !GetAsyncKeyState(VK_ESCAPE) ); ExitProcess( 0 ); }
void intro_do( long time ) { //--- update parameters ----------------------------------------- const float t = 0.001f*(float)time; // camera position fparams[ 0] = 2.0f; fparams[ 1] = 0.0f; fparams[ 2] = 2.0f; // camera target fparams[ 4] = 0.0f; fparams[ 5] = 0.0f; fparams[ 6] = 0.0f; // sphere fparams[ 8] = 0.0f; fparams[ 9] = 0.0f; fparams[10] = 0.0f; fparams[11] = 1.0f; /* // Gen renderbuffer glGenRenderbuffersEXT(1, &frontb); // Bind renderbuffer glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, frontb); // Init as a depth buffer glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, XRES, YRES); // Attach to the FBO for depth glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, frontb); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);*/ //--- render ----------------------------------------- static float reso[4]; reso[0] = XRES; reso[1] = YRES; reso[2] = XRES; reso[3] = YRES; oglUseProgram( pid ); glUniform1fARB( oglGetUniformLocation( pid, "iGlobalTime" ), t ); oglUniform4fv( oglGetUniformLocation( pid, "iResolution" ), 1, reso ); oglUniform4fv( oglGetUniformLocation( pid, "fpar" ), 4, fparams ); glRects( -1, -1, 1, 1 ); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); }
int entrypoint(void) { //if (ChangeDisplaySettings(&screenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) return -1; ShowCursor(0); HDC hDC = GetDC(CreateWindowExA(WS_EX_APPWINDOW, "static", 0, WINDOWFLAGS, 0, 0, XRES, YRES, 0, 0, 0, 0)); SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd); wglMakeCurrent(hDC, wglCreateContext(hDC)); const int t = ((PFNGLCREATESHADERPROGRAMEXTPROC)wglGetProcAddress("glCreateShaderProgramEXT"))(GL_FRAGMENT_SHADER, fragmentShader); ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(t); do { static MSG dummyMessage; PeekMessageA(&dummyMessage, 0, 0, 0, 1); // Remove all Windows messages to prevent "Program not responding" dialog. glColor3us((unsigned short)GetTickCount(), 0, 0); glRects(-1, -1, 1, 1); SwapBuffers(hDC); //wglSwapLayerBuffers( hDC, WGL_SWAP_MAIN_PLANE ); } while (!GetAsyncKeyState(VK_ESCAPE)); ExitProcess(0); return 1; }
///////////////////////////////////////////////////////// // Render // void GEMglRects :: render(GemState *state) { glRects (x1, y1, x2, y2); }
int main(int argc, char *argv[]) { char *shader = "mandelbrot (copy).frag"; if (argc > 1) { shader = argv[1]; } glfwInit(); /*int major, minor, rev; glfwGetGLVersion(&major, &minor, &rev); printf("using openGL %d.%d.%d\n", major, minor, rev);*/ GLFWvidmode mode; glfwGetDesktopMode( &mode ); window_width = mode.Width; window_height = mode.Height; printf("%dx%d\n", window_width, window_height); //glfwOpenWindow(window_width, window_height, 5, 6, 5, 0, 8, 0, GLFW_FULLSCREEN); glfwOpenWindow(mode.Width, mode.Height, mode.RedBits, mode.GreenBits, mode.BlueBits, 0, 0, 0, GLFW_FULLSCREEN); // setup opengl perspective stuff. glMatrixMode(GL_PROJECTION); float aspect_ratio = ((float)window_height) / window_width; glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50); glDisable( GL_DEPTH_TEST ); glTranslatef(0.0, 0.0, -1.00); glClearColor(0.0, 1.0, 1.0, 0.0); // will create and set shader program. GLhandleARB program = SetupFragmentShader(shader); // get the location of all the uniforms prepareUniforms(program); // must call use program before setting uniforms values glUseProgram(program); uint param_i = 0; parameters[WIDTH] = createParameter1i(program, "wW", window_width ); parameters[HEIGHT] = createParameter1i(program, "wH", window_height ); parameters[TIME] = createParameter1f(program, "time", 0.0 ); parameters[MINX] = createParameter1f(program, "minx", -2.0); parameters[MINY] = createParameter1f(program, "miny", 1.0); parameters[DELTA] = createParameter1f(program, "deltax", 3.0); parameters[THETA] = createParameter1f(program, "theta", 1.0); //synchParameters ( parameters ); /*for ( param_i = 0; param_i < NPARAMS; param_i++ ) { Parameter *param = ¶meters[param_i]; switch ( param->utype ) { case INT: glUniform1i(param->loc, param->u.ival); break; case FLOAT: glUniform1f(param->loc, param->u.fval); break; default: break; } }*/ // set the dimension, must be after program is used. //glUniform1i(wWLoc, window_width ); //glUniform1i(wHLoc, window_height ); printInstructions(); double frame_start = glfwGetTime(); double frame_time = 0.0; double temp_time = 0.0; //bool need_draw = true; bool need_view_synch = true; int mouseWheel = 0; int mouseWheelDelta = 0; double fIterations = (double)(iterations); int mouseX, mouseY, curMouseX, curMouseY; mouseX = mouseY = 0; bool joystickPresent = glfwGetJoystickParam( GLFW_JOYSTICK_1, GLFW_PRESENT ); if(joystickPresent) { printf("Joystick present. Can be used to navigate in addition to mouse.\n"); }else { printf("no joystick present.\n"); } // set up fonts FTGLfont *font = NULL; //font = ftglCreateBufferFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf"); font = ftglCreatePixmapFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf"); if( !font ) { printf("failed to load font."); } ftglSetFontFaceSize(font, 50, 50); //printf("%d\n", ftglGetFontError(font)); //ftglSetFontCharMap(font, ft_encoding_unicode); //printf("%d\n", ftglGetFontError(font)); bool show_fps = true; char text[256]; // keep on rendering the frame until escape is pressed. while(glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS) { temp_time = glfwGetTime(); frame_time = temp_time - frame_start; frame_start = temp_time; // process input if(glfwGetKey('E') == GLFW_PRESS) { printf("minx: %.31f\n", minx); printf("miny: %.31f\n", miny); printf("deltax: %.31f\n",deltax); printf("deltay: %.31f\n",deltay); printf("====================================\n"); }else if(glfwGetKey('P') == GLFW_PRESS) { saveFrameBuffer(); }else if(glfwGetKey('R') == GLFW_PRESS) { minx = -2.0; miny = -1.0; deltax = 3.0; deltay = 2.0; need_view_synch = true; }else if(glfwGetKey('F') == GLFW_PRESS) { show_fps=~show_fps; //printf("fps display toggled to: %d\n", show_fps); } else if(glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS) { fIterations += 5*(60*frame_time); iterations = floor(fIterations); need_view_synch = true; }else if(glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS) { if(iterations>1) { fIterations -= 5*(60*frame_time); } iterations = floor(fIterations); need_view_synch = true; } // zooming using the arrow keys if(glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS) { zoomIn(2.0*zoom_factor*(frame_time*60.0)); need_view_synch = true; }else if(glfwGetKey(GLFW_KEY_DOWN) == GLFW_PRESS) { zoomIn(-2.0*zoom_factor*(frame_time*60.0)); need_view_synch = true; } //changing the julia set constant if(glfwGetKey('W') == GLFW_PRESS) { realc += .0025*frame_time*60.0; need_view_synch = true; }else if(glfwGetKey('S') == GLFW_PRESS) { realc -= .0025*frame_time*60.0; need_view_synch = true; } if(glfwGetKey('A') == GLFW_PRESS) { imagc += .0025*frame_time*60.0; need_view_synch = true; }else if(glfwGetKey('D') == GLFW_PRESS) { imagc -= .0025*frame_time*60.0; need_view_synch = true; } // changing the area visible (panning left and right) glfwGetMousePos(&curMouseX, &curMouseY); int mouseDeltax = curMouseX - mouseX; int mouseDeltay = curMouseY - mouseY; if(mouseDeltax||mouseDeltay)//if one of them is different { //printf("delta coordinates: (%d, %d)\n", mouseDeltax, mouseDeltay); mouseX=curMouseX; mouseY=curMouseY; //printf("new mouse coordinates: (%d, %d)\n", x, y); pan(zoom_factor*deltax*mouseDeltax/20.0, -1*zoom_factor*deltay*mouseDeltay/20.0); need_view_synch = true; } if(joystickPresent) { float pos[4]; glfwGetJoystickPos(GLFW_JOYSTICK_1, pos, 4); //the above function gives us bad results for my particular joystick :(, must fiddle around with numbers to get correct answer. float xJoyPos = pos[0];//(pos[0]*32767)/128.0 - 1.0; float yJoyPos = pos[1];//(pos[1]*32767)/128.0 + 1.0; float zJoyPos = -1*pos[2];//-1*((pos[2]*32767)/128.0 - 1.0); float rJoyPos = pos[3]; float temp = 0.0; //printf("%f\n", xJoyPos); if(xJoyPos>.02||xJoyPos<-.02) { temp = zoom_factor*xJoyPos*deltax*1.0*frame_time*60.0; pan(temp, 0); need_view_synch = true; } if(yJoyPos>.02||yJoyPos<-.02) { temp = zoom_factor*yJoyPos*deltay*1.0*frame_time*60.0; pan(0, temp); need_view_synch = true; } if(zJoyPos>.04||zJoyPos<-.04) { zoomIn(zJoyPos*scroll_zoom_factor*(frame_time*10.0)); need_view_synch = true; } if(rJoyPos>.02 || rJoyPos<-.02) { rotate( rJoyPos/5.0*frame_time ); need_view_synch = true; } unsigned char buttons[5]; glfwGetJoystickButtons(GLFW_JOYSTICK_1, buttons, 4); if(buttons[0]==GLFW_PRESS) { saveFrameBuffer(); } if(buttons[1]==GLFW_PRESS) { rotate( 0.35*frame_time ); need_view_synch = true; }else if(buttons[2]==GLFW_PRESS) { rotate( -0.35*frame_time ); need_view_synch = true; } //printf("x:%f, y:%f\n", xJoyPos, yJoyPos); } // zooming using the mouse scroll mouseWheelDelta = glfwGetMouseWheel() - mouseWheel; mouseWheel = mouseWheel+mouseWheelDelta; if(mouseWheelDelta != 0) { zoomIn(mouseWheelDelta*scroll_zoom_factor*(frame_time*60.0)); need_view_synch = true; } //printf("mouse wheel: %d\n", mouseWheelDelta); if(need_view_synch) { synchVariableUniforms(); need_view_synch = false; } // update the time inside the shader for cool animations. parameters[2].val.fval = frame_start; parameters[2].needs_update = true; synchParameters ( parameters ); //glUniform1f(timeLoc, frame_start); // write fps and iterations to a string. sprintf(text, "FPS: %4.1d; ITER: %d; ZOOMX: %-10.1f", (int)(floor(1.0/frame_time)), iterations, (3.0/deltax)); //sprintf("fps %f\n", 1.0/frame_time); // clear the buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //since all calculations are being done in the fragment shader, all we draw is a surface. glUseProgram(program); glRects(-1, -1, 1, 1); // render font on top of this? if(show_fps != 0){ glUseProgram(0); glColor3f(0.0f, 0.0f, 0.0f); ftglRenderFont(font, text, FTGL_RENDER_ALL); glUseProgram(program); } // note: swap buffers also updates the input events for glfw glfwSwapBuffers(); } // clean up the font and exit. ftglDestroyFont(font); return 1; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; // create window ZeroMemory( &wc, sizeof(WNDCLASS) ); wc.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.hInstance = hPrevInstance; wc.lpszClassName = "iq"; if( !RegisterClass(&wc) ) return( 0 ); const int dws = WS_VISIBLE | WS_CAPTION | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_SYSMENU; AdjustWindowRect( &rec, dws, 0 ); HWND hWnd = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, wc.lpszClassName, "accio", dws, (GetSystemMetrics(SM_CXSCREEN)-rec.right+rec.left)>>1, (GetSystemMetrics(SM_CYSCREEN)-rec.bottom+rec.top)>>1, rec.right-rec.left, rec.bottom-rec.top, 0, 0, hPrevInstance, 0 ); if( !hWnd ) return( 0 ); HDC hDC=GetDC(hWnd); if( !hDC ) return( 0 ); // init opengl int pf = ChoosePixelFormat(hDC,&pfd); if( !pf ) return( 0 ); if( !SetPixelFormat(hDC,pf,&pfd) ) return( 0 ); HGLRC hRC = wglCreateContext(hDC); if( !hRC ) return( 0 ); if( !wglMakeCurrent(hDC,hRC) ) return( 0 ); // init extensions for( int i=0; i<(sizeof(funcPtrs)/sizeof(void*)); i++ ) { funcPtrs[i] = wglGetProcAddress( funcNames[i] ); if( !funcPtrs[i] ) return 0; } // create shader int p = oglCreateProgram(); // int v = oglCreateShader(GL_VERTEX_SHADER); // oglShaderSource(v, 1, &vertexShader, 0); // oglCompileShader(v); // oglAttachShader(p,v); int f = oglCreateShader(GL_FRAGMENT_SHADER); oglShaderSource(f, 1, &fragmentShader, 0); oglCompileShader(f); oglAttachShader(p,f); oglLinkProgram(p); oglUseProgram(p); ShowCursor(0); // run /* do { glColor3f(t,sinf(.25f*t),0.0f); glRects(-1,-1,1,1); SwapBuffers(hDC); //wglSwapLayerBuffers( hDC, WGL_SWAP_MAIN_PLANE ); }while ( !GetAsyncKeyState(VK_ESCAPE) ); */ int done = 0; while( !done ) { const float t = .0025f*(float)(GetTickCount()&65535); MSG msg; while( PeekMessage(&msg,0,0,0,PM_REMOVE) ) { if( msg.message==WM_QUIT ) done=1; TranslateMessage( &msg ); DispatchMessage( &msg ); } glColor3f(t,sinf(.25f*t),0.0f); glRects(-1,-1,1,1); SwapBuffers( hDC ); } ExitProcess( 0 ); return 0; }
int main(int argc, char* argv[]){ if(argc<=1){ std::cerr<<"Usage: "<<argv[0]<<" <vertexShader.glsl> [-w <width>] [-h <height>] [-c{0,1,2,3,4} index]\n"; return 1; } char* wArg = getCmdOption(argv, argv+argc, "-w"); if(wArg){ width = std::atoi(wArg); } char* hArg = getCmdOption(argv, argv+argc, "-h"); if(hArg){ height = std::atoi(hArg); } char* c0Arg = getCmdOption(argv, argv+argc, "-c0"); if(c0Arg){ iChannel0Index=std::atoi(c0Arg); } char* c1Arg = getCmdOption(argv, argv+argc, "-c1"); if(c1Arg){ iChannel1Index=std::atoi(c1Arg); } char* c2Arg = getCmdOption(argv, argv+argc, "-c2"); if(c2Arg){ iChannel2Index=std::atoi(c2Arg); } char* c3Arg = getCmdOption(argv, argv+argc, "-c3"); if(c3Arg){ iChannel3Index=std::atoi(c3Arg); } SDL_Init(SDL_INIT_VIDEO); SDL_Window* window; SDL_GLContext maincontext; SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); window = SDL_CreateWindow("Shadertoy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL ); maincontext = SDL_GL_CreateContext(window); glViewport(0,0,width,height); SDL_GL_SetSwapInterval(1); std::cout<<glGetString(GL_VERSION)<<"\n"; std::ifstream shaderInStream(argv[1]); std::stringstream buffer; buffer << uniforms << shaderInStream.rdbuf()<<mainMethod; std::string contents = buffer.str(); GLuint programIndex = compile(contents); glUseProgram(programIndex); iResolutionLocation = glGetUniformLocation(programIndex, "iResolution"); iGlobalTimeLocation = glGetUniformLocation(programIndex, "iGlobalTime"); iMouseLocation = glGetUniformLocation(programIndex, "iMouse"); iChannel0Location = glGetUniformLocation(programIndex, "iChannel0"); iChannel1Location = glGetUniformLocation(programIndex, "iChannel1"); iChannel2Location = glGetUniformLocation(programIndex, "iChannel2"); iChannel3Location = glGetUniformLocation(programIndex, "iChannel3"); iChannelResolutionLocation = glGetUniformLocation(programIndex, "iChannelResolution"); loadTextures(); setStaticUniforms(); unsigned int lastTime=SDL_GetTicks(); unsigned int deltaCounter=0; unsigned int fps=0; unsigned int secondCounter=0; bool hasQuit=false; while(!hasQuit){ unsigned int now=SDL_GetTicks(); deltaCounter+=now-lastTime; secondCounter+=now-lastTime; lastTime=now; while(deltaCounter>17){ deltaCounter-=17; } SDL_Event e; while(SDL_PollEvent(&e)){ switch(e.type){ case SDL_QUIT: hasQuit=true; break; case SDL_MOUSEBUTTONDOWN: mouseXPos=mouseXClick=e.button.x; mouseYPos=mouseYClick=e.button.y; break; case SDL_MOUSEMOTION: if(e.motion.state&SDL_BUTTON(1)){ mouseXPos=e.motion.x; mouseYPos=e.motion.y; } } } setDynamicUniforms(); glRects(-1,-1,1,1); SDL_GL_SwapWindow(window); fps++; if(secondCounter>1000){ std::cout<<fps<<" FPS\n"; fps=0; secondCounter-=1000; } // SDL_Delay(10); } SDL_DestroyWindow(window); SDL_Quit(); }
M(void, glRects, jshort x1, jshort y1, jshort x2, jshort y2) { glRects(x1, y1, x2, y2); }