/** * \brief Creates a shader program. * \param name The name of the program. * \param file A stream containing the fragment shader code. * \pre name is not used by another shader. * \post get_fragment_shader(name) is the fragment_shader load from \a file */ void bear::visual::image_manager::load_shader_program ( const std::string& name, std::istream& file ) { CLAW_PRECOND( !has_shader_program(name) ); m_shader_program[ name ] = shader_program( file ); } // image_manager::load_shader_program()
void shader_drawbuffer(struct render_buffer * rb, float tx, float ty, float scale) { rs_commit(); RID glid = texture_glid(rb->texid); if (glid == 0) return; shader_texture(glid, 0); render_set(RS->R, VERTEXBUFFER, rb->vbid, 0); float sx = scale; float sy = scale; screen_trans(&sx, &sy); screen_trans(&tx, &ty); float v[4] = { sx, sy, tx, ty }; // we should call shader_adduniform to add "st" uniform first shader_setuniform(PROGRAM_RENDERBUFFER, 0, UNIFORM_FLOAT4, v); shader_program(PROGRAM_RENDERBUFFER, NULL); RS->drawcall++; renderbuffer_commit(rb); render_set(RS->R, VERTEXBUFFER, RS->vertex_buffer, 0); }
GLFWwindow *setup_program(t_properties *properties) { static const float verts[] = {-1, 1, -1, -1, 1, -1, 1, 1}; static const GLuint inds[] = {0, 1, 3, 1, 2, 3}; GLFWwindow *window; properties->width = 1000; properties->height = 1000; window = make_glfw(properties->width, properties->height); glClearColor(0, 0, 0, 1); properties->model = vao(); properties->verts = data_buffer((GLvoid *)verts, sizeof(verts)); properties->indices = index_buffer((GLvoid *)inds, sizeof(inds)); vao_add_indices(properties->model, properties->indices); vao_add_vdata(properties->model, properties->verts, 2, GL_FALSE); ft_putnbr(glGetError()); ft_putendl(" -0"); properties->shaders[0] = load_vertex(); properties->shaders[1] = load_fragment(properties->map); properties->program = shader_program(properties->shaders, 2); ft_putnbr(glGetError()); ft_putendl(" -1"); glUseProgram(properties->program); glBindVertexArray(properties->model); glEnableVertexAttribArray(0); init_uniforms(properties->program); return (window); }
int main(int argc, char* argv[]) { osg::ref_ptr<osg::Group> root(new osg::Group); osg::ref_ptr<osg::Geode> sphere(build_sphere()); root->addChild(sphere.get()); osg::ref_ptr<osg::Light> light(new osg::Light); light->setLightNum(1); light->setPosition(osg::Vec4(0., 0., 0., 1.)); osg::ref_ptr<osg::LightSource> light_source(new osg::LightSource); light_source->setLight(light.get()); osg::ref_ptr<osg::MatrixTransform> transform(new osg::MatrixTransform); transform->addChild(light_source.get()); osg::Matrix matrix; matrix.makeTranslate(osg::Vec3(10., 0., 0.)); transform->setMatrix(matrix); root->addChild(transform.get()); boost::shared_ptr<shade::GLSLWrapper> state(new shade::osg::Wrapper(sphere->getOrCreateStateSet())); boost::shared_ptr<shade::Program> shader_program(setup_shading(state)); shader_program->compile(); state->make_current(); shader_program->upload(); run_viewer(root.get()); }
static void switch_program(struct sprite_trans *t, int def) { int prog = t->program; if (prog == PROGRAM_DEFAULT) { prog = def; } shader_program(prog, t->additive); }
static void switch_program(struct sprite_trans *t, int def, struct material *m) { int prog = t->program; if (prog == PROGRAM_DEFAULT) { prog = def; } shader_program(prog, m); }
void shader_st(int prog, float x, float y, float scale) { rs_commit(); shader_program(prog); struct program *p = &RS->program[prog]; if (!p || p->st == -1) return; glUniform4f(p->st, scale, scale, x, y); }
/* int texture table float[16] uint32_t color uint32_t additive */ static int ldraw(lua_State *L) { int tex = (int)luaL_checkinteger(L,1); int texid = texture_glid(tex); if (texid == 0) { lua_pushboolean(L,0); return 1; } luaL_checktype(L, 2, LUA_TTABLE); uint32_t color = 0xffffffff; if (!lua_isnoneornil(L,3)) { color = (uint32_t)lua_tounsigned(L,3); } uint32_t additive = (uint32_t)luaL_optunsigned(L,4,0); shader_program(PROGRAM_PICTURE,additive); shader_texture(texid); int n = lua_rawlen(L, 2); int point = n/4; if (point * 4 != n) { return luaL_error(L, "Invalid polygon"); } #if !defined(_MSC_VER) float vb[n]; #else msvc::dynarray<float> vb(n); #endif int i; for (i=0;i<point;i++) { lua_rawgeti(L, 2, i*2+1); lua_rawgeti(L, 2, i*2+2); lua_rawgeti(L, 2, point*2+i*2+1); lua_rawgeti(L, 2, point*2+i*2+2); float tx = lua_tonumber(L, -4); float ty = lua_tonumber(L, -3); float vx = lua_tonumber(L, -2); float vy = lua_tonumber(L, -1); lua_pop(L,4); screen_trans(&vx,&vy); texture_coord(tex, &tx, &ty); vb[i*4+0] = vx + 1.0f; vb[i*4+1] = vy - 1.0f; vb[i*4+2] = tx; vb[i*4+3] = ty; } if (point == 4) { shader_draw(vb, color); } else { shader_drawpolygon(point, vb, color); } return 0; }
/* int texture table float[16] uint32_t color uint32_t additive */ static int ldraw(lua_State *L) { int tex = (int)luaL_checkinteger(L,1); int texid = texture_glid(tex); if (texid == 0) { lua_pushboolean(L,0); return 1; } luaL_checktype(L, 2, LUA_TTABLE); uint32_t color = 0xffffffff; if (!lua_isnoneornil(L,3)) { color = (uint32_t)lua_tounsigned(L,3); } uint32_t additive = (uint32_t)luaL_optunsigned(L,4,0); shader_program(PROGRAM_PICTURE); shader_texture(texid); int n = lua_rawlen(L, 2); int point = n/4; if (point * 4 != n) { return luaL_error(L, "Invalid polygon"); } ARRAY(struct vertex_pack, vb, point); int i; for (i=0;i<point;i++) { lua_rawgeti(L, 2, i*2+1); lua_rawgeti(L, 2, i*2+2); lua_rawgeti(L, 2, point*2+i*2+1); lua_rawgeti(L, 2, point*2+i*2+2); float tx = lua_tonumber(L, -4); float ty = lua_tonumber(L, -3); float vx = lua_tonumber(L, -2); float vy = lua_tonumber(L, -1); uint16_t u,v; lua_pop(L,4); screen_trans(&vx,&vy); texture_coord(tex, tx, ty, &u, &v); vb[i].vx = vx + 1.0f; vb[i].vy = vy - 1.0f; vb[i].tx = u; vb[i].ty = v; } if (point == 4) { shader_draw(vb, color, additive); } else { shader_drawpolygon(point, vb, color, additive); } return 0; }
int main(int argc, char* argv[]) { osg::ref_ptr<osg::Geode> quad(build_quad()); osg::ref_ptr<osg::Image> image = osgDB::readImageFile("examples/pattern.dds"); osg::ref_ptr<osg::Texture2D> tex = new osg::Texture2D; tex->setImage(image.get()); boost::shared_ptr<shade::osg::Texture> shade_tex(new shade::osg::Texture(tex.get())); boost::shared_ptr<shade::GLSLWrapper> state(new shade::osg::Wrapper(quad->getOrCreateStateSet())); boost::shared_ptr<shade::Program> shader_program(setup_shading(state, shade_tex)); shader_program->compile(); state->make_current(); shader_program->upload(); run_viewer(quad.get()); }
static int luniform_set(lua_State *L) { int prog = luaL_checkinteger(L, 1); shader_program(prog); int index = luaL_checkinteger(L, 2); enum UNIFORM_FORMAT t = luaL_checkinteger(L, 3); float v[16]; // 16 is matrix 4x4 int n = 0; switch(t) { case UNIFORM_FLOAT1: n = 1; break; case UNIFORM_FLOAT2: n = 2; break; case UNIFORM_FLOAT3: n = 3; break; case UNIFORM_FLOAT4: n = 4; break; case UNIFORM_FLOAT33: n = 9; break; case UNIFORM_FLOAT44: n = 16; break; default: return luaL_error(L, "Invalid uniform format %d", t); break; } int top = lua_gettop(L); if (top != n + 3) { return luaL_error(L, "Need float %d, only %d passed", n, top - 3); } int i; for (i=0;i<n;i++) { v[i] = luaL_checknumber(L, 4+i); } shader_setuniform(index, t, v); return 0; }
void shader_drawbuffer(struct render_buffer * rb, float tx, float ty, float scale) { rs_commit(); int glid = texture_glid(rb->texid); if (glid == 0) return; shader_texture(glid); shader_program(PROGRAM_RENDERBUFFER); RS->drawcall++; glBindBuffer(GL_ARRAY_BUFFER, rb->vbid); float sx = scale; float sy = scale; screen_trans(&sx, &sy); screen_trans(&tx, &ty); struct program *p = &RS->program[RS->current_program]; glUniform4f(p->st, sx, sy, tx, ty); renderbuffer_commit(rb); }
int shader_adduniform(int prog, const char * name, enum UNIFORM_FORMAT t) { // reset current_program assert(prog >=0 && prog < MAX_PROGRAM); shader_program(prog, NULL); struct program * p = &RS->program[prog]; assert(p->uniform_number < MAX_UNIFORM); int loc = render_shader_locuniform(RS->R, name); int index = p->uniform_number++; struct uniform * u = &p->uniform[index]; u->loc = loc; u->type = t; if (index == 0) { u->offset = 0; } else { struct uniform * lu = &p->uniform[index-1]; u->offset = lu->offset + shader_uniformsize(lu->type); } if (loc < 0) return -1; return index; }