void VCMesh::Initialize( VCPuvnVerticie* verts, UInt32* indicies, int vCount, int iCount ) { VertexCount = vCount; IndexCount = iCount; // Create VAO glGenVertexArrays(1, &m_VAO); glBindVertexArray(m_VAO); glErrorCheck(); // Create Verticie VBO glGenBuffers(1, &m_vertextVBO); glBindBuffer(GL_ARRAY_BUFFER, m_vertextVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(VCPuvnVerticie) * VertexCount, verts, GL_STATIC_DRAW); ZERO_CHECK(m_vertextVBO); // Create Index VBO glGenBuffers(1, &m_indexVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(UInt32) * iCount, indicies, GL_STATIC_DRAW); ZERO_CHECK(m_vertextVBO); // Bind Attributes glEnableVertexAttribArray(VCShaderAttribute::Position0); glEnableVertexAttribArray(VCShaderAttribute::TexCoord0); glEnableVertexAttribArray(VCShaderAttribute::Normal0); glVertexAttribPointer(VCShaderAttribute::Position0, 3, GL_FLOAT, GL_FALSE, sizeof(VCPuvnVerticie), (void*) offsetof(VCPuvnVerticie, Position) ); glVertexAttribPointer(VCShaderAttribute::TexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(VCPuvnVerticie), (void*) offsetof(VCPuvnVerticie, UV) ); glVertexAttribPointer(VCShaderAttribute::Normal0, 3, GL_FLOAT, GL_FALSE, sizeof(VCPuvnVerticie), (void*) offsetof(VCPuvnVerticie, Normal) ); glBindVertexArray(0); }
/** * Saves a screenshot of the screen's contents. * @param filename Filename of the PNG file. */ void Screen::screenshot(const std::string &filename) const { SDL_Surface *screenshot = SDL_AllocSurface(0, getWidth() - getWidth()%4, getHeight(), 24, 0xff, 0xff00, 0xff0000, 0); if (isOpenGLEnabled()) { #ifndef __NO_OPENGL GLenum format = GL_RGB; for (int y = 0; y < getHeight(); ++y) { glReadPixels(0, getHeight()-(y+1), getWidth() - getWidth()%4, 1, format, GL_UNSIGNED_BYTE, ((Uint8*)screenshot->pixels) + y*screenshot->pitch); } glErrorCheck(); #endif } else { SDL_BlitSurface(_screen, 0, screenshot, 0); } unsigned error = lodepng::encode(filename, (const unsigned char *)(screenshot->pixels), getWidth() - getWidth()%4, getHeight(), LCT_RGB); if (error) { Log(LOG_ERROR) << "Saving to PNG failed: " << lodepng_error_text(error); } SDL_FreeSurface(screenshot); }
bool Game::init() { if (glfwInit() != GL_TRUE) return false; glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); this->window = glfwOpenWindow(this->width, this->height, 8, 8, 8, 0, 24, 0, GLFW_WINDOW); if (!this->window) return false; glfwSetWindowTitle("Battlecraft Editor"); glfwSwapInterval(1); // OpenGL Settings glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_LINE_SMOOTH); glEnable(GL_BLEND); glClearColor(0.1f, 0.1f, 0.1f, 1.0f); srand(11247); this->camera = new Camera(width, height, 90); shader = new VoxelShader(); shader->compile(); tileset = new Tileset(256); tileset->set(1, 250,20,20); tileset->set(2, 20,250,20); tileset->set(3, 20,20,250); tileset->set(4, 255,0,255); chunk = new Chunk(tileset, 16,16,16); chunk->compute(); glErrorCheck("pre linesh compilation") linesh = new LineShader(); if (!linesh->compile()) return false; bounds = new Boundbox(16,16,16); printf("A: %d, a: %d\n", 'A','a'); return true; }
void OpenGL::resize(unsigned width, unsigned height) { if(gltexture == 0) glGenTextures(1, &gltexture); glErrorCheck(); iwidth = SDL_max(width, iwidth ); iheight = SDL_max(height, iheight); if(buffer_surface) delete buffer_surface; buffer_surface = new Surface(iwidth, iheight, 0, 0, ibpp); // use OpenXcom's Surface class to get an aligned buffer with bonus SDL_Surface buffer = (uint32_t*) buffer_surface->getSurface()->pixels; glBindTexture(GL_TEXTURE_2D, gltexture); glErrorCheck(); glPixelStorei(GL_UNPACK_ROW_LENGTH, iwidth); glErrorCheck(); glTexImage2D(GL_TEXTURE_2D, /* mip-map level = */ 0, /* internal format = */ GL_RGB16_EXT, width, height, /* border = */ 0, /* format = */ GL_BGRA, iformat, buffer); glErrorCheck(); }
// draw the pool ball! void PoolBall::render(glm::dmat4 &parent_model) { glm::dmat4 model = parent_model * glm::translate(glm::dmat4(1.0), glm::dvec3(x, diameter/2, z)) * rotation * glm::scale(glm::dmat4(1.0), glm::dvec3(diameter/2)); mesh.render(tex, model); #if 0 glUseProgram(0); glErrorCheck(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMultMatrixd(&view[0][0]); glMultMatrixd(&parent_model[0][0]); glMatrixMode(GL_PROJECTION); //glLoadIdentity(); extern double wide_view[]; //gluLookAt(eye[0], eye[1], eye[2], wide_view[3], wide_view[4], wide_view[5], 0, 1, 0); glLoadMatrixd(&perspective[0][0]); glEnable(GL_DEPTH_TEST); glBegin(GL_LINES); #if 0 glVertex4f(x, 0, z, 1.0f); glVertex4f(light_position[0], light_position[1], light_position[2], 1.0f); #endif #if 0 for (int i = 0; i < vertices.size(); ++i) { glVertex3f(vertices[i][0], vertices[i][1], vertices[i][2]); glVertex3f(vertices[i][0] + normals[i][0], vertices[i][1] + normals[i][1], vertices[i][2] + normals[i][2]); } #endif glEnd(); glErrorCheck(); #endif glDisable(GL_TEXTURE_2D); }
void VCGLRenderer::Initialize() { // GL Setup glClearColor(0.4f, 0.6f, 0.8f, 1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glDepthFunc(GL_LESS); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); CreateDepthFrameBuffer(); glErrorCheck(); std::cout << "VCGLRenderer Initialized" << std::endl; }
void Game::run() { double lastTime = glfwGetTime(), currentTime = 0.0; float delta = 0.0f; running = true; while(running && glfwGetWindowParam(GLFW_OPENED)) { // Calculate delta t currentTime = glfwGetTime(); delta = (float)(currentTime - lastTime); lastTime = currentTime; update(delta); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw(); camera->update(delta); glErrorCheck("main loop"); glfwSwapBuffers(); glfwSleep(0.001); } }
void VCWindow::Initalize() { std::cout << "Creating a VCWindow..." << std::endl; // ===== GLFW ====================================================== glfwSetErrorCallback(glewErrorCallback); if (!glfwInit()) { std::cout << "Failed to initialize GLFW." << std::endl; std::cin.ignore(); } //glfwWindowHint(GLFW_DECORATED, false); //glfwWindowHint(GLFW_SAMPLES, 4); //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #if DEBUG glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true); #endif GLFWWindowHandle = glfwCreateWindow(1280, 600, "Thilenius - A 0.0.1", NULL, NULL); if (!GLFWWindowHandle) { glfwTerminate(); std::cout << "Failed to create a window." << std::endl; std::cin.ignore(); } glfwMakeContextCurrent(GLFWWindowHandle); // ===== GLEW ====================================================== glewExperimental = true; GLenum glewError = glewInit(); if( glewError != GLEW_OK ) { printf( "Error initializing GLEW! %s\n", glewGetErrorString( glewError ) ); std::cin.ignore(); } // ===== Debug ====================================================== #if DEBUG glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB); glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE); glDebugMessageCallbackARB(&glDebugCallback, NULL); #endif GLint major, minor; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); std::cout << "OpenGL version: " << major << "." << minor << std::endl; std::cout << "Hardware: " << glGetString(GL_RENDERER) << std::endl << std::endl; glfwGetWindowSize(GLFWWindowHandle, &Width, &Height); FullViewport = VCRectangle(0, 0, Width, Height); SetVSync(false); std::cout << "VCWindow Initialized." << std::endl; glErrorCheck(); VCInteropWindowSetPos(200, 200); }
void MainLoop() { DemoData.Type=255; MouseString=new char[100]; memset(MouseString,0,100); while (!Done) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else if (Keys[VK_ESCAPE]) Done=true; else { PlayBackFunction(); WriteDebug("Main Loop\n\n"); CurrentCursor=Arrow; glClear(0x4100); SwitchTo2D(); ToolTip=LastClickedToolTip; memset(ItemNameString,0,256); //glFlush(); glColor4f(1,1,1,1); if (CommandLineCommand) ErrorMessage=""; if (CommandLineCommand && CommandLineEnter) { CommandLineCommand=false; CommandLineEnter=false; CommandLineReading=false; CheckCommand(); CommandLine=""; } MainGUI.CheckGUI(); for (int x=0; x<256; x++) Keys[x]=false; if (MainGUI.ItemList[3].ButtonPushed && ActualWorld) ActualWorld->CalculateParticles(); glColor4f(highblue); rectangle(-1,35,1025,35); glColor4f(1,1,1,1); PolyCounter=0; VertexCounter=0; ObjMemCounter=0; CallCounter=0; /*if (TextureList) { for (TEXTURE *t=TextureList; t; t=t->Next) { ObjMemCounter+=sizeof(TEXTURE); ObjMemCounter+=4*t->XRes*t->XRes*8; } } if (Scenes) { for (SCENE *s=Scenes; s; s=s->Next) { ObjMemCounter+=s->ObjectCapacity*sizeof(OBJECT); for (int x=0; x<s->ObjectNum; x++) { ObjMemCounter+=s->ObjectList[x].VertexCapacity*sizeof(VERTEX); for (int y=0; y<s->ObjectList[x].VertexNum; y++) ObjMemCounter+=s->ObjectList[x].VertexList[y].EdgeCapacity*4; ObjMemCounter+=s->ObjectList[x].PolygonCapacity*sizeof(POLYGON); ObjMemCounter+=s->ObjectList[x].EdgeCapacity*sizeof(EDGE); } } } if (Worlds) { for (WORLD *w=Worlds;w;w=w->Next) { ObjMemCounter+=w->ObjectCapacity*sizeof(WORLDOBJECT); for (int x=0; x<w->ObjectNum; x++) if (w->ObjectList[x].EmitterData) { ObjMemCounter+=sizeof(EMITTERDATA); if (w->ObjectList[x].EmitterData->Particles) ObjMemCounter+=(sizeof(PARTICLE))*w->ObjectList[x].EmitterData->MaxParticles; if (w->ObjectList[x].EmitterData->Particles && w->ObjectList[x].EmitterData->Tail) ObjMemCounter+=256*sizeof(VECTOR3)*w->ObjectList[x].EmitterData->MaxParticles; } } }*/ MainGUI.DrawGUI(); glErrorCheck(); if (!MainGUI.ItemList[3].ActiveBranch && !MainGUI.ItemList[0].ActiveBranch) { for (WORLD *a=Worlds;a;a=a->Next) for (int x=0; x<a->ObjectNum; x++) if (a->ObjectList[x].Primitive==aDDict_SPHEREEMITTER || a->ObjectList[x].Primitive==aDDict_CUBEEMITTER || a->ObjectList[x].Primitive==aDDict_CYLINDEREMITTER) a->ObjectList[x].EmitterData->LastFrameChecked=timeGetTime(); } glDisable(GL_DEPTH_TEST); if (strcmp(ErrorMessage,"") && !ReadLine) glColor4f(1,1,0.3f,1); glRasterPos2i(410,21); if (!CommandLineReading && !ReadLine) { if (!strcmp(ErrorMessage,"")) glPrint(ToolTip,base); else glPrint(ErrorMessage,base); } else { char *Buffer=new char [strlen(CommandLine)+100]; sprintf(Buffer,"%s_",CommandLine); glPrint(Buffer,base); delete Buffer; } if (strlen(MouseString) /*&& !strcmp(MouseString,"")*/) { glColor4f(0,0,0,1); glRasterPos2i(mx+16,my+12); glPrint(MouseString,base); glColor4f(1,1,1,1); glRasterPos2i(mx+15,my+11); glPrint(MouseString,base); } memset(MouseString,0,100); glColor4f(0,0,0,1); glRasterPos2i(mx+16,my); glPrint(ItemNameString,base); glColor4f(1,1,1,1); glRasterPos2i(mx+15,my-1); glPrint(ItemNameString,base); glEnable(GL_DEPTH_TEST); glColor4f(1,1,1,1); SetCursor(CurrentCursor); SwapBuffers(hDC); if (WaitLeftButton) { WaitLeftButtonRelease(); WaitLeftButton=false; } if (WaitMiddleButton) { WaitMiddleButtonRelease(); WaitMiddleButton=false; } if (WaitRightButton) { WaitRightButtonRelease(); WaitRightButton=false; } if (CommandLineEnter) { CommandLine=""; CommandLineEnter=false; ReadLine=false;} if (LeftButton || RightButton || MiddleButton) SetCapture(hWnd); else ReleaseCapture(); lastwheel=Wheel; Wheel=0; Sleep(10); } } }
void OpenGL::refresh(bool smooth, unsigned inwidth, unsigned inheight, unsigned outwidth, unsigned outheight) { while (glGetError() != GL_NO_ERROR); // clear possible error from who knows where #ifndef __NO_SHADERS if(shader_support && (fragmentshader || vertexshader)) { glUseProgram(glprogram); GLint location; float inputSize[2] = { (float)inwidth, (float)inheight }; location = glGetUniformLocation(glprogram, "rubyInputSize"); glUniform2fv(location, 1, inputSize); float outputSize[2] = { (float)outwidth, (float)outheight }; location = glGetUniformLocation(glprogram, "rubyOutputSize"); glUniform2fv(location, 1, outputSize); float textureSize[2] = { (float)iwidth, (float)iheight }; location = glGetUniformLocation(glprogram, "rubyTextureSize"); glUniform2fv(location, 1, textureSize); } #endif glErrorCheck(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, smooth ? GL_LINEAR : GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, smooth ? GL_LINEAR : GL_NEAREST); glErrorCheck(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, outwidth, 0, outheight, -1.0, 1.0); glViewport(0, 0, outwidth, outheight); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glErrorCheck(); glPixelStorei(GL_UNPACK_ROW_LENGTH, buffer_surface->getSurface()->pitch / buffer_surface->getSurface()->format->BytesPerPixel); glErrorCheck(); glTexSubImage2D(GL_TEXTURE_2D, /* mip-map level = */ 0, /* x = */ 0, /* y = */ 0, iwidth, iheight, GL_BGRA, iformat, buffer); //OpenGL projection sets 0,0 as *bottom-left* of screen. //therefore, below vertices flip image to support top-left source. //texture range = x1:0.0, y1:0.0, x2:1.0, y2:1.0 //vertex range = x1:0, y1:0, x2:width, y2:height double w = double(inwidth) / double(iwidth); double h = double(inheight) / double(iheight); int u = outwidth; int v = outheight; glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0, 0); glVertex3i(0, v, 0); glTexCoord2f(w, 0); glVertex3i(u, v, 0); glTexCoord2f(0, h); glVertex3i(0, 0, 0); glTexCoord2f(w, h); glVertex3i(u, 0, 0); glEnd(); glErrorCheck(); glFlush(); glErrorCheck(); #ifndef __NO_SHADERS if(shader_support) { glUseProgram(0); } #endif }