manager::manager(shader_program_ptr shader) { // Reset errors, so we can track errors that happened here. glGetError(); GLint blend_src_mode; GLint blend_dst_mode; // Save current blend mode glGetIntegerv(GL_BLEND_SRC, &blend_src_mode); glGetIntegerv(GL_BLEND_DST, &blend_dst_mode); blend_stack.push(blend_mode(blend_src_mode, blend_dst_mode, glIsEnabled(GL_BLEND) != 0)); GLint atu; glGetIntegerv(GL_ACTIVE_TEXTURE, &atu); active_texture_unit.push(atu); if(shader == NULL || active_shader_program == shader) { return; } if(active_shader_program != shader) { shader_stack.push(active_shader_program); active_shader_program = shader; } ASSERT_LOG(active_shader_program != NULL, "Active shader was NULL"); check_gl_errors(); active_shader_program->prepare_draw(); GLenum err = glGetError(); ASSERT_LOG(err == GL_NONE, "Error in shader code: " << shader->name() << " : 0x" << std::hex << err << ": " << gl_error_to_string(err)); }
SDL_GLContext* glcontext_create(SDL_Window* window) { glcontext_init(); SDL_GLContext* gl = SDL_GL_CreateContext(window); SDL_GL_SetSwapInterval(0); check_gl_errors("GL Context creation"); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); glClearColor(1, 1, 1, 1); SDL_LogInfo( SDL_LOG_CATEGORY_RENDER, "GL version: %s - GLSL version: %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION)); return gl; }
static void handle_events(void) { int running = 1; int status = MAIN_MENU; unsigned prev_ticks; while (running) { SDL_Event event; int delay; int response; prev_ticks = SDL_GetTicks(); while (SDL_PollEvent(&event)) { if (status == GAMEPLAY) { response = handle_gameplay_events(event); if (response == MENU) { status = MAIN_MENU; } } else if (status == MAIN_MENU) { response = handle_mainmenu_events(event); if (response == QUIT) { running = 0; } else if (response == NEWGAME) { status = GAMEPLAY; } } } if (status == GAMEPLAY) { handle_gameplay_updates(); } else if (status == MAIN_MENU) { handle_mainmenu_updates(); } delay = 1000/FPS - (SDL_GetTicks() - prev_ticks); check_gl_errors(); if (delay > 0) SDL_Delay(delay); } }
Game* game_create(int width, int height) { srand(time(NULL)); Game* game = malloc(sizeof(*game)); game->run = 0; game->window = window_create(width, height); game->gl = glcontext_create(game->window); game->timer = timer_create(); glGenVertexArrays(1, &game->vao_id); glBindVertexArray(game->vao_id); check_gl_errors("VAO creation"); game->scene = scene_create(); return game; }
void c_RendererViewer::display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); //glClearDepth(1.0f); glDepthFunc(GL_LEQUAL); glColor3f(1.0f, 0.f, 0.f); //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //glShadeModel(GL_SMOOTH); draw(""); glutSwapBuffers(); update_fps(); check_gl_errors(); return; }
ross_internal void render(Input input, Input last_input, GLuint shader_program, ProgramBuffers program_buffers) { ross_persist u64 vertex_count = 0; glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shader_program); if(input.mouse.left) { GLfloat vertex[2] = { 0.0f, 0.0f }; GLdouble mouse_obj_x; GLdouble mouse_obj_y; GLdouble mouse_obj_z; GLdouble model[16]; GLdouble proj[16]; GLint view[4]; glGetDoublev(GL_MODELVIEW_MATRIX, model); glGetDoublev(GL_PROJECTION_MATRIX, proj); glGetIntegerv(GL_VIEWPORT, view); GLdouble win_x, win_y, win_z; win_x = (r64)input.mouse.x; win_y = (r64)view[3] - (r64)input.mouse.y; glReadPixels(win_x, win_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &win_z); gluUnProject(win_x, win_y, win_z, model, proj, view, &mouse_obj_x, &mouse_obj_y, &mouse_obj_z); #if 0 GLint buffer_size; glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &buffer_size); GLfloat *buffer = (GLfloat *)ross_alloc(buffer_size * sizeof(GLfloat)); glGetBufferSubData(GL_ARRAY_BUFFER, 0, buffer_size, buffer); b8 repeated_data = 0; for(u64 i = 0; i < buffer_size; ++i) { if(buffer[i] == mouse_obj_x && buffer[i+1] == mouse_obj_y) { printf("%lu\n", i); repeated_data = 1; break; } } ross_free(buffer); if(!repeated_data) { vertex[0] = mouse_obj_x; vertex[1] = mouse_obj_y; vertex_count++; glBindBuffer(GL_ARRAY_BUFFER, program_buffers.object_buffer); glBufferSubData(GL_ARRAY_BUFFER, vertex_count * sizeof(vertex), sizeof(vertex), vertex); //printf("mousex: %.02f, mousey: %.02f \n", vertices[0], vertices[1]); } #endif vertex[0] = mouse_obj_x; vertex[1] = mouse_obj_y; glBindBuffer(GL_ARRAY_BUFFER, program_buffers.object_buffer); glBufferSubData(GL_ARRAY_BUFFER, vertex_count * sizeof(vertex), sizeof(vertex), vertex); vertex_count++; } //printf("%lu\n", vertex_count); glPointSize(10); GLint position_loc = glGetAttribLocation(shader_program, "vs_position"); glEnableVertexAttribArray(position_loc); glVertexAttribPointer(position_loc, 2, GL_FLOAT, GL_FALSE, 0, 0); check_gl_errors(); glDrawArrays(GL_POINTS, 0, vertex_count); glDisableVertexAttribArray(position_loc); }
int main(int argc, char **argv) { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { APP_LOG << "Failed to initialize SDL: " << SDL_GetError() << '\n'; return EXIT_FAILURE; } int window_width = 640; int window_height = 480; SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); SDL_Window *window = SDL_CreateWindow( "Sample Application", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_width, window_height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); if (window == NULL) { APP_LOG << "Failed to create a window: " << SDL_GetError() << '\n'; SDL_Quit(); return EXIT_FAILURE; } SDL_GLContext gl_context = SDL_GL_CreateContext(window); SDL_GL_SetSwapInterval(1); // Wait for vertical refresh glewExperimental = true; GLenum glew_error = glewInit(); if (glew_error != GLEW_OK) { APP_LOG << "Failed to load OpenGL functions: " << glewGetErrorString(glew_error) << '\n'; SDL_GL_DeleteContext(gl_context); SDL_DestroyWindow(window); SDL_Quit(); return EXIT_FAILURE; } print_debug_info(); if(!load_game(window_width, window_height)) { APP_LOG << "Failed to load content" << '\n'; SDL_GL_DeleteContext(gl_context); SDL_DestroyWindow(window); SDL_Quit(); return EXIT_FAILURE; } init_game(); gui::init(window_width, window_height); uint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); glViewport(0, 0, window_width, window_height); double frame_time = 1.0 / 60.0; bool running = true; while (running) { double frame_begin = get_elapsed_time(); handle_events(running, window); gui::update(frame_time); update_game(frame_time); render_game(frame_time); SDL_GL_SwapWindow(window); frame_time = get_elapsed_time() - frame_begin; if (check_gl_errors()) running = false; if (frame_time < 1.0 / 60.0) { sleep(1.0 / 60.0 - frame_time); frame_time = get_elapsed_time() - frame_begin; } } gui::dispose(); SDL_GL_DeleteContext(gl_context); SDL_DestroyWindow(window); SDL_Quit(); return EXIT_SUCCESS; }
void wesTriangleRateBenchmark(AppState *as) { int startTime, endTime; size_t totalTris=0, totalVerts=0; float elapsedTimeMS, trisPerSecond, vertsPerSecond; Vertex2D *baseVerts; Color3D *baseColors; Vertex3D *baseNormals; Vertex2D *baseTCs; GLuint trianglesListIndx = 0; int nVertsPerAxis; int nFrames=0; Vertex2D *dispatchVerts=NULL; Color3D *dispatchColors=NULL; Vertex3D *dispatchNormals=NULL; Vertex2D *dispatchTCs=NULL; GLuint *dispatchIndices=NULL; int dispatchVertexCount, dispatchTriangles; int screenWidth = as->imgWidth; GLfloat r=screenWidth; int screenHeight = as->imgHeight; int testDurationMS = as->testDurationMS; size_t triangleLimit = as->triangleLimit; double triangleAreaPixels = as->triangleAreaInPixels; int lighting=as->enableLighting; int colorMaterial = as->enableColorMaterial; int texturingEnabled = (as->textureSize > 0) ? 1 : 0; int textureSize = as->textureSize; /* * Objective(s): * 1. Want triangles with a specified area (e.g., 8sq pixels) for the * purpose of generating a graphics load with reasonably precise * characteristics . * 2. Want triangles to *all* remain on-screen so that triangle rate * reflects the cost of rendering visible triangles/fragments. * * Approach: * 1. build a base mesh of vertices. This set of verts is positioned * so that it will remain entirely within the view frustum no matter * how we rotate it (in 2D) * 2. dispatch off the mesh vertices as disjoint triangles using * vertex arrays. This approach is not the most efficient way to * represent densely packed triangles, but has a couple of benefits: * - It allows us to easily set the maximum number of triangles per * frame to be dispatched off to the renderer w/o having to resort * to tricks like inserting degenerate triangles in strips to connect * up two disjoint strips. * - It allows us to glDrawArrays rather than glDrawElements. The latter * uses one level of indirection, and will be less efficient. * - In the end, we care about vertex rate, not triangle rate. * * An early version of this code used the glDrawElements with indices. * Rather than insert compile-time switches, this old code is simply * cut-n-pasted and commented out at the end of this file. */ /* Make sure we are drawing to the front buffer */ glDrawBuffer(GL_FRONT); glDisable(GL_DEPTH_TEST); /* Clear the screen */ glClear(GL_COLOR_BUFFER_BIT); if (as->outlineMode != 0) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /* Load identities */ glMatrixMode( GL_PROJECTION ); glPushMatrix(); glLoadIdentity( ); glMatrixMode( GL_MODELVIEW ); glPushMatrix(); glLoadIdentity(); /* change range from -1..1 to 0..[screenWidth, screenHeight] */ glTranslatef(-1.0, -1.0, 0.0); glScalef(2.0/r, 2.0/r, 1.0F); if (lighting) { /* turn on OpenGL lighting stuff */ GLfloat lightPosition[] = {(float)screenWidth*0.5F, (float)screenHeight*0.5F, 200.0F, 1.0F}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); if (colorMaterial) { glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_DIFFUSE); } /* just use OpenGL's default light values: directional light source */ } else { glDisable(GL_LIGHTING); glDisable(GL_COLOR_MATERIAL); } if (texturingEnabled) { buildAndDownloadTexture(textureSize); glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); } else glDisable(GL_TEXTURE_2D); /* build the base quadmesh vertex array */ buildBaseArrays(triangleAreaPixels, as->imgWidth, as->imgHeight, &nVertsPerAxis, &baseVerts, &baseColors, &baseNormals, &baseTCs); /* now, repackage that information into bundles suitable for submission to GL using the specified primitive type */ if (as->triangleType == DISJOINT_TRIANGLES) { int triangleLimit; if ((as->triangleLimit*3) > as->vertexBufLimit) triangleLimit = as->vertexBufLimit/3; else triangleLimit = as->triangleLimit; buildDisjointTriangleArrays(nVertsPerAxis, triangleLimit, &dispatchTriangles, &dispatchVertexCount, baseVerts, baseColors, baseNormals, baseTCs, &dispatchVerts, &dispatchColors, &dispatchNormals, &dispatchTCs); as->computedVertsPerArrayCall = dispatchVertexCount; as->computedIndicesPerArrayCall = 0; } else if (as->triangleType == TRIANGLE_STRIPS) { if ((as->triangleLimit+2) > as->vertexBufLimit) triangleLimit = as->vertexBufLimit-2; else triangleLimit = as->triangleLimit; buildTriangleStripArrays(nVertsPerAxis, triangleLimit, &dispatchTriangles, &dispatchVertexCount, baseVerts, baseColors, baseNormals, baseTCs, &dispatchVerts, &dispatchColors, &dispatchNormals, &dispatchTCs); as->computedVertsPerArrayCall = dispatchVertexCount; as->computedIndicesPerArrayCall = 0; } else if (as->triangleType == INDEXED_DISJOINT_TRIANGLES) { if ((as->triangleLimit*3) > as->vertexBufLimit) triangleLimit = as->vertexBufLimit/3; else triangleLimit = as->triangleLimit; buildIndexedDisjointTriangleArrays(nVertsPerAxis, triangleLimit, &dispatchTriangles, &dispatchVertexCount, baseVerts, baseColors, baseNormals, baseTCs, &dispatchVerts, &dispatchColors, &dispatchNormals, &dispatchTCs, &dispatchIndices); as->computedVertsPerArrayCall = (nVertsPerAxis+1)*(nVertsPerAxis+1); as->computedIndicesPerArrayCall = dispatchVertexCount; } else if (as->triangleType == INDEXED_TRIANGLE_STRIPS) { if ((as->triangleLimit+2) > as->vertexBufLimit) triangleLimit = as->vertexBufLimit-2; else triangleLimit = as->triangleLimit; buildIndexedTriangleStripArrays(nVertsPerAxis, triangleLimit, &dispatchTriangles, &dispatchVertexCount, baseVerts, baseColors, baseNormals, baseTCs, &dispatchVerts, &dispatchColors, &dispatchNormals, &dispatchTCs, &dispatchIndices); as->computedVertsPerArrayCall = (nVertsPerAxis+1)*(nVertsPerAxis+1); as->computedIndicesPerArrayCall = dispatchVertexCount; } /* Set up the pointers */ glVertexPointer(2, GL_FLOAT, 0, (const GLvoid *)dispatchVerts); glEnableClientState(GL_VERTEX_ARRAY); glColorPointer(3, GL_FLOAT, 0, (const GLvoid *)dispatchColors); glEnableClientState(GL_COLOR_ARRAY); if (lighting) { glNormalPointer(GL_FLOAT, 0, (const GLvoid *)dispatchNormals); glEnableClientState(GL_NORMAL_ARRAY); } if (texturingEnabled) { glTexCoordPointer(2, GL_FLOAT, 0, (const GLvoid *)dispatchTCs); glEnableClientState(GL_TEXTURE_COORD_ARRAY); } glFinish(); /* make sure all setup is finished */ startTime = endTime = glutGet(GLUT_ELAPSED_TIME); while ((endTime - startTime) < testDurationMS) { int buildingList=0; if (as->clearPerFrame != 0) glClear(GL_COLOR_BUFFER_BIT); /* tmp for debug */ #if SCREENSHOT_MODE /* screenshot mode won't work with -retained */ /* Clear the screen */ glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, dispatchVertexCount); glDrawArrays(GL_TRIANGLES, 0, dispatchVertexCount); fprintf(stderr," You have screenshot mode enabled! \n"); fflush(stderr); sleep(5); break; #endif if (as->retainedModeEnabled != 0) { if (trianglesListIndx == 0) /* no list yet, build one */ { trianglesListIndx = glGenLists(1); /* glNewList(trianglesListIndx, GL_COMPILE_AND_EXECUTE); */ glNewList(trianglesListIndx, GL_COMPILE); buildingList = 1; } else glCallList(trianglesListIndx); } if ((buildingList == 0) || (as->retainedModeEnabled != 0)) { switch (as->triangleType) { case DISJOINT_TRIANGLES: glDrawArrays(GL_TRIANGLES, 0, dispatchVertexCount); break; case TRIANGLE_STRIPS: glDrawArrays(GL_TRIANGLE_STRIP, 0, dispatchVertexCount); break; case INDEXED_DISJOINT_TRIANGLES: glDrawElements(GL_TRIANGLES, dispatchVertexCount, GL_UNSIGNED_INT, dispatchIndices); break; case INDEXED_TRIANGLE_STRIPS: glDrawElements(GL_TRIANGLE_STRIP, dispatchVertexCount, GL_UNSIGNED_INT, dispatchIndices); break; default: break; } } glTranslatef(r/2.0, r/2.0, 0.0F); glRotatef(0.01F, 0.0F, 0.0F, 1.0F); glTranslatef(-r/2.0, -r/2.0, 0.0F); if (buildingList == 1) { glEndList(); glCallList(trianglesListIndx); /* cheat - reset the clock to eliminate the time required for creating the display list from the frame rate computation */ startTime = endTime = glutGet(GLUT_ELAPSED_TIME); } endTime = glutGet(GLUT_ELAPSED_TIME); nFrames++; totalTris += dispatchTriangles; totalVerts += as->computedVertsPerArrayCall; } /* TODO: Ask about how to get it to draw */ glFinish(); endTime = glutGet(GLUT_ELAPSED_TIME); /* Restore the gl stack */ glMatrixMode( GL_MODELVIEW ); glPopMatrix(); glMatrixMode( GL_PROJECTION ); glPopMatrix(); /* Before printing the results, make sure we didn't have ** any GL related errors. */ check_gl_errors(); /* the pause that refreshes -- */ #ifdef _WIN32 Sleep(250); #else usleep(250000); #endif elapsedTimeMS = endTime - startTime; float elapsedTimeS = elapsedTimeMS / 1000; printf("Elapsed Time: %f s\n", elapsedTimeS); printf("Total Triangles: %d\n", (int) totalTris); printf("Total Verticies: %d\n", (int) totalVerts); printf("Frames per Second (FPS): %f\n", nFrames / elapsedTimeS); /* * myAppState.appName, * myAppState.triangleAreaInPixels, * myAppState.computedMTrisPerSecond, * myAppState.computedMVertexOpsPerSecond, * myAppState.computedVertsPerArrayCall, * myAppState.computedMFragsPerSecond, * (int) myAppState.computedIndicesPerArrayCall); */ myAppState.computedMTrisPerSecond = (totalTris / 1000000.0f) / elapsedTimeS; myAppState.computedMVertexOpsPerSecond = (totalVerts / 1000000.0f) / elapsedTimeS; myAppState.computedMFragsPerSecond = myAppState.computedMTrisPerSecond * myAppState.triangleAreaInPixels; printf("verts/frame = %d \n", dispatchVertexCount); printf("nframes = %d \n", nFrames); free((void *)baseVerts); free((void *)baseColors); free((void *)baseNormals); free((void *)baseTCs); free((void *)dispatchVerts); free((void *)dispatchColors); free((void *)dispatchNormals); free((void *)dispatchTCs); if (dispatchIndices != NULL) free((void *)dispatchIndices); if (texturingEnabled) cleanupTexture(); }
void buildAndDownloadTexture(int textureSize) { Color4DUbyte *texturePixels; int i, j, indx; #if 0 Color4DUbyte green={0x0, 0xFF, 0x0, 0xFF}; Color4DUbyte red={0xFF, 0x00, 0x0, 0xFF}; #endif Color4DUbyte green={0x0, 0xFF, 0x0, 0x3F}; Color4DUbyte red={0xFF, 0x00, 0x0, 0x3F}; glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, &textureName); glBindTexture(GL_TEXTURE_2D, textureName); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); texturePixels = (Color4DUbyte *)malloc(sizeof(Color4DUbyte)*textureSize*textureSize); indx = 0; for (j=0;j<textureSize;j++) { Color4DUbyte *even, *odd; if ((j&1) == 0) /* odd */ { even = &green; odd = &red; } else { even = &red; odd = &green; } for (i=0;i<textureSize;i++) { if ((i&1) == 0) texturePixels[indx] = *even; else texturePixels[indx] = *odd; indx++; } } /* it would be nice to add another parameter to select the internal texture format from the command line. */ glTexImage2D(GL_TEXTURE_2D, 0, TEXTURE_STORAGE_MODE, textureSize, textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid *)texturePixels); free((void *)texturePixels); glFinish(); check_gl_errors (); }
void sprite_sheet::incremental_load(const application_folder & in_application_folder) { switch(internal_status) { case sprite_sheet_status::ready: case sprite_sheet_status::failed: break; case sprite_sheet_status::waiting_to_load: { if(internal_loaded_file_data.status() == file_data_status::waiting_to_load) { internal_status = sprite_sheet_status::loading; internal_loaded_file_data.load(in_application_folder, internal_sheet_file_path.c_str()); } break; } case sprite_sheet_status::loading: { if(internal_loaded_file_data.status() == file_data_status::loading) { } else if(internal_loaded_file_data.status() == file_data_status::ready) { auto file_data = internal_loaded_file_data.data(); atl::input_bit_string_type bit_string(file_data.data(), file_data.data() + file_data.size()); // Load frames: glActiveTexture(GL_TEXTURE0); uint32_t numSheets; atl::bit_string_read_chunked_integer<uint32_t>(bit_string, numSheets, 3); uint32_t totalSprites; atl::bit_string_read_chunked_integer<uint32_t>(bit_string, totalSprites, 7); sprites.resize(totalSprites); textures.resize(numSheets); for(auto & sheetId : textures) { // give the sheet a valid opengl texture ID glGenTextures(1, &sheetId); check_gl_errors(); uint32_t fontSheetWidthPower, fontSheetHeightPower; atl::bit_string_read_ranged_integer<uint32_t>(bit_string, fontSheetWidthPower, 0, 12); atl::bit_string_read_ranged_integer<uint32_t>(bit_string, fontSheetHeightPower, 0, 12); uint32_t sheetWidth = 1 << fontSheetWidthPower; uint32_t sheetHeight = 1 << fontSheetHeightPower; atl::size2f sheetSize(sheetWidth, sheetHeight); uint32_t sheetSprites; atl::bit_string_read_ranged_integer<uint32_t>(bit_string, sheetSprites, 0, totalSprites); while(sheetSprites-- > 0) { // Deserialize the frame index: uint32_t spriteIndex; atl::bit_string_read_ranged_integer<uint32_t>(bit_string, spriteIndex, 0, totalSprites - 1); // Deserialize the texture coordinates: atl::box2f l_texCoords; { uint32_t l_texT, l_texR, l_texB, l_texL; atl::bit_string_read_ranged_integer<uint32_t>(bit_string, l_texT, 0, sheetHeight); atl::bit_string_read_ranged_integer<uint32_t>(bit_string, l_texR, 0, sheetWidth); atl::bit_string_read_ranged_integer<uint32_t>(bit_string, l_texB, 0, sheetHeight); atl::bit_string_read_ranged_integer<uint32_t>(bit_string, l_texL, 0, sheetWidth); if(l_texR - l_texL == 1) { l_texCoords.l = l_texCoords.r = (float(l_texL) + 0.5f) / float(sheetWidth); } else { l_texCoords.l = (float(l_texL)) / float(sheetWidth); l_texCoords.r = (float(l_texR)) / float(sheetWidth); } if(l_texB - l_texT == 1) { l_texCoords.t = l_texCoords.b = (float(l_texT) + 0.5f) / float(sheetHeight); } else { l_texCoords.t = (float(l_texT)) / float(sheetHeight); l_texCoords.b = (float(l_texB)) / float(sheetHeight); } } // Deserialize the drawn area: atl::box2f l_area; atl::bit_string_read_values(bit_string, l_area.t, l_area.r, l_area.b, l_area.l); sprites[spriteIndex].set(sheetId, l_texCoords, l_area); } // Decode PNG data and upload to card: glBindTexture(GL_TEXTURE_2D, sheetId); int32_t numPNGBytesInt32; atl::bit_string_read_value(bit_string, numPNGBytesInt32); size_t numPNGBytes = numPNGBytesInt32; atl::bit_string_skip_to_next_byte(bit_string); const unsigned char * l_pngBytes = bit_string.ptr; bit_string.ptr += numPNGBytes; unsigned char * imageDataOut = nullptr; unsigned int imageWidthOut; unsigned int imageHeightOut; unsigned int decodeError = lodepng_decode32(&imageDataOut, &imageWidthOut, &imageHeightOut, l_pngBytes, numPNGBytes); atl_fatal_if(decodeError > 0, "Couldn't decode PNG in sprite sheet"); atl_fatal_if(imageWidthOut != sheetWidth || imageHeightOut != sheetHeight, "PNG in sprite sheet of unexpected size"); // Allocate GL resource: glPixelStorei(GL_UNPACK_ALIGNMENT, 1); check_gl_errors(); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, atl::integer_cast<GLsizei>(sheetWidth, [](){}), atl::integer_cast<GLsizei>(sheetHeight, [](){}), 0, GL_RGBA, GL_UNSIGNED_BYTE, imageDataOut); check_gl_errors(); GLint lFilteringMode = internal_texture_filtering_mode == texture_filtering_mode::linear ? GL_LINEAR : GL_NEAREST; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, lFilteringMode); check_gl_errors(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, lFilteringMode); check_gl_errors(); delete[] imageDataOut; } internal_loaded_file_data.free(); internal_status = sprite_sheet_status::ready; } else { internal_status = sprite_sheet_status::failed; } break; } } }
void init_stuff() { int seed; Uint32 (*my_timer_pointer) (unsigned int) = my_timer; //TODO: process command line options chdir(datadir); //Initialize all strings init_translatables(); #ifdef WRITE_XML load_translatables();//Write to the current working directory - hopefully we'll have write rights here... #endif //read the config file read_config(); //Parse command line options read_command_line(); //OK, we have the video mode settings... setup_video_mode(full_screen,video_mode); //now you may set the video mode using the %<foo> in-game video_mode_set=1; //Good, we should be in the right working directory - load all translatables from their files load_translatables(); init_video(); resize_window(); init_gl_extensions(); #ifdef CAL3D create_cal3d_model(); init_cal3d_model(); #endif seed = time (NULL); srand (seed); cache_system_init(MAX_CACHE_SYSTEM); init_texture_cache(); init_md2_cache(); init_e3d_cache(); init_2d_obj_cache(); load_ignores(); load_filters(); load_e3d_list(); load_e2d_list(); load_part_list(); load_knowledge_list(); load_cursors(); build_cursors(); change_cursor(CURSOR_ARROW); build_glow_color_table(); init_actors_lists(); memset(tile_list, 0, sizeof(tile_list)); memset(lights_list, 0, sizeof(lights_list)); init_particles_list(); memset(actors_defs, 0, sizeof(actors_defs)); init_actor_defs(); load_map_tiles(); //lights setup build_global_light_table(); build_sun_pos_table(); reset_material(); init_lights(); disable_local_lights(); init_colors(); clear_error_log(); clear_conn_log(); clear_thunders(); build_rain_table(); read_bin_cfg(); build_levels_table();//for some HUD stuff init_scale_array(); if(!no_sound)init_sound(); //initialize the fonts init_fonts(); check_gl_errors(); //load the necesary textures //font_text=load_texture_cache("./textures/font.bmp",0); icons_text=load_texture_cache("./textures/gamebuttons.bmp",0); hud_text=load_texture_cache("./textures/gamebuttons2.bmp",0); cons_text=load_texture_cache("./textures/console.bmp",255); sky_text_1=load_texture_cache("./textures/sky.bmp",70); particle_textures[0]=load_texture_cache("./textures/particle0.bmp",0); particle_textures[1]=load_texture_cache("./textures/particle1.bmp",0); particle_textures[2]=load_texture_cache("./textures/particle2.bmp",0); particle_textures[3]=load_texture_cache("./textures/particle3.bmp",0); particle_textures[4]=load_texture_cache("./textures/particle4.bmp",0); particle_textures[5]=load_texture_cache("./textures/particle5.bmp",0); particle_textures[6]=load_texture_cache("./textures/particle6.bmp",0); particle_textures[7]=load_texture_cache("./textures/particle7.bmp",0); items_text_1=load_texture_cache("./textures/items1.bmp",0); items_text_2=load_texture_cache("./textures/items2.bmp",0); items_text_3=load_texture_cache("./textures/items3.bmp",0); items_text_4=load_texture_cache("./textures/items4.bmp",0); items_text_5=load_texture_cache("./textures/items5.bmp",0); items_text_6=load_texture_cache("./textures/items6.bmp",0); items_text_7=load_texture_cache("./textures/items7.bmp",0); items_text_8=load_texture_cache("./textures/items8.bmp",0); items_text_9=load_texture_cache("./textures/items9.bmp",0); portraits1_tex=load_texture_cache("./textures/portraits1.bmp",0); portraits2_tex=load_texture_cache("./textures/portraits2.bmp",0); portraits3_tex=load_texture_cache("./textures/portraits3.bmp",0); portraits4_tex=load_texture_cache("./textures/portraits4.bmp",0); portraits5_tex=load_texture_cache("./textures/portraits5.bmp",0); halo_tex=load_texture_cache("./textures/halo.bmp",0); if(have_multitexture)ground_detail_text=load_texture_cache("./textures/ground_detail.bmp",255); check_gl_errors(); create_char_error_str[0]=0; init_opening_interface(); init_hud_interface(); if(SDLNet_Init()<0) { char str[120]; sprintf(str,"%s: %s\n",failed_sdl_net_init,SDLNet_GetError()); log_error(str); SDLNet_Quit(); SDL_Quit(); exit(2); } if(SDL_InitSubSystem(SDL_INIT_TIMER)<0) { char str[120]; sprintf(str, "%s: %s\n", failed_sdl_timer_init,SDL_GetError()); log_error(str); SDL_Quit(); exit(1); } SDL_SetTimer (1000/(18*4), my_timer_pointer); ReadXML("languages/en/Encyclopedia/index.xml"); read_key_config(); load_questlog(); init_buddy(); //initiate function pointers init_attribf(); //we might want to do this later. connect_to_server(); }
void simple_font_renderer::incremental_load(const application_folder & in_application_folder) { switch(internal_status) { case simple_font_renderer_status::ready: case simple_font_renderer_status::failed: break; case simple_font_renderer_status::waiting_to_load: { internal_vertex_shader_file.load(in_application_folder, "SimpleFont.vsh"); internal_fragment_shader_file.load(in_application_folder, "SimpleFont.fsh"); internal_status = simple_font_renderer_status::loading; break; } case simple_font_renderer_status::loading: { auto l_vsh_status = internal_vertex_shader_file.status(); auto l_fsh_status = internal_fragment_shader_file.status(); if((l_vsh_status != file_data_status::loading && l_vsh_status != file_data_status::ready) || (l_fsh_status != file_data_status::loading && l_fsh_status != file_data_status::ready)) { internal_status = simple_font_renderer_status::failed; } else if(l_vsh_status == file_data_status::ready && l_fsh_status == file_data_status::ready) { GLuint l_vertex_shader = 0, l_fragment_shader = 0; if(!compile_shader(internal_vertex_shader_file.data(), &l_vertex_shader, GL_VERTEX_SHADER) || !compile_shader(internal_fragment_shader_file.data(), &l_fragment_shader, GL_FRAGMENT_SHADER)) { atl_debug_log("Shader failed to compile"); internal_status = simple_font_renderer_status::failed; } else { // Index data: { unsigned short l_vertIdx = 0; for(unsigned int idx = 0; idx < pcsm_maxIndicesPerPass; idx += pcsm_indicesPerCharacter) { pm_indexData[idx + 0] = l_vertIdx + 0; pm_indexData[idx + 1] = l_vertIdx + 1; pm_indexData[idx + 2] = l_vertIdx + 2; pm_indexData[idx + 3] = l_vertIdx + 1; pm_indexData[idx + 4] = l_vertIdx + 2; pm_indexData[idx + 5] = l_vertIdx + 3; l_vertIdx += 4; } } // Do GL stuff to set up the vertex buffer: for(int i = 0; i < pcsm_numBuffers; i++) { pm_glVertexArray[i].alloc(); pm_glVertexBuffer[i].alloc(); } pm_glIndexBuffer.alloc(); for(int idx_buffer = 0; idx_buffer < pcsm_numBuffers; idx_buffer++) { pm_glVertexArray[idx_buffer].bind(); internal_configure_vertex_array(idx_buffer); glBufferData(GL_ARRAY_BUFFER, atl::c_array_byte_length(pm_vertexData), NULL, GL_DYNAMIC_DRAW); check_gl_errors(); glBufferData(GL_ELEMENT_ARRAY_BUFFER, atl::c_array_byte_length(pm_indexData), &pm_indexData[0], GL_STATIC_DRAW); check_gl_errors(); } atl::c_array_last_by_ref(pm_glVertexArray).unbind(); // Create shader program. pm_glProgram.alloc(); // Attach vertex shader to program. glAttachShader(pm_glProgram, l_vertex_shader); // Attach fragment shader to program. glAttachShader(pm_glProgram, l_fragment_shader); // Bind attribute locations. // This needs to be done prior to linking. glBindAttribLocation(pm_glProgram, ATTRIBUTE_VERT_POSITION, "v_position"); glBindAttribLocation(pm_glProgram, ATTRIBUTE_VERT_TEX_COORD, "v_texCoord"); glBindAttribLocation(pm_glProgram, ATTRIBUTE_VERT_COLOR, "v_color"); glBindAttribLocation(pm_glProgram, ATTRIBUTE_VERT_EDGE, "v_edge"); glBindAttribLocation(pm_glProgram, ATTRIBUTE_VERT_RADIUS, "v_radius"); // Link program. if(link_program(pm_glProgram)) { // Get uniform locations. pm_shaderUniforms[UNIFORM_SCREEN_DIM] = glGetUniformLocation(pm_glProgram, "u_screenBounds"); pm_shaderUniforms[UNIFORM_TRANSLATION] = glGetUniformLocation(pm_glProgram, "u_translation"); pm_shaderUniforms[UNIFORM_SAMPLER_DISTANCE_FIELD] = glGetUniformLocation(pm_glProgram, "s_distanceField"); internal_status = simple_font_renderer_status::ready; } else { atl_debug_log("Shader failed to link"); pm_glProgram.free(); internal_status = simple_font_renderer_status::failed; } } internal_vertex_shader_file.free(); internal_fragment_shader_file.free(); // Release vertex and fragment shaders. if(l_vertex_shader != 0) { glDetachShader(pm_glProgram, l_vertex_shader); glDeleteShader(l_vertex_shader); } if(l_fragment_shader != 0) { glDetachShader(pm_glProgram, l_fragment_shader); glDeleteShader(l_fragment_shader); } } break; } } }
void rendering_scene() { glMatrixMode(GL_MODELVIEW); render_scene_context(g_rcontext); check_gl_errors("render_scene"); glutSwapBuffers(); }
void flat_textured_renderer::incremental_load(const application_folder & in_application_folder) { switch(internal_status) { case flat_textured_renderer_status::ready: case flat_textured_renderer_status::failed: break; case flat_textured_renderer_status::waiting_to_load: { internal_vertex_shader_file.load(in_application_folder, "flat_textured.vsh"); internal_fragment_shader_file.load(in_application_folder, "flat_textured.fsh"); internal_status = flat_textured_renderer_status::loading; break; } case flat_textured_renderer_status::loading: { auto l_vsh_status = internal_vertex_shader_file.status(); auto l_fsh_status = internal_fragment_shader_file.status(); if((l_vsh_status != file_data_status::loading && l_vsh_status != file_data_status::ready) || (l_fsh_status != file_data_status::loading && l_fsh_status != file_data_status::ready)) { internal_status = flat_textured_renderer_status::failed; } else if(l_vsh_status == file_data_status::ready && l_fsh_status == file_data_status::ready) { GLuint l_vertex_shader = 0, l_fragment_shader = 0; if(!compile_shader(internal_vertex_shader_file.data(), &l_vertex_shader, GL_VERTEX_SHADER) || !compile_shader(internal_fragment_shader_file.data(), &l_fragment_shader, GL_FRAGMENT_SHADER)) { atl_debug_log("Shader failed to compile"); internal_status = flat_textured_renderer_status::failed; } else { // Create shader program. internal_program.alloc(); // Attach vertex shader to program. glAttachShader(internal_program, l_vertex_shader); // Attach fragment shader to program. glAttachShader(internal_program, l_fragment_shader); // Bind attribute locations. // This needs to be done prior to linking. glBindAttribLocation(internal_program, ATTRIBUTE_POSITION, "v_position"); check_gl_errors(); glBindAttribLocation(internal_program, ATTRIBUTE_TEXTURE_COORDINATES, "v_texture_coordinates"); check_gl_errors(); glBindAttribLocation(internal_program, ATTRIBUTE_COLOR_MULTIPLY, "v_color_multiply"); check_gl_errors(); glBindAttribLocation(internal_program, ATTRIBUTE_COLOR_LERP, "v_color_lerp"); check_gl_errors(); // Link program. if(link_program(internal_program)) { // Get uniform locations. internal_shader_uniforms[UNIFORM_SCREEN_DIM] = glGetUniformLocation(internal_program, "u_stage_bounds"); check_gl_errors(); internal_status = flat_textured_renderer_status::ready; } else { atl_debug_log("Shader failed to link"); internal_program.free(); internal_status = flat_textured_renderer_status::failed; } } internal_vertex_shader_file.free(); internal_fragment_shader_file.free(); // Release vertex and fragment shaders. if(l_vertex_shader != 0) { glDetachShader(internal_program, l_vertex_shader); glDeleteShader(l_vertex_shader); } if(l_fragment_shader != 0) { glDetachShader(internal_program, l_fragment_shader); glDeleteShader(l_fragment_shader); } } break; } } }
static void render_scene (Viewer *self) { // glClearColor (0.0, 0.0, 0.0, 1.0); glClearColor(self->backgroundColor[0], self->backgroundColor[1], self->backgroundColor[2], self->backgroundColor[3]); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (self->view_handler) self->view_handler->update_gl_matrices(self, self->view_handler); glMatrixMode (GL_MODELVIEW); /* give the ambient light a blue tint to match the blue sky */ float light0_amb[] = { 0.8, 0.8, 1.0, 1 }; float light0_dif[] = { 1, 1, 1, 1 }; float light0_spe[] = { .5, .5, .5, 1 }; float light0_pos[] = { 100, 100, 100, 0 }; glLightfv (GL_LIGHT0, GL_AMBIENT, light0_amb); glLightfv (GL_LIGHT0, GL_DIFFUSE, light0_dif); glLightfv (GL_LIGHT0, GL_SPECULAR, light0_spe); glLightfv (GL_LIGHT0, GL_POSITION, light0_pos); glEnable (GL_LIGHT0); if (self->prettier_flag) { glEnable(GL_LINE_STIPPLE); glEnable(GL_LINE_SMOOTH); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); glEnable(GL_POINT_SMOOTH); glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); } for (unsigned int ridx = 0; ridx < g_ptr_array_size(self->renderers); ridx++) { Renderer *renderer = g_ptr_array_index(self->renderers, ridx); if (renderer->enabled) { glPushAttrib(GL_ENABLE_BIT | GL_POINT_BIT | GL_POLYGON_STIPPLE_BIT | GL_POLYGON_BIT | GL_LINE_BIT | GL_FOG_BIT | GL_LIGHTING_BIT ); glPushMatrix(); if (renderer->draw) renderer->draw (self, renderer); check_gl_errors (renderer->name); glPopMatrix(); glPopAttrib(); } } /* glColor3f(1, 1, 1); glBegin(GL_POINTS); glVertex3f(0,0,0); glVertex3f(1,0,0); glVertex3f(2,0,0); glVertex3f(0,1,0); glEnd(); */ }