void window_render_func(void) { ++frame_count; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // const GLfloat bg_color[] = { 0.0f, 0.0f, 0.0f, 1.0f }; // glClearBufferfv(GL_COLOR, 0, bg_color); glUseProgram(rendering_program); GLfloat attrib[] = { 0.0f, 0.0f, 0.0f, 0.0f }; GLfloat color[] = {0.0f, 0.6f, 0.8f, 1.0f}; glVertexAttrib4fv(0, attrib); glVertexAttrib4fv(1, color); // glPointSize(5.0f); // glPatchParameteri(GL_PATCH_VERTICES, 3); // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // glDrawArrays(GL_PATCHES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3); glutSwapBuffers(); }
void Sample::render() { // Clear the color buffer. glClear(GL_COLOR_BUFFER_BIT); _program->use(); const GLfloat positions[] = { -0.5f, -0.5f, 0.0f, +0.5f, -0.5f, 0.0f, +0.0f, +0.5f, 0.0f }; const GLfloat color[] = { 1.0f, 0.0f, 0.0f, 1.0f }; glEnableVertexAttribArray(kPositionIndex); glVertexAttribPointer(kPositionIndex, 3, GL_FLOAT, GL_FALSE, 0, positions); glDisableVertexAttribArray(kColorIndex); glVertexAttrib4fv(kColorIndex, color); glDrawArrays(GL_TRIANGLES, 0, 3); glFlush(); }
/// // Draw a triangle using the program object created in Init() // void Draw ( ESContext *esContext ) { UserData *userData = esContext->userData; GLfloat color[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; // 3 vertices, with (x,y,z) per-vertex GLfloat vertexPos[3 * 3] = { 0.0f, 0.5f, 0.0f, // v0 -0.5f, -0.5f, 0.0f, // v1 0.5f, -0.5f, 0.0f // v2 }; glViewport ( 0, 0, esContext->width, esContext->height ); glClear ( GL_COLOR_BUFFER_BIT ); glUseProgram ( userData->programObject ); glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vertexPos ); glEnableVertexAttribArray ( 0 ); glVertexAttrib4fv ( 1, color ); glDrawArrays ( GL_TRIANGLES, 0, 3 ); glDisableVertexAttribArray ( 1 ); }
void Shader::use(bool forceReload) { static Shader *previousShader = NULL; if (this == previousShader && !forceReload) return; previousShader = this; glUseProgram(*_shaderNo); for (uint32 i = 0; i < _attributes.size(); ++i) { Graphics::VertexAttrib &attrib = _attributes[i]; if (attrib._enabled) { glEnableVertexAttribArray(i); glBindBuffer(GL_ARRAY_BUFFER, attrib._vbo); glVertexAttribPointer(i, attrib._size, attrib._type, attrib._normalized, attrib._stride, (const GLvoid *)attrib._offset); } else { glDisableVertexAttribArray(i); switch (attrib._size) { case 2: glVertexAttrib2fv(i, attrib._const); break; case 3: glVertexAttrib3fv(i, attrib._const); break; case 4: glVertexAttrib4fv(i, attrib._const); break; } } } }
//-------------------------------------------------------- void CShaderNode::draw() { CC_NODE_DRAW_SETUP(); //传递uniform变量 CCGLProgram* shader = getShaderProgram(); shader->setUniformLocationWith2f(m_unUniformResolution, m_tagResolutionPos.x, m_tagResolutionPos.y); shader->setUniformLocationWith1i(m_unUniformTex0, 0); glUniform1f(m_unUniformTime, m_fTime); //获取attribute变量 CCSize size = this->getContentSize(); float w = size.width; float h = size.height; ccGLBindTexture2D(m_pTexture); //绑定纹理到槽位 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, w, h, 0); //截取屏幕数据到纹理 glEnableVertexAttribArray(m_unAttributePosition); //传递attribute变量 GLfloat vertices[12] = { 0, 0, //左下0 w, 0, //右下1 w, h, //右上2 0, 0, //左下0 0, h, //左上3 w, h, //右上2 }; glVertexAttribPointer(m_unAttributePosition, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttrib4fv(m_unAttributeColor, m_fColor); //绘制 glDrawArrays(GL_TRIANGLES, 0, 6); }
void VectorIcon::DrawInRect (const Rect& rect, int align, const float* color_ptr, short gamma, float rotate, bool scale) const { int x = rect.left(); int y = rect.bottom(); if (align & AlignLeft) { x = rect.left() + size().width() / 2; } else if (align & AlignRight) { x = rect.right() - size().width() / 2; } else if (align & AlignHorizontalCenter) { x = rect.hcenter(); } if (align & AlignTop) { y = rect.top() - size().height() / 2; } else if (align & AlignBottom) { y = rect.bottom() + size().height() / 2; } else if (align & AlignVerticalCenter) { y = rect.vcenter(); } AbstractWindow::shaders()->widget_triangle_program()->use(); glVertexAttrib4fv(AttributeColor, color_ptr); glUniform2f( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_POSITION), x, y); glUniform1i( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_GAMMA), gamma); glUniform1i( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_ANTI_ALIAS), 1); glUniform1f( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_ROTATION), rotate); if (scale) { float scale_x = rect.width() * 1.f / size().width(); float scale_y = rect.height() * 1.f / size().height(); glUniform2f( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_SCALE), scale_x, scale_y); } else { glUniform2f( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_SCALE), 1.f, 1.f); } glBindVertexArray(vao_); glDrawElements(GL_TRIANGLES, elements_, GL_UNSIGNED_INT, BUFFER_OFFSET(0)); }
void render(double currentTime) { static const GLfloat backgroundColor[] = {0.5f, 0.0f, 0.0f, 1.0f}; glClearBufferfv(GL_COLOR, 0, backgroundColor); // Use the program object we created earlier for rendering; glUseProgram(rendering_program); float t = static_cast<float>(currentTime); GLfloat offset[] = {std::sin(t) * 0.5f, std::cos(t) * 0.6f, 0.0f, 0.0f}; GLfloat color[] = {std::sin(t) * 0.5f, std::cos(t) * 0.5f, 0.0f, 1.0f}; // Update the value of input attributes glVertexAttrib4fv(0, offset); glVertexAttrib4fv(1, color); // Draw one triangle glDrawArrays(GL_TRIANGLES, 0, 3); }
// should be moved to render justgoingtothrowthatoutthere // render is in app instead of common currently, so that would have to be changed void RenderMesh(Camera* pCamera, Shader* pShader, Mesh* pMesh, const Vec4f& position, const Mat4f& orientation) { if(pShader == NULL) return; // should go away when we sort if(pMesh == NULL) return; // should go away when we sort pShader->StartUsing(); pShader->SetPosition(&position); pShader->SetOrientation(&orientation); pShader->SetCameraParams(pCamera); GLuint colorHandle = pShader->GetColorHandle(); // this is getting ugly to look at in a hurry // need to do buffers soon int numTris = pMesh->getNumberTriangles(); int startTriangle = 0; int endTriangle = numTris; glBegin(GL_TRIANGLES); Vec4f a, b, c; Vec4f colorA, colorB, colorC; //int colorIndex = 0; for (int t = startTriangle; t < endTriangle && t < numTris; t++) { pMesh->getTriangle(t, a, b, c); if(colorHandle != -1) { pMesh->getColors(t, colorA, colorB, colorC); glVertexAttrib4fv(colorHandle, colorA.raw()); } glVertex4fv(a.raw()); if(colorHandle != -1) { glVertexAttrib4fv(colorHandle, colorB.raw()); } glVertex4fv(b.raw()); if(colorHandle != -1) { glVertexAttrib4fv(colorHandle, colorC.raw()); } glVertex4fv(c.raw()); //if ((t+1) % 2 == 0) { // colorIndex = (colorIndex + 1) % colorArray.size(); //} } glEnd(); pShader->StopUsing(); }
bool ShaderManager :: SetAttributeVector ( const char * name, const Vector4D& value ) { int location = glGetAttribLocation ( Program, name ); if ( location < 0 ) return false; glVertexAttrib4fv ( location, value ); return true; }
void render(float time) { const GLfloat backgroundColor[] = { (float)sin(time) * 0.5f + 0.5f, (float)cos(time) * 0.5f + 0.5f, 0.0f, 1.0f }; glClearBufferfv(GL_COLOR, 0, backgroundColor); GLfloat offset[] = { (float)sin(time) * 0.5f, (float)cos(time) * 0.6f, 0.0f, 0.0f }; GLfloat triangleColor[] = { (float)cos(time) * 0.7f + 0.5f, (float)sin(time) * 0.7f + 0.5f, 0.0f, 1.0f }; glVertexAttrib4fv(1, triangleColor); glVertexAttrib4fv(2, offset); glDrawArrays(GL_PATCHES, 0, 3); }
virtual void render(double currentTime) { static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f }; glClearBufferfv(GL_COLOR, 0, green); glUseProgram(program); GLfloat attrib[] = { (float)sin(currentTime) * 0.5f, (float)cos(currentTime) * 0.5f, 0.0f, 0.0f }; glVertexAttrib4fv(0, attrib); glDrawArrays(GL_TRIANGLES, 0, 3); }
void Shader::SetAttribute(const char* var, float* v, int size) { int id = GetAttributeId(var); if(id >= 0) { if(size == 2) glVertexAttrib2fv(id, v); else if(size == 3) glVertexAttrib3fv(id, v); else if(size == 4) glVertexAttrib4fv(id, v); } }
void display(void) { /*clear all pixels*/ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Take care of any mouse rotations or panning mv = LookAt(vec4(0, 0, 10+z_distance, 1.0), vec4(0, 0, 0, 1.0), vec4(0, 1, 0, 0.0)); mv = mv * RotateX(view_rotx) * RotateY(view_roty) * RotateZ(view_rotz); glUniformMatrix4fv(model_view, 1, GL_TRUE, mv); //You need to send some vertex attributes and uniform variables here glUniform4fv(ambient_light, 1, vec4(0.2, 0.2, 0.2, 1)); glUniform4fv(light_color, 1, vec4(1, 1, 1, 1)); glUniform4fv(light_position, 1, mv*vec4(50, 50, 50, 1)); // Light will follow the object (Because of mv) glVertexAttrib4fv(vAmbientDiffuseColor, vec4(0, 0.5, 0, 1)); glVertexAttrib4fv(vSpecularColor, vec4(1, 1, 1, 1)); glVertexAttrib1f(vSpecularExponent, 10); if(mode == 0){ glBindVertexArray( vao[0] ); glDrawArrays( GL_TRIANGLES, 0, spherevertcount ); // draw the sphere }else{ //WARNING: //Nvidia kludge: If you want the teapot to show up correctly, make sure that //vPosition is the first attribute listed in your vertex shader, and vNormal //is the 3rd attribute listed. Not sure what ATI cards will do with this glBindVertexArray(0); glutSolidTeapot(2); //not very bandwidth efficient } glFlush(); /*start processing buffered OpenGL routines*/ glutSwapBuffers(); }
void MainWindow::paintGL() { glUseProgram(thisData->program); glBindVertexArray(thisData->vao); glClearColor(0.1f,0.6f,0.3f,1); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); auto rand_data=[]() ->GLfloat{ GLfloat ans=std::sin((rand()%123'456)*(1/1'000.0f)); return ((rand()&1)? ans:(-ans))*0.5f; }; const GLfloat offset[]{ rand_data(),rand_data(),0,0 }; glVertexAttrib4fv(0,offset); glDrawArrays(GL_TRIANGLES,0,3); }
enum piglit_result piglit_display(void) { float color[4][4] = { {1, 0, 0, 1}, {0, 1, 0, 1}, {0, 0, 1, 1}, {1, 1, 1, 1}, }; bool pass = true; glViewport(0, 0, piglit_width, piglit_height); glClearColor(0.5, 0.5, 0.5, 0.5); glClear(GL_COLOR_BUFFER_BIT); glVertexAttrib1fv(attr, color[0]); glViewport(0, 0, piglit_width/2, piglit_height/2); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glVertexAttrib2fv(attr, color[1]); glViewport(0, piglit_height/2, piglit_width/2, piglit_height/2); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glVertexAttrib3fv(attr, color[2]); glViewport(piglit_width/2, 0, piglit_width/2, piglit_height/2); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glVertexAttrib4fv(attr, color[3]); glViewport(piglit_width/2, piglit_height/2, piglit_width/2, piglit_height/2); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); pass &= piglit_probe_rect_rgba(0, 0, piglit_width / 2, piglit_height / 2, color[0]); pass &= piglit_probe_rect_rgba(0, piglit_height / 2, piglit_width / 2, piglit_height / 2, color[1]); pass &= piglit_probe_rect_rgba(piglit_width / 2, 0, piglit_width / 2, piglit_height / 2, color[2]); pass &= piglit_probe_rect_rgba(piglit_width / 2, piglit_height / 2, piglit_width / 2, piglit_height / 2, color[3]); piglit_present_results(); return pass ? PIGLIT_PASS : PIGLIT_FAIL; }
virtual void render(double currentTime) override { static const GLfloat red[] = { 0.0f, 0.0f, 0.0f, 1.0f }; glClearBufferfv(GL_COLOR, 0, red); glUseProgram(m_program); GLfloat attrib[] = {(float)sin(currentTime) * 0.5f, (float)cos(currentTime) * 0.5f, 0.f, 0.f, }; glVertexAttrib4fv(0, attrib); //fragmentAttrib와 전달하는 값은 같지만, 인터페이스 블록을 이용하여 값을 묶어서 전달한다. const GLfloat color[] = {1.0, 0.8, 1.0, 1.0}; glVertexAttrib4fv(1, color); // 드로잉을 요청한다. // @param mode ex) GL_TRIANGLES, GL_LINES, ... // @param first 포인터 // @param count 버텍스의 갯수 glDrawArrays(GL_TRIANGLES, 0, 3); };
virtual void render (double currentTime) { const GLfloat clearBuffer [] = { 0.0f , 0.2f , 0.0f , 1.0f}; glClearBufferfv(GL_COLOR,0,clearBuffer); glUseProgram(shaderProgram); GLfloat attrib[] = { ( float ) sin(currentTime) * 0.5f + 0.5f, (float) cos(currentTime) * 0.5f + 0.5f, 0.0f, 1.0f }; glVertexAttrib4fv ( 0 , attrib ); glDrawArrays(GL_TRIANGLES,0,3); } // end of rend
void AttributeVariable::set(GLfloat * fv_, int param_, int count_) { switch(param_) { case 1: { glVertexAttrib1fv(_location, fv_); } break; case 2: { glVertexAttrib2fv(_location, fv_); } break; case 3: { glVertexAttrib3fv(_location, fv_); } break; case 4: { glVertexAttrib4fv(_location, fv_); } break; default: break; } }
static void setup_generic_const_attribute (CoglContext *context, CoglPipeline *pipeline, CoglAttribute *attribute) { int name_index = attribute->name_state->name_index; int attrib_location = _cogl_pipeline_progend_glsl_get_attrib_location (pipeline, name_index); int columns; int i; if (attrib_location == -1) return; if (attribute->d.constant.boxed.type == COGL_BOXED_MATRIX) columns = attribute->d.constant.boxed.size; else columns = 1; /* Note: it's ok to access a COGL_BOXED_FLOAT as a matrix with only * one column... */ switch (attribute->d.constant.boxed.size) { case 1: GE( context, glVertexAttrib1fv (attrib_location, attribute->d.constant.boxed.v.matrix)); break; case 2: for (i = 0; i < columns; i++) GE( context, glVertexAttrib2fv (attrib_location + i, attribute->d.constant.boxed.v.matrix)); break; case 3: for (i = 0; i < columns; i++) GE( context, glVertexAttrib3fv (attrib_location + i, attribute->d.constant.boxed.v.matrix)); break; case 4: for (i = 0; i < columns; i++) GE( context, glVertexAttrib4fv (attrib_location + i, attribute->d.constant.boxed.v.matrix)); break; default: g_warn_if_reached (); } }
void StateSystem::VertexImmediateState::applyGL(GLbitfield changed) const { for (GLuint i = 0; i < MAX_VERTEXATTRIBS; i++){ if (!isBitSet(changed,i)) continue; switch(data[i].mode){ case VERTEXMODE_FLOAT: glVertexAttrib4fv(i,data[i].floats); break; case VERTEXMODE_INT: glVertexAttribI4iv(i,data[i].ints); break; case VERTEXMODE_UINT: glVertexAttribI4uiv(i,data[i].uints); break; } } }
/* Render the mesh at the given frame and frame interpolator (0-1 value) */ void MeshRenderer::render() { const char* name = pMesh->name.c_str(); glBindVertexArray(vao); glActiveTexture(GL_TEXTURE0); // Fill the bones texture with the bone transforms at the current frame. Data is obtained by calling cluster->getInterpolatedTransforms. We currently support up to 120 clusters. // The arrays are mat4s in row major form. glBindBuffer(GL_TEXTURE_BUFFER, bonesBuffer); typedef vector<Cluster> ClusterList; ClusterList& clusters = pMesh->clusters; float* boneTransforms = new float[120 * 16]; memset(boneTransforms, 0, 120 * sizeof(float) * 16); float* boneTransformsStart = boneTransforms; const Matrix44<float>& globalCurrentTransform = pMesh->getGlobalCurrentTransform(); for (ClusterList::iterator it = clusters.begin(); it != clusters.end(); it++) { Cluster &cluster = *it; // Obtain the bone to world transform for the current frame const Matrix44<float>& currentBoneTransform = Animator::get()->getTransform(cluster.animatorTransformID); // Multiply this by the mesh to bone transform Matrix44<float> clusterTransform = currentBoneTransform * cluster.meshToBoneTransform; // Transform the bone's transform by the inverse of the mesh's transform to account for scaling etc of the mesh. clusterTransform = globalCurrentTransform * clusterTransform; memcpy(boneTransformsStart, &clusterTransform, sizeof(float)* 16); boneTransformsStart += 16; } glBufferSubData(GL_TEXTURE_BUFFER, 0, 120 * sizeof(float) * 16, boneTransforms); glBindBuffer(GL_TEXTURE_BUFFER, 0); glBindTexture(GL_TEXTURE_BUFFER, bonesTexture); glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, bonesBuffer); glVertexAttrib4fv(2, color); glBeginTransformFeedback(GL_TRIANGLES); glDrawElements(GL_TRIANGLES, pMesh->lIndices.size(), GL_UNSIGNED_SHORT, 0); glEndTransformFeedback(); vmath::vec4* verts = (vmath::vec4*)glMapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, GL_READ_ONLY); //vmath::uvec4 vertArray[5000]; //memcpy(vertArray, verts, sizeof(vertArray)); glUnmapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER); delete[]boneTransforms; }
void Drawable::draw(const ProjectionInfo& proj, const LightContainer& lights) const { if (m_mesh.expired()) return; auto& mesh = *getMesh(); { auto& shdr = getShader(); auto& modelMat = getObject()->getTransform().getMatrix(); const auto VMMatrix = proj.viewMatrix * modelMat; shdr.setUniform("u_PVMMatrix", proj.projectionMatrix * VMMatrix); if (!mesh.hasVertexComponent(Mesh::Color)) { // Default vertex color // Used if the mesh itself doesn't have colors glCheck(glVertexAttrib4fv(Mesh::VertexIndex::Color, &getColor().colors[0])); } if (getMaterial()) { auto& mat = *getMaterial(); if (mat.getAttributes() & Material::LightingAttribs) { shdr.setUniform("u_VMMatrix", VMMatrix); shdr.setUniform("u_NMatrix", glm::transpose(glm::inverse(glm::mat3(VMMatrix)))); lights.sendToShader(shdr, *this, proj.viewMatrix); } mat.sendToShader(shdr); } static const DynamicSetting<bool> validateSetting("engine@Debug|bValidateShaders", false); if (validateSetting.value && !shdr.validate()) return; } mesh.draw(); }
void VectorIcon::Draw (int x, int y, const float* color_ptr, short gamma, float rotate, float scale_x, float scale_y) const { AbstractWindow::shaders()->widget_triangle_program()->use(); glVertexAttrib4fv(AttributeColor, color_ptr); glUniform2f( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_POSITION), x, y); glUniform1i( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_GAMMA), gamma); glUniform1i( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_ANTI_ALIAS), 1); glUniform1f( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_ROTATION), rotate); glUniform2f( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_SCALE), scale_x, scale_y); glBindVertexArray(vao_); glDrawElements(GL_TRIANGLES, elements_, GL_UNSIGNED_INT, BUFFER_OFFSET(0)); glUniform1f( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_ROTATION), 0.f); glUniform2f( AbstractWindow::shaders()->location(Shaders::WIDGET_TRIANGLE_SCALE), 1.f, 1.f); }
void Shader::use(bool forceReload) { static Shader *previousShader = nullptr; static uint32 previousNumAttributes = 0; if (this == previousShader && !forceReload) return; // The previous shader might have had more attributes. Disable any extra ones. if (_attributes.size() < previousNumAttributes) { for (uint32 i = _attributes.size(); i < previousNumAttributes; ++i) { glDisableVertexAttribArray(i); } } previousShader = this; previousNumAttributes = _attributes.size(); glUseProgram(*_shaderNo); for (uint32 i = 0; i < _attributes.size(); ++i) { Graphics::VertexAttrib &attrib = _attributes[i]; if (attrib._enabled) { glEnableVertexAttribArray(i); glBindBuffer(GL_ARRAY_BUFFER, attrib._vbo); glVertexAttribPointer(i, attrib._size, attrib._type, attrib._normalized, attrib._stride, (const GLvoid *)attrib._offset); } else { glDisableVertexAttribArray(i); switch (attrib._size) { case 2: glVertexAttrib2fv(i, attrib._const); break; case 3: glVertexAttrib3fv(i, attrib._const); break; case 4: glVertexAttrib4fv(i, attrib._const); break; } } } }
void rglVertexAttrib4fv(GLuint name, GLfloat* v) { glVertexAttrib4fv(name, v); }
bool FRenderState::ApplyShader() { if (mSpecialEffect > EFF_NONE) { activeShader = GLRenderer->mShaderManager->BindEffect(mSpecialEffect); } else { activeShader = GLRenderer->mShaderManager->Get(mTextureEnabled ? mEffectState : 4, mAlphaThreshold >= 0.f); activeShader->Bind(); } int fogset = 0; if (mFogEnabled) { if ((mFogColor & 0xffffff) == 0) { fogset = gl_fogmode; } else { fogset = -gl_fogmode; } } glVertexAttrib4fv(VATTR_COLOR, mColor.vec); activeShader->muDesaturation.Set(mDesaturation / 255.f); activeShader->muFogEnabled.Set(fogset); activeShader->muTextureMode.Set(mTextureMode); activeShader->muCameraPos.Set(mCameraPos.vec); activeShader->muLightParms.Set(mLightParms); activeShader->muFogColor.Set(mFogColor); activeShader->muObjectColor.Set(mObjectColor); activeShader->muDynLightColor.Set(mDynColor.vec); activeShader->muInterpolationFactor.Set(mInterpolationFactor); activeShader->muClipHeightTop.Set(mClipHeightTop); activeShader->muClipHeightBottom.Set(mClipHeightBottom); activeShader->muTimer.Set(gl_frameMS * mShaderTimer / 1000.f); activeShader->muAlphaThreshold.Set(mAlphaThreshold); activeShader->muLightIndex.Set(mLightIndex); // will always be -1 for now activeShader->muClipSplit.Set(mClipSplit); if (mGlowEnabled) { activeShader->muGlowTopColor.Set(mGlowTop.vec); activeShader->muGlowBottomColor.Set(mGlowBottom.vec); activeShader->muGlowTopPlane.Set(mGlowTopPlane.vec); activeShader->muGlowBottomPlane.Set(mGlowBottomPlane.vec); activeShader->currentglowstate = 1; } else if (activeShader->currentglowstate) { // if glowing is on, disable it. static const float nulvec[] = { 0.f, 0.f, 0.f, 0.f }; activeShader->muGlowTopColor.Set(nulvec); activeShader->muGlowBottomColor.Set(nulvec); activeShader->muGlowTopPlane.Set(nulvec); activeShader->muGlowBottomPlane.Set(nulvec); activeShader->currentglowstate = 0; } if (mColormapState != activeShader->currentfixedcolormap) { float r, g, b; activeShader->currentfixedcolormap = mColormapState; if (mColormapState == CM_DEFAULT) { activeShader->muFixedColormap.Set(0); } else if (mColormapState < CM_MAXCOLORMAP) { FSpecialColormap *scm = &SpecialColormaps[gl_fixedcolormap - CM_FIRSTSPECIALCOLORMAP]; float m[] = { scm->ColorizeEnd[0] - scm->ColorizeStart[0], scm->ColorizeEnd[1] - scm->ColorizeStart[1], scm->ColorizeEnd[2] - scm->ColorizeStart[2], 0.f }; activeShader->muFixedColormap.Set(1); activeShader->muColormapStart.Set(scm->ColorizeStart[0], scm->ColorizeStart[1], scm->ColorizeStart[2], 0.f); activeShader->muColormapRange.Set(m); } else if (mColormapState == CM_FOGLAYER) { activeShader->muFixedColormap.Set(3); } else if (mColormapState == CM_LITE) { if (gl_enhanced_nightvision) { r = 0.375f, g = 1.0f, b = 0.375f; } else { r = g = b = 1.f; } activeShader->muFixedColormap.Set(2); activeShader->muColormapStart.Set(r, g, b, 1.f); } else if (mColormapState >= CM_TORCH) { int flicker = mColormapState - CM_TORCH; r = (0.8f + (7 - flicker) / 70.0f); if (r > 1.0f) r = 1.0f; b = g = r; if (gl_enhanced_nightvision) b = g * 0.75f; activeShader->muFixedColormap.Set(2); activeShader->muColormapStart.Set(r, g, b, 1.f); } } if (mTextureMatrixEnabled) { mTextureMatrix.matrixToGL(activeShader->texturematrix_index); activeShader->currentTextureMatrixState = true; } else if (activeShader->currentTextureMatrixState) { activeShader->currentTextureMatrixState = false; identityMatrix.matrixToGL(activeShader->texturematrix_index); } if (mModelMatrixEnabled) { mModelMatrix.matrixToGL(activeShader->modelmatrix_index); activeShader->currentModelMatrixState = true; } else if (activeShader->currentModelMatrixState) { activeShader->currentModelMatrixState = false; identityMatrix.matrixToGL(activeShader->modelmatrix_index); } return true; }
void vertex_attrib_4fv(gl::uint_t index, const gl::float_t * v) { glVertexAttrib4fv(index, v); }
bool ShaderManager :: SetAttributeVector ( int location, const Vector4D& value ) { glVertexAttrib4fv ( location, value ); return true; }
// 渲染 ///////////////////////////////////////////////////////////////////////////////// void OGLESRenderEngine::DoRender(RenderTechnique const & tech, RenderLayout const & rl) { uint32_t const num_instance = rl.NumInstances(); BOOST_ASSERT(num_instance != 0); OGLESShaderObjectPtr cur_shader = checked_pointer_cast<OGLESShaderObject>(tech.Pass(0)->GetShaderObject()); checked_cast<OGLESRenderLayout const *>(&rl)->Active(cur_shader); size_t const vertexCount = rl.UseIndices() ? rl.NumIndices() : rl.NumVertices(); GLenum mode; uint32_t primCount; OGLESMapping::Mapping(mode, primCount, rl); numPrimitivesJustRendered_ += num_instance * primCount; numVerticesJustRendered_ += num_instance * vertexCount; GLenum index_type = GL_UNSIGNED_SHORT; uint8_t* index_offset = nullptr; if (rl.UseIndices()) { if (EF_R16UI == rl.IndexStreamFormat()) { index_type = GL_UNSIGNED_SHORT; index_offset += rl.StartIndexLocation() * 2; } else { index_type = GL_UNSIGNED_INT; index_offset += rl.StartIndexLocation() * 4; } } uint32_t const num_passes = tech.NumPasses(); size_t const inst_format_size = rl.InstanceStreamFormat().size(); if (glloader_GLES_VERSION_3_0() && rl.InstanceStream()) { OGLESGraphicsBuffer& stream(*checked_pointer_cast<OGLESGraphicsBuffer>(rl.InstanceStream())); uint32_t const instance_size = rl.InstanceSize(); BOOST_ASSERT(num_instance * instance_size <= stream.Size()); uint8_t* elem_offset = nullptr; for (size_t i = 0; i < inst_format_size; ++ i) { vertex_element const & vs_elem = rl.InstanceStreamFormat()[i]; GLint attr = cur_shader->GetAttribLocation(vs_elem.usage, vs_elem.usage_index); if (attr != -1) { GLint const num_components = static_cast<GLint>(NumComponents(vs_elem.format)); GLenum type; GLboolean normalized; OGLESMapping::MappingVertexFormat(type, normalized, vs_elem.format); normalized = (((VEU_Diffuse == vs_elem.usage) || (VEU_Specular == vs_elem.usage)) && !IsFloatFormat(vs_elem.format)) ? GL_TRUE : normalized; GLvoid* offset = static_cast<GLvoid*>(elem_offset + rl.StartInstanceLocation() * instance_size); stream.Active(false); glVertexAttribPointer(attr, num_components, type, normalized, instance_size, offset); glEnableVertexAttribArray(attr); glVertexAttribDivisor(attr, 1); } elem_offset += vs_elem.element_size(); } if (so_rl_) { glBeginTransformFeedback(so_primitive_mode_); } if (rl.UseIndices()) { for (uint32_t i = 0; i < num_passes; ++ i) { RenderPassPtr const & pass = tech.Pass(i); pass->Bind(); if (so_rl_) { OGLESShaderObjectPtr shader = checked_pointer_cast<OGLESShaderObject>(pass->GetShaderObject()); glTransformFeedbackVaryings(shader->GLSLProgram(), static_cast<GLsizei>(so_vars_ptrs_.size()), &so_vars_ptrs_[0], GL_SEPARATE_ATTRIBS); for (uint32_t j = 0; j < so_buffs_.size(); ++ j) { glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, j, so_buffs_[j]); } } glDrawElementsInstanced(mode, static_cast<GLsizei>(rl.NumIndices()), index_type, index_offset, num_instance); pass->Unbind(); } } else { for (uint32_t i = 0; i < num_passes; ++ i) { RenderPassPtr const & pass = tech.Pass(i); pass->Bind(); if (so_rl_) { OGLESShaderObjectPtr shader = checked_pointer_cast<OGLESShaderObject>(pass->GetShaderObject()); glTransformFeedbackVaryings(shader->GLSLProgram(), static_cast<GLsizei>(so_vars_ptrs_.size()), &so_vars_ptrs_[0], GL_SEPARATE_ATTRIBS); for (uint32_t j = 0; j < so_buffs_.size(); ++ j) { glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, j, so_buffs_[j]); } } glDrawArraysInstanced(mode, rl.StartVertexLocation(), static_cast<GLsizei>(rl.NumVertices()), num_instance); pass->Unbind(); } } if (so_rl_) { glEndTransformFeedback(); } for (size_t i = 0; i < inst_format_size; ++ i) { vertex_element const & vs_elem = rl.InstanceStreamFormat()[i]; GLint attr = cur_shader->GetAttribLocation(vs_elem.usage, vs_elem.usage_index); if (attr != -1) { glDisableVertexAttribArray(attr); glVertexAttribDivisor(attr, 0); } } } else { for (uint32_t instance = rl.StartInstanceLocation(); instance < rl.StartInstanceLocation() + num_instance; ++ instance) { if (rl.InstanceStream()) { GraphicsBuffer& stream = *rl.InstanceStream(); uint32_t const instance_size = rl.InstanceSize(); BOOST_ASSERT(num_instance * instance_size <= stream.Size()); GraphicsBuffer::Mapper mapper(stream, BA_Read_Only); uint8_t const * buffer = mapper.Pointer<uint8_t>(); uint32_t elem_offset = 0; for (size_t i = 0; i < inst_format_size; ++ i) { BOOST_ASSERT(elem_offset < instance_size); vertex_element const & vs_elem = rl.InstanceStreamFormat()[i]; GLint attr = cur_shader->GetAttribLocation(vs_elem.usage, vs_elem.usage_index); if (attr != -1) { void const * addr = &buffer[instance * instance_size + elem_offset]; GLfloat const * float_addr = static_cast<GLfloat const *>(addr); GLint const num_components = static_cast<GLint>(NumComponents(vs_elem.format)); GLenum type; GLboolean normalized; OGLESMapping::MappingVertexFormat(type, normalized, vs_elem.format); normalized = (((VEU_Diffuse == vs_elem.usage) || (VEU_Specular == vs_elem.usage)) && !IsFloatFormat(vs_elem.format)) ? GL_TRUE : normalized; switch (num_components) { case 1: BOOST_ASSERT(IsFloatFormat(vs_elem.format)); glVertexAttrib1fv(attr, float_addr); break; case 2: BOOST_ASSERT(IsFloatFormat(vs_elem.format)); glVertexAttrib2fv(attr, float_addr); break; case 3: BOOST_ASSERT(IsFloatFormat(vs_elem.format)); glVertexAttrib3fv(attr, float_addr); break; case 4: if (IsFloatFormat(vs_elem.format)) { glVertexAttrib4fv(attr, float_addr); } else { GLubyte const * byte_addr = static_cast<GLubyte const *>(addr); if (normalized) { glVertexAttrib4f(attr, byte_addr[0] / 255.0f, byte_addr[1] / 255.0f, byte_addr[2] / 255.0f, byte_addr[3] / 255.0f); } else { glVertexAttrib4f(attr, byte_addr[0], byte_addr[1], byte_addr[2], byte_addr[3]); } } break; default: BOOST_ASSERT(false); break; } } elem_offset += vs_elem.element_size(); } } if (so_rl_) { glBeginTransformFeedback(so_primitive_mode_); } if (rl.UseIndices()) { for (uint32_t i = 0; i < num_passes; ++ i) { RenderPassPtr const & pass = tech.Pass(i); pass->Bind(); if (so_rl_) { OGLESShaderObjectPtr shader = checked_pointer_cast<OGLESShaderObject>(pass->GetShaderObject()); glTransformFeedbackVaryings(shader->GLSLProgram(), static_cast<GLsizei>(so_vars_ptrs_.size()), &so_vars_ptrs_[0], GL_SEPARATE_ATTRIBS); for (uint32_t j = 0; j < so_buffs_.size(); ++ j) { glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, j, so_buffs_[j]); } } glDrawElements(mode, static_cast<GLsizei>(rl.NumIndices()), index_type, index_offset); pass->Unbind(); } } else { for (uint32_t i = 0; i < num_passes; ++ i) { RenderPassPtr const & pass = tech.Pass(i); pass->Bind(); if (so_rl_) { OGLESShaderObjectPtr shader = checked_pointer_cast<OGLESShaderObject>(pass->GetShaderObject()); glTransformFeedbackVaryings(shader->GLSLProgram(), static_cast<GLsizei>(so_vars_ptrs_.size()), &so_vars_ptrs_[0], GL_SEPARATE_ATTRIBS); for (uint32_t j = 0; j < so_buffs_.size(); ++ j) { glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, j, so_buffs_[j]); } } glDrawArrays(mode, rl.StartVertexLocation(), static_cast<GLsizei>(rl.NumVertices())); pass->Unbind(); } } if (so_rl_) { glEndTransformFeedback(); } } } checked_cast<OGLESRenderLayout const *>(&rl)->Deactive(cur_shader); }
void GraphicsContext3D::vertexAttrib4fv(GC3Duint index, GC3Dfloat* array) { makeContextCurrent(); glVertexAttrib4fv(index, array); }