void Renderer::drawQuad(Shader *shader, vec4 color, GLuint texId) { if (shader == 0) shader = quadShader; if (!bindShader(shader)) return; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texId); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); float time = (timeGetTime() - startTime) / 10000.0f; float h = glutGet(GLUT_WINDOW_HEIGHT); float w = glutGet(GLUT_WINDOW_WIDTH); shader->setUniform1f("time", time); shader->setUniform2f("resolution", vec2(w, h)); shader->setUniform4fv("color", 1, (float*)&color); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboQuad); GLuint uniform_pos = glGetAttribLocation(shader->shaderProgram, "position"); glEnableVertexAttribArrayARB(uniform_pos); glVertexAttribPointerARB(uniform_pos, 2, GL_FLOAT, GL_FALSE, sizeof(vec2), 0); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); /*char c[200]; sprintf_s(c, "%f", time); render_text(0, c, 0, 12, vec4(1, 1, 1, 10));*/ }
void Scene::loadShader() { std::string sourcePtr = read("vshader.cg"); auto optimal = cgD3D11GetOptimalOptions(mCgVertexShaderProfile); mVertexShaderId = cgCreateProgram(mCgContext, CG_SOURCE, sourcePtr.c_str(), mCgVertexShaderProfile, "main", optimal); if(mVertexShaderId != nullptr) { optimal = cgD3D11GetOptimalOptions(mCgFragmentShaderProfile); HRESULT res = cgD3D11LoadProgram(mVertexShaderId, NULL); CGerror error; const char *errorString = cgGetLastErrorString(&error); sourcePtr = read("fshader.cg"); mFragmentShaderId = cgCreateProgram(mCgContext, CG_SOURCE, sourcePtr.c_str(), mCgFragmentShaderProfile, "main", optimal); errorString = cgGetLastErrorString(&error); res = cgD3D11LoadProgram(mFragmentShaderId, NULL); } // here the uniform can be set CGparameter location = cgGetNamedParameter(mVertexShaderId, "ambient"); checkForCgError("could not get uniform color location", mCgContext, false); cgSetParameter4fv(location, glm::value_ptr(glm::vec4(1.0,1.0,1.0,1.0))); checkForCgError("could not set uniform color", mCgContext, false); bindShader(mVertexShaderId, mFragmentShaderId); }
void Renderer::flushText() { if (!currentFont) return; if (!bindShader(textShader)) return; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, currentFont->tex); 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_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo); glEnableVertexAttribArrayARB(uniform_coord); glVertexAttribPointerARB(uniform_coord, 4, GL_FLOAT, GL_FALSE, sizeof(TextVertex), 0); glEnableVertexAttribArrayARB(uniform_atr); glVertexAttribPointerARB(uniform_atr, 4, GL_FLOAT, GL_FALSE, sizeof(TextVertex), (const void *)(offsetof(TextVertex, c))); float* ptr = (float*)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); if (ptr) { memcpy(ptr, textVertexBuffer, sizeof(TextVertex)* currentTextVertexBufferPos); glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); glDrawArrays(GL_TRIANGLES, 0, currentTextVertexBufferPos); } currentTextVertexBufferPos = 0; }
void ShaderLibrary::bindNormalMap(GLfloat near0, GLfloat far0) { bindShader("Normal Map"); s_normalBuffer->bind(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); setUniformVariable("Normal Map", "Near", near0); setUniformVariable("Normal Map", "Far", far0); }
void ShaderLibrary::loadPreferences() { QString defaultShader(Preferences::DefaultShader()); if (defaultShader.isEmpty()) defaultShader = NoShader; QVariantMap defaultShaderParameters(Preferences::DefaultShaderParameters()); setUniformVariables(defaultShader, defaultShaderParameters); bindShader(defaultShader); }
void Raycaster::glRenderAction(GLContextData& contextData) const { /* Get the OpenGL-dependent application data from the GLContextData object: */ DataItem* dataItem=contextData.retrieveDataItem<DataItem>(this); /* Save OpenGL state: */ glPushAttrib(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ENABLE_BIT|GL_LIGHTING_BIT|GL_POLYGON_BIT); /* Initialize the ray termination depth frame buffer: */ dataItem->initDepthBuffer(); /* Bind the ray termination framebuffer: */ GLint currentFramebuffer; glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT,¤tFramebuffer); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,dataItem->depthFramebufferID); /* Get the projection and modelview matrices: */ PTransform mv=glGetModelviewMatrix<Scalar>(); PTransform pmv=glGetProjectionMatrix<Scalar>(); pmv*=mv; /* Clip the render domain against the view frustum's front plane and all clipping planes: */ Polyhedron<Scalar>* clippedDomain=clipDomain(pmv,mv); /* Draw the clipped domain's back faces to the depth buffer as ray termination conditions: */ glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); clippedDomain->drawFaces(); /* Unbind the depth framebuffer: */ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,currentFramebuffer); /* Install the GLSL shader program: */ dataItem->shader.useProgram(); bindShader(pmv,mv,dataItem); /* Draw the clipped domain's front faces: */ glEnable(GL_BLEND); glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA); glDepthMask(GL_FALSE); glCullFace(GL_BACK); if(clippedDomain!=0) clippedDomain->drawFaces(); /* Uninstall the GLSL shader program: */ unbindShader(dataItem); GLShader::disablePrograms(); /* Clean up: */ delete clippedDomain; /* Restore OpenGL state: */ glPopAttrib(); }
void TextureShader::compile() { Shader::compile(); bindShader(); texCoordAttributeHandle = glGetAttribLocation(program, "a_texCoord"); if (texCoordAttributeHandle < 0) { LOGE("*** Couldn't get shader's a_texCoord location."); ABORT_GAME; } samplerHandle = glGetUniformLocation(program, "s_texture"); if (samplerHandle < 0) { LOGE("*** Couldn't get shader's s_texture location."); ABORT_GAME; } unbindShader(); }
void ShaderState::bind() { bindShader(); bindVertexShader(); bindFragmentShader(); bindProgram(); typedef ShaderState C; class_<C, ShaderStatePtr, bases<State>, boost::noncopyable> ("ShaderState", no_init) .def(init< optional<ProgramPtr> >()) .def("isSupported", &C::isSupported) .staticmethod("isSupported") .def("setUniform4f", &C::setUniform4f) ; implicitly_convertible<ShaderStatePtr, StatePtr>(); }
void gl4::DeferredRender::bindDefaultShader() { bindShader(deferred1); }
void Scene::render() { //Keep the cubes rotating mCubeRotation += .05f; if(mCubeRotation > 1.0f) mCubeRotation = 0.0f; ////Reset cube1World //mCube1World = glm::mat4(); ////Define cube1's world space matrix //mRotation = glm::cross(mRotation, glm::angleAxis(mCubeRotation * 180.f / 3.14f, glm::vec3(0,1,0))); //glm::mat4 translation = glm::translate( 0.0f, 0.0f, 4.0f ); ////Set cube1's world space using the transformations //mCube1World = translation * glm::toMat4(mRotation); ////Reset cube2World //mCube1World = glm::mat4(); ////Define cube2's world space matrix //mRotation = glm::cross(-mRotation, glm::angleAxis(mCubeRotation * 180.f / 3.14f, glm::vec3(0,1,0))); //glm::mat4 scale = glm::scale( 1.3f, 1.3f, 1.3f ); ////Set cube2's world space matrix //mCube2World = glm::toMat4(mRotation) * scale; //glm::vec4 camPos = glm::vec4( 0.0f, 3.0f, -8.0f, 0.0f ); //glm::mat4 camProjection = glm::perspective(70.f, mWidth / (float)mHeight, 0.1f, 1000.0f); //glm::mat4 camView = glm::translate(glm::vec3(0,0,-3.5)); //glm::mat4 WVP = camProjection * camView * mCube1World; // //CGparameter location = cgGetNamedParameter(mVertexShaderId, "matrices.WVP"); //cgSetMatrixParameterfc(location, glm::value_ptr(WVP)); // do 3D rendering on the back buffer here //UINT stride = sizeof(VERTEX); //UINT offset = 0; //mDeviceContext->Draw(4, 0); unsigned int strides[2] = { sizeof(glm::vec3), sizeof(glm::vec4) }; unsigned int offsets[2] = { 0, 0 }; //dev->IASetVertexBuffers(0, 2, &pVBuffer, strides, offsets ); //mDeviceContext->IASetInputLayout(mVboLayout); // this needs to be done to really set it //mDeviceContext->IASetVertexBuffers(0, 2, mVbo, strides, offsets); //mDeviceContext->IASetIndexBuffer( mIbo, DXGI_FORMAT_R16_UINT, 0 ); //mDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); //mDeviceContext->DrawIndexed(36, 0, 0); // GETTING A LOCATION CAN BE DONE WHEN A SHADER IS BOUND CGparameter location2 = cgGetNamedParameter(mVertexShaderId, "ambient"); cgD3D11UnbindProgram(mVertexShaderId); cgD3D11UnbindProgram(mFragmentShaderId); // We can only set the uniform if the shader is NOT BOUND! // In OpenGL we have to check that! // FOUND OUT: a location can be retrieved and set to a value when a shader is not bound! // BUT SETTING IT's VALUE CAN ONLY BE DONE WHEN IT's UNBOUND! cgSetParameter4fv(location2, glm::value_ptr(glm::vec4(0.5,mCubeRotation,1.0,1.0))); checkForCgError("could not set uniform color", mCgContext, false); bindShader(mVertexShaderId, mFragmentShaderId); mDeviceContext->IASetInputLayout(mVboLayout); // this needs to be done to really set it mDeviceContext->IASetVertexBuffers(0, 2, mPlaneVbo, strides, offsets); mDeviceContext->IASetIndexBuffer( mPlaneIbo, DXGI_FORMAT_R32_UINT, 0 ); mDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); mDeviceContext->DrawIndexed(6, 0, 0); }
void ShaderLibrary::generateFilters() { GLuint texId(s_normalBuffer->texture()); QSize size(s_normalBuffer->size()); Texture texture; texture.id = texId; texture.size = size; texture.slot = NormalBuffer; texture.data = 0; setTextureVariable("Filters", "NormalMap", texture); s_filterBuffer->bind(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); bindShader("Filters"); texture.id = s_normalBuffer->texture(); texture.size = size; texture.slot = (Texture_t)0; texture.data = 0; setTextureVariable("Filters", "NormalMap", texture); texture.id = s_rotationTextureId; texture.size = QSize(s_rotationTextureSize, s_rotationTextureSize); texture.slot = (Texture_t)1; texture.data = s_rotationTextureData; setTextureVariable("Filters", "RotationTexture", texture); //bindTextures("Filters"); /* glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texId); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, s_rotationTextureId); */ float w(size.width()); float h(size.height()); float z(0.0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(z, w, z, h, -1, 1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glBegin(GL_QUADS); glTexCoord2f(1.0f,0.0f); glVertex3f(w, z, z); glTexCoord2f(0.0f,0.0f); glVertex3f(z, z, z); glTexCoord2f(0.0f,1.0f); glVertex3f(z, h, z); glTexCoord2f(1.0f,1.0f); glVertex3f(w, h, z); glEnd(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); s_filterBuffer->release(); releaseTextures(); }
bool ShaderLibrary::resume() { return bindShader(s_currentShader); }