void TopazSample::drawModel(GLenum mode, NvGLSLProgram& program, TopazGLModel& model) { glVertexAttribFormat(VERTEX_POS, 3, GL_FLOAT, GL_FALSE, model.getModel()->getCompiledPositionOffset()); glVertexAttribBinding(VERTEX_POS, 0); glEnableVertexAttribArray(VERTEX_POS); program.enable(); program.bindTextureRect("pattern", 0, brushStyle->getTextureId()); glBindBufferRange(GL_UNIFORM_BUFFER, UBO_OBJECT, model.getBufferID("ubo"), 0, sizeof(ObjectData)); glBindVertexBuffer(0, model.getBufferID("vbo"), 0, model.getModel()->getCompiledVertexSize() * sizeof(float)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.getBufferID("ibo")); glDrawElements(mode, model.getModel()->getCompiledIndexCount(NvModelPrimType::TRIANGLES), GL_UNSIGNED_INT, nullptr); if (model.cornerPointsExists()) { glBindBufferRange(GL_UNIFORM_BUFFER, UBO_OBJECT, model.getCornerBufferID("ubo"), 0, sizeof(ObjectData)); glBindVertexBuffer(0, model.getCornerBufferID("vbo"), 0, sizeof(nv::vec3f)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.getCornerBufferID("ibo")); glDrawElements(GL_LINE_STRIP, model.getCornerIndices().size(), GL_UNSIGNED_INT, nullptr); } program.disable(); glDisableVertexAttribArray(VERTEX_POS); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindVertexBuffer(0, 0, 0, 0); }
//====================================================================== //====================================================================== void NvGraphicShaderGL::Load(const char* vert, const char* frag) { NvGLSLProgram *prog = NvGLSLProgram::createFromStrings(vert, frag); CHECK_GL_ERROR(); if (prog==NULL) { // !!!!TBD TODO return; } m_program = prog; prog->enable(); m_positionIndex = prog->getAttribLocation("position"); m_uvIndex = prog->getAttribLocation("tex"); prog->setUniform1i(prog->getUniformLocation("sampler"), 0); // texunit index zero. m_matrixIndex = prog->getUniformLocation("pixelToClipMat"); m_alphaIndex = prog->getUniformLocation("alpha"); m_colorIndex = prog->getUniformLocation("color"); prog->disable(); CHECK_GL_ERROR(); }
int CShaderObject::GenProgram(const char* vertexShader, const char* fragmentShader) { NvGLSLProgram* prog = NvGLSLProgram::createFromStrings(vertexShader, fragmentShader); if (prog) { pid = prog->getProgram(); delete prog; } else pid =-1; return pid; }
void SceneRenderer::drawScene(NvGLSLProgram& a_proc, const MatrixStorage& mats, RenderAlphaTest doAlpha, bool a_renderDepth) { CHECK_GL_ERROR(); MatrixStorage ncMats = mats; GLint positionAttrHandle = a_proc.getAttribLocation("g_Position"); GLint normalAttrHandle = a_proc.getAttribLocation("g_Normal", false); GLint texCoordAttrHandle = a_proc.getAttribLocation("g_TexCoord", false); CHECK_GL_ERROR(); // these matrices cam be set once a_proc.setUniformMatrix4fv("g_ProjectionMatrix", ncMats.m_projection._array); printMatrixLog("viewMatrix", ncMats.m_view); printMatrixLog("set g_ProjectionMatrix", ncMats.m_projection); a_proc.setUniform1f("g_lightAmbient", m_scene.m_lightAmbient); a_proc.setUniform1f("g_lightDiffuse", m_scene.m_lightDiffuse); a_proc.setUniform3f("g_lightDirection", m_scene.m_lightVector[0], m_scene.m_lightVector[1], m_scene.m_lightVector[2]); CHECK_GL_ERROR(); // Note that we are drawing the terrain first which is a sub-optimal rendering order and a deliberate mistake. if (!a_renderDepth && (doAlpha & RENDER_SOLID)) { nv::matrix4f modelMatrix; modelMatrix.make_identity(); modelMatrix.set_scale(nv::vec3f(1.0f, 1.0, 1.0f)); ncMats.m_model = modelMatrix; ncMats.multiply(); CHECK_GL_ERROR(); a_proc.setUniformMatrix4fv("g_ModelViewMatrix", ncMats.m_modelView._array); a_proc.setUniformMatrix4fv("g_ModelViewProjectionMatrix", ncMats.m_modelViewProjection._array); a_proc.setUniformMatrix4fv("g_LightModelViewMatrix", m_scene.m_lightView._array); CHECK_GL_ERROR(); a_proc.setUniform4f("g_color", 1.0f, 1.0f, 1.0f, 1.0f); CHECK_GL_ERROR(); a_proc.bindTexture2D("g_texture", 0, m_pTerrain->getColorTex()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); CHECK_GL_ERROR(); m_pTerrain->Draw(positionAttrHandle, normalAttrHandle, texCoordAttrHandle); CHECK_GL_ERROR(); } // render objects for(uint32_t i = 0; i < m_models.size(); i++) { MeshObj& mesh = m_models[i]; if (doAlpha == RENDER_ALL || (doAlpha == RENDER_ALPHA && mesh.m_alphaTest) || (doAlpha == RENDER_SOLID && !mesh.m_alphaTest)) { ncMats.m_model = mesh.m_modelMatrix; ncMats.multiply(); CHECK_GL_ERROR(); a_proc.setUniformMatrix4fv("g_ModelViewMatrix", ncMats.m_modelView._array); a_proc.setUniformMatrix4fv("g_ModelViewProjectionMatrix", ncMats.m_modelViewProjection._array); printMatrixLog("set modelViewProjection", ncMats.m_modelViewProjection); CHECK_GL_ERROR(); if (doAlpha & RENDER_ALPHA) { a_proc.bindTexture2D("g_texture", 0, mesh.m_texId); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } if(!a_renderDepth) { a_proc.setUniform4f("g_color", mesh.m_color.x, mesh.m_color.y, mesh.m_color.z, mesh.m_color.w); a_proc.setUniform1f("g_lightSpecular", mesh.m_specularValue); } if (mesh.m_cullFacing) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); CHECK_GL_ERROR(); mesh.m_pModelData->drawElements(positionAttrHandle, normalAttrHandle, texCoordAttrHandle); CHECK_GL_ERROR(); } } glEnable(GL_CULL_FACE); }
unsigned int generate1DConvolutionFP_filter(const char* vs, float *weights, int width, bool vertical, bool tex2D, int img_width, int img_height) { // calculate new set of weights and offsets int nsamples = 2*width+1; int nsamples2 = (int) ceilf(nsamples/2.0f); float *weights2 = new float [nsamples2]; float *offsets = new float [nsamples2]; for(int i=0; i<nsamples2; i++) { float a = weights[i*2]; float b; if (i*2+1 > nsamples-1) b = 0; else b = weights[i*2+1]; weights2[i] = a + b; offsets[i] = b / (a + b); // printf("%d: %f %f\n", i, weights2[i], offsets[i]); } // printf("nsamples = %d\n", nsamples2); char szBuffer[16]; std::ostringstream ost; ost << "precision highp float;\n" "uniform sampler2D TexSampler;\n" "varying vec2 TexCoord0;\n" "void main()\n" "{\n" "vec3 sum = vec3(0.0,0.0,0.0);\n" "vec2 texcoord;\n"; for(int i=0; i<nsamples2; i++) { float x_offset = 0, y_offset = 0; if (vertical) { y_offset = (i*2)-width+offsets[i]; } else { x_offset = (i*2)-width+offsets[i]; } if (tex2D) { x_offset = x_offset / img_width; y_offset = y_offset / img_height; } float weight = weights2[i]; sprintf(szBuffer, "%f", x_offset); ost << "texcoord = TexCoord0 + vec2(" << szBuffer; sprintf(szBuffer, "%f", y_offset); ost << ", " << szBuffer << ");\n"; sprintf(szBuffer, "%f", weight); ost << "sum += texture2D(TexSampler, texcoord).rgb*" << szBuffer << ";\n"; } ost << "gl_FragColor = vec4(sum, 1.0);\n" "}\n"; delete [] weights2; delete [] offsets; NvGLSLProgram* program = NvGLSLProgram::createFromStrings(vs, (char*)ost.str().c_str()); int id = program->getProgram(); return id; }