void cView::draw() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glLoadIdentity(); glPushMatrix(); useShader(shader); object(); glPopMatrix(); glPushMatrix(); useShader(shader_ground); ground(); glPopMatrix(); }
void PumpkinObject::setRenderState(TextureMap tex, ShaderMap shade) { // used again (also in Pyramid::loadTexture) to identify the texture to be used - must be called before submitting geometry batch // not needed with only one texture that was already loaded - but that will rarely be the case glBindTexture(GL_TEXTURE_2D, this->model->textureID[tex]); useShader(shade); };
// Runs into main thread static void particles_draw(particles_type *ps, float x, float y, float zoom) { if (!ps->alive || !ps->vertices || !ps->colors || !ps->texcoords) return; GLfloat *vertices = ps->vertices; GLfloat *colors = ps->colors; GLshort *texcoords = ps->texcoords; if (x < -10000) x = -10000; if (x > 10000) x = 10000; if (y < -10000) y = -10000; if (y > 10000) y = 10000; SDL_mutexP(ps->lock); if (ps->blend_mode == BLEND_ADDITIVE) glBlendFunc(GL_SRC_ALPHA,GL_ONE); if (multitexture_active) tglActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, ps->texture); if (multitexture_active && main_fbo) { tglActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, main_fbo->texture); } glTexCoordPointer(2, GL_SHORT, 0, texcoords); glColorPointer(4, GL_FLOAT, 0, colors); glVertexPointer(2, GL_FLOAT, 0, vertices); glTranslatef(x, y, 0); glPushMatrix(); glScalef(ps->zoom * zoom, ps->zoom * zoom, ps->zoom * zoom); glRotatef(ps->rotate, 0, 0, 1); if (ps->shader) useShader(ps->shader, 1, 1, main_fbo ? main_fbo->w : 1, main_fbo ? main_fbo->h : 1, 1, 1, 1, 1); int remaining = ps->batch_nb; while (remaining >= PARTICLES_PER_ARRAY) { glDrawArrays(GL_QUADS, remaining - PARTICLES_PER_ARRAY, PARTICLES_PER_ARRAY); remaining -= PARTICLES_PER_ARRAY; } if (remaining) glDrawArrays(GL_QUADS, 0, remaining); if (ps->shader) tglUseProgramObject(0); glPopMatrix(); glTranslatef(-x, -y, 0); if (ps->blend_mode) glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); if (multitexture_active && main_fbo) { tglActiveTexture(GL_TEXTURE0); } SDL_mutexV(ps->lock); }
void OpenGLWidget::initializeGL() { resolver = new Resolver(); r = resolver; connect(selector, SIGNAL(useShader(QString, QString)), this, SLOT(createFractal(QString, QString))); initializeOpenGLFunctions(); glClearColor(0.0f, 0.5f, 1.0f, 1.0f); m_vao.create(); QOpenGLBuffer m_vvbo(QOpenGLBuffer::VertexBuffer); QOpenGLBuffer m_ebo(QOpenGLBuffer::IndexBuffer); // Fill mode for polygons glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Shaders createFractal("mandelbrot", "Mandelbrot Set"); resizeGL(WINDOW_WIDTH, WINDOW_HEIGHT); paneButton = new QPushButton(this); paneButton->setGeometry(QRect(0, 0, 25, 25)); paneButton->setText("<->"); connect(paneButton, SIGNAL(pressed()), pane, SLOT(toggle())); paneButton->show(); animButton->raise(); animButton->setGeometry(QRect(WINDOW_WIDTH - 25, 0, 25, 25)); animButton->setText("<->"); connect(animButton, SIGNAL(pressed()), ::anim, SLOT(toggle())); animButton->show(); updateTimer = new QTimer(this); updateTimer->setInterval(1000.f / 1000.f); connect(updateTimer, SIGNAL(timeout()), this, SLOT(drawFrame())); updateTimer->start(); animLayout->addWidget(anim.buttonAdd); animLayout->addWidget(anim.buttonDelete); animLayout->addWidget(anim.checkBox); for (unsigned int i = 0; i < 1024; ++i) keys[i] = false; anim.origin = &origin; anim.zoom = &zoom; anim.maxIterations = &maxIterations; drawFrame(); }
void GenerateTextures(const uint8_t* operationBuffer) { uint32_t* destBuffer; uint32_t currIndex = operationBuffer[0]; uint32_t prevIndex = currIndex + 1; GLuint program = compileShader(g_operatorVertexShader, g_operatorFragmentShader); useShader(program); #ifndef NDEBUG int S = ((PFNGLGETUNIFORMLOCATIONPROC)GL_FUNC(get_uniform_location))(program, "S"); int F = ((PFNGLGETUNIFORMLOCATIONPROC)GL_FUNC(get_uniform_location))(program, "F"); #endif CreateTexture(prevIndex, TEXTURE_WIDTH, TEXTURE_HEIGHT, 4, GL_RGBA, TEXTURE_MEMORY(0)); checkGLError(); float params[3]; int slot = 0; // evaluate the array one operation function after another do { glViewport(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glEnable(GL_TEXTURE_2D); glDisable(GL_CULL_FACE); glDisable(GL_BLEND); glBindTexture(GL_TEXTURE_2D, prevIndex); checkGLError(); params[0] = operationBuffer[currIndex]; setFloatParams(0, params, 3); setIntParams(3, &slot, 1); glRecti(-1, -1, 1, 1); destBuffer = TEXTURE_MEMORY(currIndex); glReadPixels(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, destBuffer); CreateTexture(currIndex, TEXTURE_WIDTH, TEXTURE_HEIGHT, 4, GL_RGBA, destBuffer); //glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0); checkGLError(); prevIndex = currIndex; currIndex -= operationBuffer[currIndex] >> TEXTURE_OPERATION_MODE_SHIFT; } while(--currIndex); }
void drawText(const string* text, float x, float y, const string* font_name, int font_size) { text = (string*)wstrdup(text); HDC hdc = CreateCompatibleDC(0); string* font_face = wstrdup(font_name); int font_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72); HFONT hfont = CreateFontW(font_height, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH, font_face); free(font_face); SelectObject(hdc, hfont); SetBkMode(hdc, TRANSPARENT); SetTextColor(hdc, RGB(255, 0, 0)); RECT rect = { 0 }; DrawTextW(hdc, text, -1, &rect, DT_CALCRECT | DT_NOCLIP); int width = rect.right - rect.left + 2, height = rect.bottom - rect.top + 2; rect.left++; rect.top++; void* bitmap_data = calloc(width*height, sizeof(uint32_t)); for (int i = 3; i < width*height*4; i+= 4) *((uint8_t*)bitmap_data+i) = 0xff; HBITMAP hbitmap = CreateBitmap(width, height, 1, 32, bitmap_data); SelectObject(hdc, hbitmap); DrawTextW(hdc, text, -1, &rect, DT_TOP | DT_LEFT); DeleteObject(hfont); free(text); GetBitmapBits(hbitmap, width*height*4, bitmap_data); DeleteObject(hbitmap); DeleteDC(hdc); for (int i = 0; i < width*height*4; i += 4) { *((uint8_t*)bitmap_data+i+3) = *((uint8_t*)bitmap_data+i+2); *((uint8_t*)bitmap_data+i ) = 0x10; *((uint8_t*)bitmap_data+i+1) = 0x10; *((uint8_t*)bitmap_data+i+2) = 0x10; } textureT* tex = createTexture(); textureT* old_tex = useTexture(tex, 0); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, bitmap_data); free(bitmap_data); triMeshT* text_quad = createQuad(2.0f, 2.0f); if (!text_shader) initTextShader(); shaderT* old_shader = useShader(text_shader); setShaderParam("ScreenSize", &(vec2) { (float)screenWidth(), (float)screenHeight() }); setShaderParam("TextRect" , &(vec4) { (float)x, (float)y, (float)width, (float)height }); GLint depth_mask; glGetIntegerv(GL_DEPTH_WRITEMASK, &depth_mask); GLboolean cull_face, depth_test; glGetBooleanv(GL_CULL_FACE, &cull_face); glGetBooleanv(GL_DEPTH_TEST, &depth_test); glDepthMask(GL_FALSE); glDisable (GL_CULL_FACE); glDisable (GL_DEPTH_TEST); drawMesh(text_quad); glDepthMask(depth_mask); if (cull_face) glEnable(GL_CULL_FACE); if (depth_test) glEnable(GL_DEPTH_TEST); useTexture (old_tex, 0); useShader (old_shader); freeTexture(tex); freeMesh (text_quad); }
// Updates the contents of the root layer texture that fall inside the updateRect // and re-composits all sublayers. void LayerRendererChromium::drawLayers(const IntRect& updateRect, const IntRect& visibleRect, const IntRect& contentRect, const IntPoint& scrollPosition) { ASSERT(m_hardwareCompositing); if (!m_rootLayer) return; makeContextCurrent(); GLC(glBindTexture(GL_TEXTURE_2D, m_rootLayerTextureId)); // If the size of the visible area has changed then allocate a new texture // to store the contents of the root layer and adjust the projection matrix // and viewport. int visibleRectWidth = visibleRect.width(); int visibleRectHeight = visibleRect.height(); if (visibleRectWidth != m_rootLayerTextureWidth || visibleRectHeight != m_rootLayerTextureHeight) { m_rootLayerTextureWidth = visibleRect.width(); m_rootLayerTextureHeight = visibleRect.height(); m_projectionMatrix = orthoMatrix(0, visibleRectWidth, visibleRectHeight, 0, -1000, 1000); GLC(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_rootLayerTextureWidth, m_rootLayerTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0)); } // The GL viewport covers the entire visible area, including the scrollbars. GLC(glViewport(0, 0, visibleRectWidth, visibleRectHeight)); // Bind the common vertex attributes used for drawing all the layers. LayerChromium::prepareForDraw(layerSharedValues()); // FIXME: These calls can be made once, when the compositor context is initialized. GLC(glDisable(GL_DEPTH_TEST)); GLC(glDisable(GL_CULL_FACE)); GLC(glDepthFunc(GL_LEQUAL)); GLC(glClearStencil(0)); if (m_scrollPosition == IntPoint(-1, -1)) m_scrollPosition = scrollPosition; IntPoint scrollDelta = toPoint(scrollPosition - m_scrollPosition); // Scroll only when the updateRect contains pixels for the newly uncovered region to avoid flashing. if ((scrollDelta.x() && updateRect.width() >= abs(scrollDelta.x()) && updateRect.height() >= contentRect.height()) || (scrollDelta.y() && updateRect.height() >= abs(scrollDelta.y()) && updateRect.width() >= contentRect.width())) { // Scrolling works as follows: We render a quad with the current root layer contents // translated by the amount the page has scrolled since the last update and then read the // pixels of the content area (visible area excluding the scroll bars) back into the // root layer texture. The newly exposed area is subesquently filled as usual with // the contents of the updateRect. TransformationMatrix scrolledLayerMatrix; #if PLATFORM(SKIA) float scaleFactor = 1.0f; #elif PLATFORM(CG) // Because the contents of the OpenGL texture are inverted // vertically compared to the Skia backend, we need to move // the backing store in the opposite direction. float scaleFactor = -1.0f; #else #error "Need to implement for your platform." #endif scrolledLayerMatrix.translate3d(0.5 * visibleRect.width() - scrollDelta.x(), 0.5 * visibleRect.height() + scaleFactor * scrollDelta.y(), 0); scrolledLayerMatrix.scale3d(1, -1, 1); useShader(m_scrollShaderProgram); GLC(glUniform1i(m_scrollShaderSamplerLocation, 0)); LayerChromium::drawTexturedQuad(m_projectionMatrix, scrolledLayerMatrix, visibleRect.width(), visibleRect.height(), 1, m_scrollShaderMatrixLocation, -1); GLC(glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, contentRect.width(), contentRect.height())); m_scrollPosition = scrollPosition; } else if (abs(scrollDelta.y()) > contentRect.height() || abs(scrollDelta.x()) > contentRect.width()) { // Scrolling larger than the contentRect size does not preserve any of the pixels, so there is // no need to copy framebuffer pixels back into the texture. m_scrollPosition = scrollPosition; } // FIXME: The following check should go away when the compositor renders independently from its own thread. // Ignore a 1x1 update rect at (0, 0) as that's used a way to kick off a redraw for the compositor. if (!(!updateRect.x() && !updateRect.y() && updateRect.width() == 1 && updateRect.height() == 1)) { // Update the root layer texture. ASSERT((updateRect.x() + updateRect.width() <= m_rootLayerTextureWidth) && (updateRect.y() + updateRect.height() <= m_rootLayerTextureHeight)); #if PLATFORM(SKIA) // Get the contents of the updated rect. const SkBitmap bitmap = m_rootLayerCanvas->getDevice()->accessBitmap(false); int rootLayerWidth = bitmap.width(); int rootLayerHeight = bitmap.height(); ASSERT(rootLayerWidth == updateRect.width() && rootLayerHeight == updateRect.height()); void* pixels = bitmap.getPixels(); // Copy the contents of the updated rect to the root layer texture. GLC(glTexSubImage2D(GL_TEXTURE_2D, 0, updateRect.x(), updateRect.y(), updateRect.width(), updateRect.height(), GL_RGBA, GL_UNSIGNED_BYTE, pixels)); #elif PLATFORM(CG) // Get the contents of the updated rect. ASSERT(static_cast<int>(CGBitmapContextGetWidth(m_rootLayerCGContext.get())) == updateRect.width() && static_cast<int>(CGBitmapContextGetHeight(m_rootLayerCGContext.get())) == updateRect.height()); void* pixels = m_rootLayerBackingStore.data(); // Copy the contents of the updated rect to the root layer texture. // The origin is at the lower left in Core Graphics' coordinate system. We need to correct for this here. GLC(glTexSubImage2D(GL_TEXTURE_2D, 0, updateRect.x(), m_rootLayerTextureHeight - updateRect.y() - updateRect.height(), updateRect.width(), updateRect.height(), GL_RGBA, GL_UNSIGNED_BYTE, pixels)); #else #error "Need to implement for your platform." #endif } glClearColor(0, 0, 1, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Render the root layer using a quad that takes up the entire visible area of the window. // We reuse the shader program used by ContentLayerChromium. const ContentLayerChromium::SharedValues* contentLayerValues = contentLayerSharedValues(); useShader(contentLayerValues->contentShaderProgram()); GLC(glUniform1i(contentLayerValues->shaderSamplerLocation(), 0)); TransformationMatrix layerMatrix; layerMatrix.translate3d(visibleRect.width() * 0.5f, visibleRect.height() * 0.5f, 0); LayerChromium::drawTexturedQuad(m_projectionMatrix, layerMatrix, visibleRect.width(), visibleRect.height(), 1, contentLayerValues->shaderMatrixLocation(), contentLayerValues->shaderAlphaLocation()); // If culling is enabled then we will cull the backface. GLC(glCullFace(GL_BACK)); // The orthographic projection is setup such that Y starts at zero and // increases going down the page so we need to adjust the winding order of // front facing triangles. GLC(glFrontFace(GL_CW)); // The shader used to render layers returns pre-multiplied alpha colors // so we need to send the blending mode appropriately. GLC(glEnable(GL_BLEND)); GLC(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)); // Translate all the composited layers by the scroll position. TransformationMatrix matrix; matrix.translate3d(-m_scrollPosition.x(), -m_scrollPosition.y(), 0); // Traverse the layer tree and update the layer transforms. float opacity = 1; const Vector<RefPtr<LayerChromium> >& sublayers = m_rootLayer->getSublayers(); size_t i; for (i = 0; i < sublayers.size(); i++) updateLayersRecursive(sublayers[i].get(), matrix, opacity); m_rootVisibleRect = visibleRect; // Enable scissoring to avoid rendering composited layers over the scrollbars. GLC(glEnable(GL_SCISSOR_TEST)); FloatRect scissorRect(contentRect); // The scissorRect should not include the scroll offset. scissorRect.move(-m_scrollPosition.x(), -m_scrollPosition.y()); scissorToRect(scissorRect); // Clear the stencil buffer to 0. GLC(glClear(GL_STENCIL_BUFFER_BIT)); // Disable writes to the stencil buffer. GLC(glStencilMask(0)); // Traverse the layer tree one more time to draw the layers. for (i = 0; i < sublayers.size(); i++) drawLayersRecursive(sublayers[i].get(), scissorRect); GLC(glDisable(GL_SCISSOR_TEST)); m_gles2Context->swapBuffers(); m_needsDisplay = false; }
int main(){ //create widnow and context glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //using open gl 3.3 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//idk what this does glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); GLFWwindow * window = glfwCreateWindow(m_screenWidth, m_screenHeight, "OPEN GL LIGHTS", nullptr, nullptr); if(window == nullptr){ std::cout << "failed to create GLFW window"<<std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK){ std::cout<< "failed to init GLEW"<<std::endl; } glViewport(0,0, m_screenWidth, m_screenHeight); glEnable(GL_DEPTH_TEST); glfwSetKeyCallback(window, key_callback); glfwSetCursorPosCallback(window, mouse_callback); //shader init Shader ourShader( "vertex_shader.vertex", "fragmentShader.fragment"); Shader lampShader( "lamp_vertex_shader.vertex", "lamp_fragment_shader.fragment"); //view spaces m_camera = Camera(); projection = glm::perspective(m_aspect, m_screenWidth/m_screenHeight, 0.1f, 100.0f); //create cube data GLuint VAO;//remember to delete these GLuint VBO; createCube(&VAO, &VBO); GLuint lightVAO; glGenVertexArrays(1, &lightVAO); glBindVertexArray(lightVAO); // We only need to bind to the VBO, the container's VBO's data already contains the correct data. glBindBuffer(GL_ARRAY_BUFFER, VBO); // Set the vertex attributes (only position data for our lamp) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), (GLvoid*)0);//pos glEnableVertexAttribArray(0); glBindVertexArray(0); //game loop while(!glfwWindowShouldClose(window)){ glfwPollEvents(); glClearColor(0.2f, 0.2f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLfloat currentFrame = glfwGetTime(); m_deltaTime = currentFrame - m_lastFrame; m_lastFrame = currentFrame; doMovement(); model = glm::mat4(); useShader(ourShader); GLint viewPosLoc = glGetUniformLocation(*ourShader.getProgram(), "viewPos"); glUniform3f(viewPosLoc, m_camera.getPosition().x, m_camera.getPosition().y, m_camera.getPosition().z); GLint lightPosLoc = glGetUniformLocation(*ourShader.getProgram(), "lightPos"); glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z); GLint objectColorLoc = glGetUniformLocation(*ourShader.getProgram(), "objectColor"); GLint lightColorLoc = glGetUniformLocation(*ourShader.getProgram(), "lightColor"); glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f); glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f); glBindVertexArray(VAO);//use cube data glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0);//stop using cube data model = glm::mat4(); //model = glm::rotate(model, currentFrame * 50.0f, glm::vec3(0.0f, 0.0f, 1.0f)); lightPos.x = 1.0f + sin(glfwGetTime()) * 2.0f; lightPos.y = sin(glfwGetTime() / 2.0f) * 1.0f; model = glm::translate(model, lightPos); model = glm::scale(model, glm::vec3(0.2f)); useShader(lampShader); glBindVertexArray(lightVAO);//use lamp data glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0);//stop using lamp data glfwSwapBuffers(window); } glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glfwTerminate(); return 0; }