int main(int argc, char *argv[]) { int fsaa, accel; int value; int i; SDL_DisplayMode mode; Uint32 then, now; int status; shader_data *data; /* Initialize parameters */ fsaa = 0; accel = 0; /* Initialize test framework */ state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); if (!state) { return 1; } for (i = 1; i < argc;) { int consumed; consumed = SDLTest_CommonArg(state, i); if (consumed == 0) { if (SDL_strcasecmp(argv[i], "--fsaa") == 0) { ++fsaa; consumed = 1; } else if (SDL_strcasecmp(argv[i], "--accel") == 0) { ++accel; consumed = 1; } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) { i++; if (!argv[i]) { consumed = -1; } else { depth = SDL_atoi(argv[i]); consumed = 1; } } else { consumed = -1; } } if (consumed < 0) { SDL_Log ("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0], SDLTest_CommonUsage(state)); quit(1); } i += consumed; } /* Set OpenGL parameters */ state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS; state->gl_red_size = 5; state->gl_green_size = 5; state->gl_blue_size = 5; state->gl_depth_size = depth; state->gl_major_version = 2; state->gl_minor_version = 0; state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES; if (fsaa) { state->gl_multisamplebuffers=1; state->gl_multisamplesamples=fsaa; } if (accel) { state->gl_accelerated=1; } if (!SDLTest_CommonInit(state)) { quit(2); return 0; } context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(context)); if (context == NULL) { SDL_Log("Out of memory!\n"); quit(2); } /* Create OpenGL ES contexts */ for (i = 0; i < state->num_windows; i++) { context[i] = SDL_GL_CreateContext(state->windows[i]); if (!context[i]) { SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError()); quit(2); } } /* Important: call this *after* creating the context */ if (LoadContext(&ctx) < 0) { SDL_Log("Could not load GLES2 functions\n"); quit(2); return 0; } /* from here on out, it should be ok to call gles 2.0 routines */ if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) { SDL_GL_SetSwapInterval(1); } else { SDL_GL_SetSwapInterval(0); } SDL_Log("first call address is %llx data is %lx\n",(long long)(&glGetString),GL_VENDOR); const char *test = ctx.glGetString(GL_VENDOR); if (test) { SDL_Log("we got %s\n",test); } else { SDL_Log("we got a null.\n"); quit(2); } SDL_Log("try again call address is %lx\n",(long long)(&glGetString)); SDL_Log("first ctx address is %llx data is %lx ind %llx\n",(long long)(ctx.glGetString),GL_VENDOR,((long long *)(ctx.glGetString))[0] ); {const char *test = glGetString(GL_VENDOR); if (test) { SDL_Log("we got %s\n",test); } else { SDL_Log("we got a null.\n"); quit(2); } } SDL_GetCurrentDisplayMode(0, &mode); SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format)); SDL_Log("\n"); SDL_Log("Vendor : %s\n", glGetString(GL_VENDOR)); SDL_Log("Renderer : %s\n", glGetString(GL_RENDERER)); SDL_Log("Version : %s\n", glGetString(GL_VERSION)); SDL_Log("Extensions : %s\n", glGetString(GL_EXTENSIONS)); SDL_Log("\n"); status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value); if (!status) { SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value); } else { SDL_Log( "Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError()); } status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value); if (!status) { SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value); } else { SDL_Log( "Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError()); } status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value); if (!status) { SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value); } else { SDL_Log( "Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError()); } status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value); if (!status) { SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value); } else { SDL_Log( "Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError()); } if (fsaa) { status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value); if (!status) { SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value); } else { SDL_Log( "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n", SDL_GetError()); } status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value); if (!status) { SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, value); } else { SDL_Log( "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n", SDL_GetError()); } } if (accel) { status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value); if (!status) { SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value); } else { SDL_Log( "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n", SDL_GetError()); } } datas = (shader_data *)SDL_calloc(state->num_windows, sizeof(shader_data)); /* Set rendering settings for each context */ for (i = 0; i < state->num_windows; ++i) { int w, h; status = SDL_GL_MakeCurrent(state->windows[i], context[i]); if (status) { SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError()); /* Continue for next window */ continue; } SDL_GL_GetDrawableSize(state->windows[i], &w, &h); glViewport(0, 0, w, h); data = &datas[i]; data->angle_x = 0; data->angle_y = 0; data->angle_z = 0; /* Shader Initialization */ process_shader(&data->shader_vert, _shader_vert_src, GL_VERTEX_SHADER); process_shader(&data->shader_frag, _shader_frag_src, GL_FRAGMENT_SHADER); /* Create shader_program (ready to attach shaders) */ data->shader_program = GL_CHECK(glCreateProgram()); /* Attach shaders and link shader_program */ GL_CHECK(glAttachShader(data->shader_program, data->shader_vert)); GL_CHECK(glAttachShader(data->shader_program, data->shader_frag)); GL_CHECK(glLinkProgram(data->shader_program)); /* Get attribute locations of non-fixed attributes like color and texture coordinates. */ data->attr_position = GL_CHECK(glGetAttribLocation(data->shader_program, "av4position")); data->attr_color = GL_CHECK(glGetAttribLocation(data->shader_program, "av3color")); /* Get uniform locations */ data->attr_mvp = GL_CHECK(glGetUniformLocation(data->shader_program, "mvp")); GL_CHECK(glUseProgram(data->shader_program)); /* Enable attributes for position, color and texture coordinates etc. */ GL_CHECK(glEnableVertexAttribArray(data->attr_position)); GL_CHECK(glEnableVertexAttribArray(data->attr_color)); /* Populate attributes for position, color and texture coordinates etc. */ GL_CHECK(glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, _vertices)); GL_CHECK(glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, _colors)); GL_CHECK(glEnable(GL_CULL_FACE)); GL_CHECK(glEnable(GL_DEPTH_TEST)); } /* Main render loop */ frames = 0; then = SDL_GetTicks(); done = 0; #ifdef __EMSCRIPTEN__ emscripten_set_main_loop(loop, 0, 1); #else while (!done) { loop(); } #endif /* Print out some timing information */ now = SDL_GetTicks(); if (now > then) { SDL_Log("%2.2f frames per second\n", ((double) frames * 1000) / (now - then)); } #if !defined(__ANDROID__) && !defined(__NACL__) quit(0); #endif return 0; }
int main(int argc, char **argv) { EGLDisplay sEGLDisplay; EGLContext sEGLContext; EGLSurface sEGLSurface; /* EGL Configuration */ EGLint aEGLAttributes[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_DEPTH_SIZE, 16, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint aEGLContextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLConfig aEGLConfigs[1]; EGLint cEGLConfigs; #ifdef _WIN32 MSG sMessage; #else XSetWindowAttributes win_attrs; int attrs[64], idx = 0, num_config = 0; int major, minor; Colormap colormap; XVisualInfo *pVisual; XEvent e; #endif GLint iLocPosition = 0; GLint iLocColour, iLocTexCoord, iLocNormal, iLocMVP; GLint iLocXangle, iLocYangle, iLocZangle; GLint iLocAspect, iLocLightPos, iLocSampler, iLocSampler2; GLuint uiProgram, uiFragShader, uiVertShader; GLenum myTex, myTex2; int bDone = 0; const unsigned int uiWidth = 640; const unsigned int uiHeight = 480; int iXangle = 0, iYangle = 0, iZangle = 0; float aTBNmatrix1[9], aTBNmatrix2[9]; float aLightPos[] = { 0.0f, 0.0f, -1.0f }; // Light is nearest camera. unsigned char *myPixels = calloc(1, 128*128*4); // Holds texture data. unsigned char *myPixels2 = calloc(1, 128*128*4); // Holds texture data. float aRotate[16], aModelView[16], aPerspective[16], aMVP[16]; int i; /* EGL Init */ #ifdef _WIN32 hDisplay = EGL_DEFAULT_DISPLAY; #else hDisplay = XOpenDisplay(NULL); if (!hDisplay) { printf("Could not open display\n"); exit(-1); } #endif sEGLDisplay = EGL_CHECK(eglGetDisplay(hDisplay)); EGL_CHECK(eglInitialize(sEGLDisplay, NULL, NULL)); EGL_CHECK(eglChooseConfig(sEGLDisplay, aEGLAttributes, aEGLConfigs, 1, &cEGLConfigs)); if (cEGLConfigs == 0) { printf("No EGL configurations were returned.\n"); exit(-1); } #ifdef _WIN32 hWindow = create_window(uiWidth, uiHeight); #else hWindow = create_window("OpenGL ES 2.0 Example on a Linux Desktop", uiWidth, uiHeight, hDisplay, sEGLDisplay, aEGLConfigs[0], &colormap, &pVisual); #endif sEGLSurface = EGL_CHECK(eglCreateWindowSurface(sEGLDisplay, aEGLConfigs[0], (EGLNativeWindowType)hWindow, NULL)); if (sEGLSurface == EGL_NO_SURFACE) { printf("Failed to create EGL surface.\n"); exit(-1); } sEGLContext = EGL_CHECK(eglCreateContext(sEGLDisplay, aEGLConfigs[0], EGL_NO_CONTEXT, aEGLContextAttributes)); if (sEGLContext == EGL_NO_CONTEXT) { printf("Failed to create EGL context.\n"); exit(-1); } EGL_CHECK(eglMakeCurrent(sEGLDisplay, sEGLSurface, sEGLSurface, sEGLContext)); /* Shader Initialisation */ process_shader(&uiVertShader, "shader.vert", GL_VERTEX_SHADER); process_shader(&uiFragShader, "shader.frag", GL_FRAGMENT_SHADER); /* Create uiProgram (ready to attach shaders) */ uiProgram = GL_CHECK(glCreateProgram()); /* Attach shaders and link uiProgram */ GL_CHECK(glAttachShader(uiProgram, uiVertShader)); GL_CHECK(glAttachShader(uiProgram, uiFragShader)); GL_CHECK(glLinkProgram(uiProgram)); /* Get attribute locations of non-fixed attributes like colour and texture coordinates. */ iLocPosition = GL_CHECK(glGetAttribLocation(uiProgram, "av4position")); iLocColour = GL_CHECK(glGetAttribLocation(uiProgram, "av3colour")); #ifdef DEBUG printf("iLocPosition = %i\n", iLocPosition); printf("iLocColour = %i\n", iLocColour); #endif /* Get uniform locations */ iLocMVP = GL_CHECK(glGetUniformLocation(uiProgram, "mvp")); #ifdef DEBUG printf("iLocMVP = %i\n", iLocMVP); #endif GL_CHECK(glUseProgram(uiProgram)); /* Enable attributes for position, colour and texture coordinates etc. */ GL_CHECK(glEnableVertexAttribArray(iLocPosition)); GL_CHECK(glEnableVertexAttribArray(iLocColour)); /* Populate attributes for position, colour and texture coordinates etc. */ GL_CHECK(glVertexAttribPointer(iLocPosition, 3, GL_FLOAT, GL_FALSE, 0, aVertices)); GL_CHECK(glVertexAttribPointer(iLocColour, 3, GL_FLOAT, GL_FALSE, 0, aColours)); GL_CHECK(glEnable(GL_CULL_FACE)); GL_CHECK(glEnable(GL_DEPTH_TEST)); #ifndef _WIN32 XSelectInput(hDisplay, hWindow, KeyPressMask | ExposureMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | VisibilityChangeMask | ButtonPressMask | ButtonReleaseMask | StructureNotifyMask); #endif /* Enter event loop */ while (!bDone) { #ifdef _WIN32 if(PeekMessage(&sMessage, NULL, 0, 0, PM_REMOVE)) { if(sMessage.message == WM_QUIT) { bDone = 1; } else { TranslateMessage(&sMessage); DispatchMessage(&sMessage); } } #else while (XPending(hDisplay) > 0) { XNextEvent(hDisplay, &e); if (e.type == ButtonPress) { bDone = 1; } } #endif /* * Do some rotation with Euler angles. It is not a fixed axis as * quaterions would be, but the effect is cool. */ rotate_matrix(iXangle, 1.0, 0.0, 0.0, aModelView); rotate_matrix(iYangle, 0.0, 1.0, 0.0, aRotate); multiply_matrix(aRotate, aModelView, aModelView); rotate_matrix(iZangle, 0.0, 1.0, 0.0, aRotate); multiply_matrix(aRotate, aModelView, aModelView); /* Pull the camera back from the cube */ aModelView[14] -= 2.5; perspective_matrix(45.0, (double)uiWidth/(double)uiHeight, 0.01, 100.0, aPerspective); multiply_matrix(aPerspective, aModelView, aMVP); GL_CHECK(glUniformMatrix4fv(iLocMVP, 1, GL_FALSE, aMVP)); iXangle += 3; iYangle += 2; iZangle += 1; if(iXangle >= 360) iXangle -= 360; if(iXangle < 0) iXangle += 360; if(iYangle >= 360) iYangle -= 360; if(iYangle < 0) iYangle += 360; if(iZangle >= 360) iZangle -= 360; if(iZangle < 0) iZangle += 360; GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)); GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 36)); if (!eglSwapBuffers(sEGLDisplay, sEGLSurface)) { printf("Failed to swap buffers.\n"); } #ifdef _WIN32 Sleep(20); #else usleep(20000); #endif } /* Cleanup shaders */ GL_CHECK(glUseProgram(0)); GL_CHECK(glDeleteShader(uiVertShader)); GL_CHECK(glDeleteShader(uiFragShader)); GL_CHECK(glDeleteProgram(uiProgram)); /* EGL clean up */ EGL_CHECK(eglMakeCurrent(sEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)); EGL_CHECK(eglDestroySurface(sEGLDisplay, sEGLSurface)); EGL_CHECK(eglDestroyContext(sEGLDisplay, sEGLContext)); EGL_CHECK(eglTerminate(sEGLDisplay)); #ifndef _WIN32 /* X windows clean up */ XDestroyWindow(hDisplay, hWindow); XFreeColormap(hDisplay, colormap); XFree(pVisual); XCloseDisplay(hDisplay); #endif return 0; }